Thread: nested while loop

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123

    nested while loop

    Hey all, program works, but one simple program. After starting program and typing in

    Code:
    101 S
    (number can be any regular float value), there the program is putting

    Code:
    0.00000
    Please help me remove it. Otherwise, code functions correctly.

    This exercise was out of one of the C programming books I am reading. Thanks for continuous help!

    Code:
    /* Write a program that acts as a simple "printing" calculator.  The program should allow the user to type in expressions of the form
     *
     * number	operator
     *   
     *  The following operators should be recognized by the program:
     *  +	-	*	/	S	E
     *
     *  The S operator tells the program to set the "accumulator" to the typed-in number.  The E operator tells the program that execution is to end.  The arithmetic operations are performated on the contents of the accumulator with the number that was keyed in acting as the second operand. */
    
    
    #include <stdio.h>
    
    int main(void)
    {
    	float number = 0;
    	float accuml = 0;
    
    	char operator;
    
    	printf("Begin Calulations\n");
    
    	int i = 1;
    
    	while(i <= 10)
    	{
    		scanf("%f %c", &number, &operator);
    
    		if(operator == 'S')
    			printf("%.5f\n", number);
    		else if(operator == '+')
    			accuml += number;
    		else if(operator == '-')
    			accuml -= number;
    		else if(operator == '*')
    			accuml *= number;
    		else if(operator == '/')
    			accuml /= number;
    		else if(operator == 'E')
    			break;
    		else
    			printf("Error Operator!\n");
    		
    
    		while (i <= 10)
    		{
    			printf("%f\n", accuml);
    			break;
    		}
    	}
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You might want to read your own comment about what you're supposed to do when the operator is S.

    EDIT:
    Also, are you aware that your "nested while loop" is just a glorified if statement? You could have done the same thing by changing it to an if statement and removing the break.

    Actually, since you don't change i within the outer loop, you don't even need this if statement at all, and if you don't really need i since you just have a deliberate infinite loop disguised as a loop controlled by an index.
    Last edited by laserlight; 02-21-2019 at 04:44 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123
    seems to be working fine now. Thanks laserlight!

    Code:
    /* Write a program that acts as a simple "printing" calculator.  The program should allow the user to type in expressions of the form
     *
     * number	operator
     *   
     *  The following operators should be recognized by the program:
     *  +	-	*	/	S	E
     *
     *  The S operator tells the program to set the "accumulator" to the typed-in number.  The E operator tells the program that execution is to end.  The arithmetic operations are performated on the contents of the accumulator with the number that was keyed in acting as the second operand. */
    
    
    #include <stdio.h>
    
    int main(void)
    {
    	float number = 0;
    	float accuml = 0;
    
    	char operator;
    
    	printf("Begin Calulations\n");
    
    	int i = 1;
    
    	while(i <= 10)
    	{
    		scanf("%f %c", &number, &operator);
    
    		if(operator == 'S')
    		{
    			printf("= %.5f\n", number);
    			accuml = 0;
    		}
    
    		else if(operator == '+')
    		{
    			accuml += number;
    		}
    
    		else if(operator == '-')
    		{
    			accuml -= number;
    		}
    
    		else if(operator == '*')
    		{
    			accuml *= number;
    		}
    
    		else if(operator == '/')
    		{
    			if(number == 0)
    			{
    				printf("Error! Divison by 0\n");
    				break;
    			}
    		}
    
    		else if(operator == 'E')
    		{
    			break;
    		}
    
    		else
    		{
    			printf("Error Operator!\n");
    		}
    		
    		while (i <= 10)
    		{
    			if(accuml == 0)
    			{
    				break;
    			}
    
    			else
    			{
    				printf("= %f\n", accuml);
    				break;
    			}
    		}
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with for loop and nested if's
    By Gil Carvalho in forum C Programming
    Replies: 10
    Last Post: 06-14-2012, 11:20 AM
  2. help with nested loop
    By khoavo123 in forum C Programming
    Replies: 0
    Last Post: 01-23-2012, 01:29 PM
  3. Nested while loop inside for loop
    By Sonny in forum C Programming
    Replies: 71
    Last Post: 07-31-2011, 08:38 PM
  4. Nested Loop
    By DuckCowMooQuack in forum C Programming
    Replies: 8
    Last Post: 02-15-2011, 10:37 AM
  5. Help About Nested Loop
    By Antigloss in forum C Programming
    Replies: 16
    Last Post: 10-17-2006, 10:18 AM

Tags for this Thread