Thread: simple loop incrament question

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

    simple loop incrament question

    I have a simple calculator program, the program runs and I want it to say "calculator is on" but I only want it to say this "one time" at the beginning. I made a value count but it resets it self back to 0 at the beging of the loop when I return main() and start over. Can someone help me figure out what im doing wrong here.

    btw to get the program to reset you need to enter 'r' in the terminal followed by 'y'

    thanks so much for the help, my program seems pretty messy to me so if anyone has advice id love to here it.

    Code:
    #include <stdio.h>
    
    double calculate(double currentVal, char operatorInput, double inputVal);
    
    
    int main(){
    	int count = 0;
    	int done = 0;
    	double newResult = 0;
    	double inputVal;
    	char operatorInput;
    	char r, R;
    	char terminate;
    	char terminateB;
    	char terminateC;
    	
    	printf("%d\n", count);
    	if (count <= 0) {
    		printf("                    simple calculator                     \n");
    		printf("**********************************************************\n");
    		printf("Calculator is on.\n");
    		count++;
    		printf("%d\n", count);
    	}
    	
    	
    	printf("result = %.1f\n", newResult);
    	
    	while (!done) {
    		scanf("%c", &operatorInput);
    		if (operatorInput == 'r' || operatorInput == 'R') {
    			EOF;
    			printf("final result is %.1f\n", newResult);
    			printf("agian? [y/n]\n");
    			getchar();
    			scanf("%c", &terminate, &terminateB, &terminateC  );
    				if (terminate == 'y' || terminate == 'Y') {
    					getchar();
    					main();
    				}
    					else{
    						printf("end of program");
    						getchar();
    						return 0;
    			}
    		}
    		
    		
    		scanf("%lf", &inputVal);
    		getchar(); //flush buffer
    		
    		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("%c is an unknown operation\n", operatorInput );
    			printf("re-enter, your last line:\n");
    		}
    	}
    }
    
    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
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest reading up on loops and how the "break" command works.

    You also might want to read about how the return and exit commands work.

    Calling main() in a C program is a VERY BAD idea!!!!!

    Tim S.
    Last edited by stahta01; 09-27-2010 at 01:06 PM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    main() is the entry point for your program - don't call it. Instead, in your program, you have main(), main() can initialize anything your need done just once. (or it can call an initialize() function for that). Then it calls your menu program, which runs the program, gets user input, and calls any other subordinate functions, in a big do while loop.

    When the choice in the big do while loop is to quit, then menu() returns to main(), which then ends the program.

    main->menu->do calculations until user quits->then back to main->program ends.

    I have seen main() with just two lines of code:
    Code:
    int main() {
      menu();
      return 0;
    }
    You can call menu() as much as you want, but you don't need to call it more than once, because it's has a big do while loop, and it just loops around and around, running your calculator, see?

    P.S. You can have the big do while loop in main(), instead of menu(), but it's nice having a separate menu() function, when you want to extend the program.
    Last edited by Adak; 09-27-2010 at 01:13 PM.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Disregarding the wrongness of calling main directly, your approach would not have worked because each time you called main, a new instance of the function's variables (count included) would be pushed onto the stack and the new instance's count variable would always be initialized to 0 each time. Just because you call a function recursively does not mean that the variables in each instance are shared (that would mean potentially infinite recursion without stack overflow). Now, that said you could have gotten around that by making count a static variable but I would still not recommend that approach. Learn to use loops like stahta01 said.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

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

    thanks

    I appreciate the comments, it obvious that I am brand new to programing so the feedback is very helpful.

    I tried to implement some of the comments but I am still having some issues. I made one big while loop so I dont call main(). Im close but I cant get it to restart when I return 1. And my result value keeps re setting to 0.

    simple calculator
    ************************************************** ********
    Calculator is on.
    result = 0.0
    Running…
    +5
    result + 5.0 = 5.0
    new result = 5.0
    result = 0.0
    +5
    result + 5.0 = 5.0
    new result = 5.0
    result = 0.0
    r
    final result is 0.0
    agian? [y/n]
    y

    Debugger stopped.
    Program exited with status value:1.(gdb)

    My terminal is showing my variable 'result' is resting back to 0. I can see "program exited with status value 1" but that does not restart my loop. hmmmm

    Code:
    #include <stdio.h>
    
    double calculate(double currentVal, char operatorInput, double inputVal);
    
    
    int main(){
        
        printf("                    simple calculator                     \n");
        printf("**********************************************************\n");
        printf("Calculator is on.\n");
    
        
        
        int done = 0;
    
        while(!done){
        
            double newResult = 0;
            double inputVal;
            char operatorInput;
            char r, R;
            char terminate;
        
            printf("result = %.1f\n", newResult);
        
            scanf("%c", &operatorInput);
                if (operatorInput == 'r' || operatorInput == 'R') {
                    EOF;
                    printf("final result is %.1f\n", newResult);
                    printf("agian? [y/n]\n");
                    getchar();
                    scanf("%c", &terminate);
                        if (terminate == 'y' || terminate == 'Y') {
                            getchar();
                            return 1;
                }
                        else{
                            printf("end of program");
                            getchar();
                            return 0;
                }
            }
            
            
            scanf("%lf", &inputVal);
            getchar(); //flush buffer
            
            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("%c is an unknown operation\n", operatorInput );
                printf("re-enter, your last line:\n");
            }
        }
    }
    
    
    
    
    
    
    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;
    }

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    I finally figured it out....

    Code:
    		if (operatorInput == 'r' || operatorInput == 'R') {
    				EOF;
    				printf("final result is %.1f\n", newResult);
    				printf("agian? [y/n]\n");
    				getchar();
    				scanf("%c", &terminate);
    					if (terminate == 'y' || terminate == 'Y') {
    						getchar();
    						EOF;
    						next = 2;
    						newResult = 0;
    					}
    					else{
    						printf("end of program");
    						getchar();
    						return 0;
    					}
    		}
    		
    		
    		
    		if (next == 1){

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question about scope
    By s_siouris in forum C Programming
    Replies: 5
    Last Post: 09-14-2010, 01:03 PM
  2. Simple process synchronization question
    By AnTriber in forum C Programming
    Replies: 3
    Last Post: 09-10-2010, 05:51 PM
  3. While loop question.
    By darren78 in forum C++ Programming
    Replies: 8
    Last Post: 07-01-2010, 11:06 AM
  4. Trying to figure out a simple loop....
    By chadsxe in forum C++ Programming
    Replies: 9
    Last Post: 01-05-2006, 01:31 PM
  5. Simple Question from Stupid Person...
    By Monmouth3 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-02-2002, 08:47 AM