Calculator loop problem

This is a discussion on Calculator loop problem within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to create a calculator program that only allows the user three attempts, my actul calculator program ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    11

    Calculator loop problem

    Hi, I am trying to create a calculator program that only allows the user three attempts, my actul calculator program works fine however i cant figure out where the while loop should go here is my attempt. When i compile it it runs through the calculator but stops after taken the second num!

    Code:
    #include <stdio.h>
    
    main()
    
    
    {
    
    
    int num1, num2, sum;
    
    
    int count=0;
    
    
    char operation,dummy;
    
    
    
    
    printf("Enter first number: "); /*retrieves first number from user */
    
    
    scanf("%d", &num1);
    
    
    printf("Enter operation (+ or - or * or /): "); /* retrieves operation from user */
    
    
    scanf("%c%c%c",&dummy,&operation,&dummy);
    
    
    printf("Enter second number: "); /*retrieves second number from user */
    
    
    scanf("%d", &num2);
    
    
    while(count<3)
    
    
    if (operation == '+'){
    	sum = num1 +  num2;
    	printf("The sum of %d and %d is %d", num1,num2, sum);
    	
    	} /* if operation is +, will add numbers and show answer */
    	
    if(operation == '-'){
    	sum = num1-num2;
    	printf("Subtracting %d from %d is %d", num2,num1,sum);
    	
    	}/* if operation is -, will subtract and show answer */
    	
    if (operation == '*'){	
    	sum = num1 * num2;
    	printf("Multiplying %d by %d is %d", num1, num2, sum);
    	
    	} /* if operation is *, will multiply and show answer */
    	
    if (operation == '/'){
    	sum = num1 / num2;
    	printf("Dividing %d by %d is %d", num1, num2, sum);
    	
    	}
    	/* if operation is /, will divide and show answer */
    
    
    	count=count+1;
    	
    return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,480
    Be explicit when you declare main. It's int main(void), not just main(). Read this link.

    The general format for a while loop is
    Code:
    while (some condition is true)
    {
    }
    Everything that you want repeated 3x needs to go in those curly braces. I always use curly braces when working with loops and if statements, even when I don't need them, so it's clear what I want repeated and what I don't. It's a helpful visual clue. Proper code formatting and indentation would also help you spot this problem, as you would see that only the if (operator == '+') part is currently inside the while. Many editors and IDEs offer an auto-indent feature to help with this.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    Thanks for the help and advice!quite stupidly i dont indent it properly until i have it finished!which defeats the purpose really!

    I have it pretty much working now but it prints the answer three times instead of three attempts!

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,480
    Quote Originally Posted by Laika1986 View Post
    I have it pretty much working now but it prints the answer three times instead of three attempts!
    Put everything, and I mean everything, that you want repeated inside the while loop. What isn't getting repeated that should be? Put that in the loop too.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You've already had a lot of advice on this in other threads, most of which you've chosen to ignore.

    Study this code until you understand how it works.
    Code:
    #include <stdio.h>
    
    int main(void)
      {
         int num1, num2, count=0;
         char operation,
    
         while(count<3)
            {
               printf("Enter <number> < + - / *> <number> : "); 
               scanf("%d %c %d", &num1, &operation, &num2);
     
               switch (operation)
                  { 
                     case '+' :        
                        printf("The sum of %d and %d is %d\n", num1, num2, num1 + num2);
                        break;
                     case '-' :	
                        printf("Subtracting %d from %d is %d\n", num2,num1, num1 - num2);
                        break;
                     case '*' :
                        printf("Multiplying %d by %d is %d\n", num1, num2, num1 * num 2);
                        break;
                     case '/' : 	
                        printf("Dividing %d by %d is %d\n", num1, num2, num1 / num2);
                        break ;
                     default :
                        printf("Invalid operation\n"); 
    	   }
               count=count+1;
            }
         return 0;
    }
    The good news is you can't turn this in, since it's published here and your teacher is likely to find it.
    Last edited by CommonTater; 10-22-2011 at 10:08 AM.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    My teacher would also think that I am about 4 lessons ahead so i do appreciate your input but it's irrelevant. Ill close the thread myself if possible

    It was one thread to be fair and how have i ignored it?
    Last edited by Laika1986; 10-22-2011 at 12:58 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. X^Y Calculator Using a For Loop
    By b0rn2fl2 in forum C Programming
    Replies: 23
    Last Post: 10-18-2011, 11:42 AM
  2. while loop (sinple calculator)
    By aweida in forum C Programming
    Replies: 3
    Last Post: 09-27-2010, 12:40 PM
  3. what is the problem on this RPN calculator?
    By blogchama in forum C Programming
    Replies: 2
    Last Post: 05-18-2010, 01:45 PM
  4. calculator problem
    By darkphenom in forum C Programming
    Replies: 4
    Last Post: 02-18-2010, 12:18 AM
  5. Calculator Problem
    By steven in forum C Programming
    Replies: 4
    Last Post: 09-18-2001, 08:07 AM

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