Problem with fgets !

This is a discussion on Problem with fgets ! within the C Programming forums, part of the General Programming Boards category; Hello, in my program, i ask them to enter data, then i ask if they want to do it again ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    55

    Problem with fgets !

    Hello, in my program, i ask them to enter data, then i ask if they want to do it again (Y) - which repeats it again; or not (N) in which case it exits the program.

    My problem is: It works fine the first time, but when i type Y, it repeats ''Enter Text:'' and also prints ''Again? (Y/N):'', so that i can't answer the first part.

    Anyone know why it's doing this? It is having the same error as if i was using scanf and needed to 'flush' it (yes i know fflush(stdin) doesn't work )

    Here's the code:

    Code:
    #include <stdio.h>
    
    void fConvert(char[]);
    
    int main(void)
    {
    	char cText[100], cInputYN[5], cAnsYN;
    	int xAgain, iInputError;
    
    	do
    	{
    		printf("Enter text:");
    		fgets(cText, 100, stdin);
    
    		fConvert(cText);
    
    		do	
    		{
    			printf("\n\nAgain? [Y/N]");
    			fgets(cInputYN, 2, stdin);
    
    			cAnsYN = cInputYN[0];
    		
    
    			if (cInputYN[0] != '\n')
    			{
    				if (cAnsYN == 'Y' || cAnsYN == 'y')
    				{
    					xAgain = 1;
    					iInputError = 0;
    				}
    				else if (cAnsYN == 'N' || cAnsYN == 'n')
    				{
    					xAgain = 0;
    					iInputError = 0;
    				}
    				else
    				{
    					printf("\n\tInvalid letter. Please try again.\n");
    					iInputError = 1;
    				}
    			}
    			else 
    			{
    				printf("\n\tInvalid Input. Please try again.\n");
    				iInputError = 1;
    			}
    		}while (iInputError == 1);
    
    	}while (xAgain == 1);
    	
    	printf("\n\tThanks for using B-Cipher V0.1.\n\n");
    
    
    	return 0;
    }
    
    void fConvert(char b[100])
    {
    	int a = 0;
    
    	for(a; b[a]!='\0'; a++)
    	{
    		switch(b[a])
    		{
    			case 'a':
    				printf("t");
    				break;
    			case 'b':
    				printf("e");
    				break;
    			case 'c':
    				printf("s");
    				break;
    			case 'd':
    				printf("l");
    				break;
    			case 'e':
    				printf("a");
    				break;
    			case 'f':
    				printf("b");
    				break;
    			case 'g':
    				printf("c");
    				break;
    			case 'h':
    				printf("d");
    				break;
    			case 'i':
    				printf("f");
    				break;
    			case 'j':
    				printf("g");
    				break;
    			case 'k':
    				printf("h");
    				break;
    			case 'l':
    				printf("i");
    				break;
    			case 'm':
    				printf("j");
    				break;
    			case 'n':
    				printf("k");
    				break;
    			case 'o':
    				printf("m");
    				break;
    			case 'p':
    				printf("n");
    				break;
    			case 'q':
    				printf("o");
    				break;
    			case 'r':
    				printf("p");
    				break;
    			case 's':
    				printf("q");
    				break;
    			case 't':
    				printf("r");
    				break;
    			case 'u':
    				printf("u");
    				break;
    			case 'v':
    				printf("v");
    				break;
    			case 'w':
    				printf("w");
    				break;
    			case 'x':
    				printf("x");
    				break;
    			case 'y':
    				printf("y");
    				break;
    			case 'z':
    				printf("z");
    				break;
    			case ' ':
    				printf(" ");
    				break;
    			case '\n':
    				break;
    			default:
    				printf("\nError, was not a letter\n");
    		}
    	}
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >fgets(cInputYN, 2, stdin);
    Your size is off and causes the same effect as mixing scanf and fgets:
    Code:
    fgets(cInputYN, sizeof cInputYN, stdin);
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    55
    LOL rookie mistake! thanks mr. prelude!

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by Astra
    LOL rookie mistake! thanks mr. prelude!
    Wow!! First recorded in history. . . A man about to give birth!!! WOW!!!

  5. #5
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    mr.Prelude....hilarious.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I'm sure you can think up an alternative to that huge switch statement . . . perhaps something like this.
    Code:
    const char from[] = "abcd"  /* ... */, to[] = "tesl"  /* ... */, *p;
    
    if((p = strchr(from, b[a]))) putchar(to[from - p]);
    else puts("\nError, was not a letter");
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by Astra
    LOL rookie mistake! thanks mr. prelude!
    Prelude should be worried

    its not Mr.

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with fgets....
    By Huskar in forum C Programming
    Replies: 5
    Last Post: 03-29-2009, 11:13 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 04:36 PM
  3. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 09:10 AM
  4. print problem while using fgets()
    By learninC in forum C Programming
    Replies: 12
    Last Post: 05-15-2005, 10:29 PM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21