Thread: while loop (sinple calculator)

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    9

    while loop (sinple calculator)

    I am trying to make a simple calculator that will take a value and perform an operator on the value multiple times for example

    result = 0.0
    +5
    result + 5.0 = 5.0
    new result = 5.0
    *2.2
    result * 2.2 = 11.0
    updated result = 11.0

    I have created a while with a conditional if statement. The if statement works fine the first cycle but after the fist cycle it defaults to the else statement.

    Can anyone please help me figure this out. Thanks much!





    Code:
    #include <stdio.h>
    
    double calculate(double currentVal, char operatorInput, double inputVal);
    
    int main(){
    	
    	int done = 0;
    	double newResult = 5;
    	double inputVal;
    	char operatorInput;
    	
    	printf("                    simple calculator                     \n");
    	printf("**********************************************************\n");
    	printf("Calculator is on.\n");
    	printf("result = %.1f\n", newResult);
    	
    	
    	
    	
    	
    	while (!done) {
    		printf("input agian\n");	
    		scanf("%c%lf", &operatorInput, &inputVal);
    	
    		if (operatorInput == '*' || operatorInput == '/' || operatorInput == '+' || operatorInput == '-') {
    			newResult = calculate(newResult, operatorInput, inputVal);
    			printf("result %c %.1lf = %.1lf\n", operatorInput, inputVal, newResult);
    			printf("new result = %.1f\n", newResult);
    			}
    			
    			else {
    				printf("UnknownOperatorException is thrown\n");
    				printf("Please re-enter input:\n");
    				}
    	}
    	return 0;
    }
    
    
    double calculate(double currentVal, char operatorInput, double inputVal){
    	// a (x) b = c
    	double a, b, c;
    	char x;
    	
    	a = currentVal;
    	x = operatorInput;
    	b = inputVal;
    	
    	if (x == '*'){
    		c = a * b;
    	}
    		else if(x == '/'){
    			c = a / b;
    		}
    			else if(x == '+'){
    					c = a + b;
    				}
    				else if(x == '-'){
    					c = a - b;
    				}
    	return c;
    }

  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    on line 24 after your Scanf you can add a getchar();
    I copy and pasted your code over to a new project added that line and everything seemed to work fine. I don't remember where I read it (I know it was an old post here), but I think getchar() is supposed to flush the buffer after the scanf. Hopefully someone esle will come along and explain WHY this fixes it a little better than I.
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    Thanks for the help, getchar() does clear the buffer and it did work, thanks for pointing me in the right direction.
    -c

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    The calculate() function may return a garbage value if the operator does not match one of '*', '/', '+', '-'. The variable c is not initialized. But you are checking whether the operator is one of those before you call the function so you're safe. It's just that a good compiler will give you a warning that the returned variable c is not initialized.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  2. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 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. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM