weired problem in while loop

This is a discussion on weired problem in while loop within the C Programming forums, part of the General Programming Boards category; hello everyone! i am new here and i have a question. i wrote a program in c that get a ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    7

    weired problem in while loop

    hello everyone!

    i am new here and i have a question.
    i wrote a program in c that get a char and 2 numbers and with the char the user entered the program does some actions.
    Q is for quitting the program
    its all working nice to here, the problem is after i entered the while loop once and entered some inputs i get the result, but then when it comes back to the while loop its shows me the printf give me an error and shows the printf again and after that its ready to get an input.
    i tried to debug and for some reason the Variable "c" get a value of "/n" when it comes to the while loop in the second time, and i dont understand why.


    i uploaded a picture that shows the problem and added the code.
    someone sees the problem?

    Thanks.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    
    int main()
    {
    	int num1,num2;
    	int sum,check,min,max,p;
    	int gcd,i;
    	char c;
    	clrscr();
       while(c!='Q')
    	{
    	 num2=0;
    	 num1=0;
    	printf("\nEnter action (Q to quit), and two numbers by that order:");
    	scanf("%c", &c);
    	switch(c)
    	{
    		case 'a':
    			scanf("%d%d",&num1, &num2);
    			sum=(num1+num2);
    			check=sum%2;
    			if (check!=0)
    				printf("\nThe average is not a whole number");
    			else
    				printf("\nThe average is a whole number");
    			printf("\n\nThe average is: %.2f", ((float)(num1+num2)/2));
    		break;
    		case '*':
    			scanf("%d%d",&num1, &num2);
    			printf("\nThe product is: %d", num1*num2);
    		break;
    		case 'm':
    			scanf("%d%d",&num1, &num2);
    			min=(num1<num2)? num1:num2;
    			printf("The smaller number is: %d", min);
    		break;
    		case 'M':
    			scanf("%d%d",&num1, &num2);
    			max=(num1>num2)? num1:num2;
    			printf("The larger number is: %d", max);
    		break;
    		case 'g':
    		{
    			 scanf("%d%d", &num1, &num2);
    			 if(num1<num2)
    			 
    			 for(i=1;i<=num1;i++)
    			 {
    				 if(num1%i==0&&num2%i==0)
    					 gcd=i;
    			 }
    			 else
    			 {
    				 for(i=1;i<=num2;i++)
    				 { 
    					 if(num1%i==0&&num2%i==0)
    						 gcd=i;
    				 }
    			 }	
    			 
    				 printf("\GCD: %d", gcd);
    			 
    		 break;
    		 case 'Q':
    		  printf("\nProgram Terminated.");
    		 break;
    		 case '^':
    		  scanf("%d%d", &num1, &num2);
    		  p=pow(num1,num2);
    		  printf("\nThe result of %d^%d is: %d", num1, num2 , p);
    		 break;
    		 }
    		 default: printf("\nERROR");
    		 break;
    	}
    }
       getch();
       return 0;
    }
    Attached Images Attached Images  

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That is, when you enter your numbers with scanf, it leaves behind a newline (from pressing enter), which is acceptable input for "%c", so you get a "newline" as input.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    7
    what can i do to prevent that?

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    You could add getchar() to each of your switch cases, just above the statement break;

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Simple solution would be a getchar() at the end of the loop somewhere.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    I find getch() more suitable, I think getchar() accepts only enter.
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by hauzer View Post
    I find getch() more suitable, I think getchar() accepts only enter.
    No, getchar() accepts all characters. getch() is also non-standard, so it won't work on all platforms.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    Are you expecting say
    a22 33q
    to do something meaningful (for example)?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    7
    yes, a will say what to do with the 2 numbers you entered, but Q will quit the program.

    i tried getch() after the loop, it works fine, but when i press Q to quit the program it doesnt quit it just writes me "program terminated" and i enter to an endless loop

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Are you getchar()ing into variable c, so that c no longer has 'Q'?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  11. #11
    Registered User
    Join Date
    Dec 2008
    Posts
    7
    yes, i put getchar() in c, but Q get me into an endless loop

  12. #12
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    That's impossible, unless you've modified the code from you're previous post. Please post your current code.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  13. #13
    Registered User
    Join Date
    Dec 2008
    Posts
    7
    this is the current code, i put the getchar() in the end of the loop

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    
    int main()
    {
    	int num1,num2;
    	int sum,check,min,max,p;
    	int gcd,i;
    	char c;
    	clrscr();
       while(c!='Q')
    	{
    	 num2=0;
    	 num1=0;
    	printf("\nEnter action (Q to quit), and two numbers by that order:");
    	c=getchar();
    	switch(c)
    	{
    		case 'a':
    			scanf("&#37;d%d",&num1, &num2);
    			sum=(num1+num2);
    			check=sum%2;
    			if (check!=0)
    				printf("\nThe average is not a whole number");
    			else
    				printf("\nThe average is a whole number");
    			printf("\n\nThe average is: %.2f", ((float)(num1+num2)/2));
    		break;
    		case '*':
    			scanf("%d%d",&num1, &num2);
    			printf("\nThe product is: %d", num1*num2);
    		break;
    		case 'm':
    			scanf("%d%d",&num1, &num2);
    			min=(num1<num2)? num1:num2;
    			printf("The smaller number is: %d", min);
    		break;
    		case 'M':
    			scanf("%d%d",&num1, &num2);
    			max=(num1>num2)? num1:num2;
    			printf("The larger number is: %d", max);
    		break;
    		case 'g':
    		{
    			 scanf("%d%d", &num1, &num2);
    			 if(num1<num2)
    			 
    			 for(i=1;i<=num1;i++)
    			 {
    				 if(num1%i==0&&num2%i==0)
    					 gcd=i;
    			 }
    			 else
    			 {
    				 for(i=1;i<=num2;i++)
    				 { 
    					 if(num1%i==0&&num2%i==0)
    						 gcd=i;
    				 }
    			 }	
    			 
    				 printf("\GCD: %d", gcd);
    			 
    		 break;
    		 case 'Q':
    		  printf("\nProgram Terminated.");
    		 break;
    		 case '^':
    		  scanf("%d%d", &num1, &num2);
    		  p=pow(num1,num2);
    		  printf("\nThe result of %d^%d is: %d", num1, num2 , p);
    		 break;
    		 }
    		 default: printf("\nERROR");
    		 break;
    		 
    	}
    	c=getchar();
    }
       return 0;
    }

  14. #14
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Like I said. Since you are getchar()ing the '\n' into c, it will never contain the 'Q' it used to have. getchar() into a throw-away variable.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Dino View Post
    Like I said. Since you are getchar()ing the '\n' into c, it will never contain the 'Q' it used to have. getchar() into a throw-away variable.
    Or no variable at all.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM

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