Thread: Loop control

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    13

    Loop control

    I do not understand how to pause my display loop. I thought the second scanf would do the job, but it doesn't. Here is the code

    Code:
      int main ()
    {
    	int op1;					// user input operand1
    	int op2;					// user input operand2
    	char operator;				// user input operator
    	int result;					// answer for calculation					
    	int status;					// open or close calculator program
    
    for (status = 0 ;status !='q'; )
    	{
    	printf("\n\t\tCALCULATOR\n\n");
    	printf("   Make your selection by entering 2 numbers\n");
    	printf("   and an operator as shown in the form below\n\n");
    	
    	printf("\ti1 + i2		i1 plus i2\n");
    	printf("\ti1 - i2		i1 minus i2\n");
    	printf("\ti1 * i2		i1 multiply times i2\n");
    	printf("\ti1 / i2		i1 divided by i2\n");
    	printf("\ti1 ^ i2		i1 to the power i2\n");
    	printf("\ti1 G i2		the greatest common divisor (gcd) of i1 and i2\n");
    	printf("\ti1 L i2		the least common multiple (lcm) of i1 and i2\n");
    	printf("\ti1 R i2		the square root of one quarter of the sum of i1 and i2\n");
    	printf("\ti1 Q i2		quit the calculator program\n\n");
    	
    	printf("  The above menu lists available operations on the left and a brief explanation\n");
    	printf("  on the right.  Use the format of the operations on the left to enter a formula\n");
    	printf("  \n");
    	printf("Type NUMBER   OPERATOR   NUMBER and press RETURN:\n");
    	scanf("%d %c %d", &op1, &operator, &op2);
    
    	 switch (operator)
    		{
    		case '+': result = plus(op1,op2);		break;
    		case '-': result = minus(op1,op2);		break;
    		case '*': result = multi(op1,op2);		break;
    		case '/': result = divd(op1,op2);		break;
    		case '^': result = powr(op1,op2);		break;	
    		case 'g': result = gcd(op1,op2);		break;
    		case 'l': result = lcm(op1,op2);		break;
    		case 'r': result = qtrsumroot(op1,op2);	break;
    		default: printf("Please re-enter your request\n"); break;
    		}
    printf("your formula is %d %c %d.  The answer is %d\n\n", op1, operator, op2, result); 	
    
    printf("Press any key to reset calculator.  Press Q to quit.\n");
    scanf("%c". &status);
    	}
    
    }
    thanks for any insight.
    Keith

  2. #2
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Code:
    scanf("%c". &status);
    [list=1][*] , not . after "%c"[*]%c corresponds to character input but status is int[/list=1]
    The one who says it cannot be done should never interrupt the one who is doing it.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    13

    revised

    thanks pinko,
    I updated the type of status to char and revised the notation on the scanf line. There must be a logic error. I am thinking that the for(status='0'; status !='q'; ) line at the start of the function will be updated by the scanf line at the end of the function. The loop passes by the scanf. hmmmm....

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    13

    clear input buffer

    Salem,
    Thank you, I did not realize that the input stream needs to be cleared. I very much appreciate you writing -- taking time to reply. This gives me the direction needed to understand what is going on. Of course I had heard that there are better ways than scanf and that scanf has limits, but I didnt' know exactly what that means nor how it would affect me in this instance.
    Thanks again for your direction,
    Keith

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    13

    scanf FIXED

    These changes made a vast improvment in the functionality and operability of the display screen that caused me a problem using scanf. My thanks to SALEM for assisting with specific code and explanations of the nature of the problem using scanf.

    Code:
    int main ()
    {
    					
    char status;	// open or close calculator program
    char buff[100];           // user input workspace 
    
    for (status = 0 ;status !='q'; )
    	{
    	
    	fgets( buff, sizeof(buff), stdin );
    	sscanf( buff, "%d %c %d", &op1, &operator, &op2);
    
    
    	 switch (operator)
    	{
    	}
    
    printf("Press ENTER to reset calculator.  Press Q to quit.\n");
    fgets( buff, sizeof(buff), stdin );
    sscanf( buff, "%c", &status);
    }
    
    }

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Both fgets and sscanf can fail, always check their return values.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  4. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM