Click here to Skip to main content
14,694,465 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this program that is supposed to be a running calculator. I cannot get my code to work past the first calculation. I cannot figure out what is wrong with if. Any input?

What I have tried:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int calculate(int num1, char operator, int num2);

int main()
{
    int num1 = 0;
    int num2 = 0;
    int temp = 0;
    char operator;
    
    int count = scanf("%d %c %d", &num1, &operator, &num2);

    void clearBuffer(void)
    {
       while ((getchar()) != '\n')
           ;
    }

    int result = calculate(num1, operator, num2);
    printf("%d", result);
    temp = result;
      
    do
    {
        num1 = temp;
        int answer = 0;
        scanf("%c %d", &operator, &num2);
        void clearBuffer(void)
        {
            while( (getchar()) != '\n' )
                ; 
        }

        switch (operator)
        {
          case '+':
            answer = num1 + num2;
            break;
          case '-':
            answer = num1 - num2;
            break;
          case '*':
            answer = num1 * num2;
            break;
          case '/':
            answer = num1 / num2;
            break;
          case '%':
            answer = num1 % num2;
            break;
          default:
            printf("Thank you for using COP2220 Calculator!");
        }
        printf("%d", answer);
    } while(num1 >= 0);

    return 0;
}

int calculate(int num1, char operator, int num2)
{
    int answer = 0;
    switch (operator)
    {
    case '+':
        answer = num1 + num2;
        break;
    case '-':
        answer = num1 - num2;
        break;
    case '*':
        answer = num1 * num2;
        break;
    case '/':
        answer = num1 / num2;
        break;
    case '%':
        answer = num1 % num2;
        break;
    }

    return answer;
}
Posted
Updated 28-Sep-18 5:58am
v3
Comments
Rick York 28-Sep-18 11:02am
   
I didn't go through your code with a fine-toothed comb but I did adjust the spacing a little and added the pre tags to your post. I would look carefully at the clearBuffer function. I am surprised that even compiles in C actually. I would move that of the main function and make it a separate function, outside of any others. I would also revise your code so it always calls calculate to do its calculations. You have the code to do that in two places and that is something to avoid.
Richard MacCutchan 28-Sep-18 11:28am
   
Why are you doing the calculations in two places? I showed you what to do yesterday in your question at https://www.codeproject.com/Questions/1261653/Run-a-switch-for-characters-equals-in-a-while-loop[^].
Member 13999874 28-Sep-18 11:43am
   
This is just one of my many attempts to get it to compile and run. It does the same thing as when I just called the function again, but I haven’t switched it back yet. I just cannot figure out where it is going wrong. I’ve tried reaching out to my professor, but haven’t received any response.
Richard MacCutchan 28-Sep-18 12:12pm
   
I offered you a working solution yesterday, so why did you not use it?
Member 13999874 28-Sep-18 12:22pm
   
I could not get it to run correctly.
Richard MacCutchan 28-Sep-18 13:27pm
   
Then you should have replied to my message in that question with full details of what you tried, instead of opening a new question with different code.
Member 13999874 28-Sep-18 15:02pm
   
Sorry if I didn't follow the typical process. I am new to this site. I did try to reply, but it would not let me since I had accepted the solution. I do have a code now that compiles and runs thanks to all the help I got here. The only problem I am having is when an invalid operator, the code exits with my default followed by 0. I know this is because I have a return value of 0, but when I take it out, there is an error. Do you know how I might be able to fix this?
Richard MacCutchan 29-Sep-18 3:36am
   
Add a default: case to your switch statement. And if you want help with an error you need to explain what the error actually is.

Also it is always a good idea to read the documentation if you are not sure of something.
Rick York 28-Sep-18 11:54am
   
One very good reason to have the calculation code in only one place is it lets you see the flow of the code easier when it isn't so cluttered. Also, make the clearBuffer a function implemented once. Then you can easily comment it out and see what effect that has. Here is your code with the functions as I mentioned :
int calculate(int num1, char operator, int num2)
{
    int answer = 0;
    switch (operator)
    {
    case '+':
        answer = num1 + num2;
        break;
    case '-':
        answer = num1 - num2;
        break;
    case '*':
        answer = num1 * num2;
        break;
    case '/':
        answer = num1 / num2;
        break;
    case '%':
        answer = num1 % num2;
        break;
    }

    return answer;
}


void clearBuffer(void)
{
     while( getchar() != '\n' )
         ; 
}


int main()
{
    int num1 = 0;
    int num2 = 0;
    int temp = 0;
    char operator;
    
    int count = scanf("%d %c %d", &num1, &operator, &num2);

    clearBuffer();

    int result = calculate( num1, operator, num2 );
    printf( "result is %d\n", result );
    temp = result;
      
    do
    {
        num1 = temp;
        int answer = 0;
        scanf("%c %d", &operator, &num2);
        
        clearBuffer();
        
        answer = calculate( num1, operator, num2 );

        printf("answer is %d\n", answer);

    } while(num1 >= 0);

    return 0;
}

Isn't it much easier to see now? BTW - I write code upside-down with very few prototypes. That's just me.
Richard MacCutchan 28-Sep-18 12:11pm
   
Pretty much what I gave him yesterday, so no idea why he went off and did something different. BTW I'm with you on not using prototypes; one of the worst ideas ever.
Rick York 28-Sep-18 13:49pm
   
I think they make sense when code is in different modules but not for code in the same one.
Richard MacCutchan 29-Sep-18 3:32am
   
True, but only because you must define any external functions. The problem is that they have to be created in two places: the header and the implementation file. So it is much easier to make a simple mistake if one of them changes.
Rick York 30-Sep-18 12:52pm
   
Yes, I agree completely. An automatic prototype generator could be a nice tool, maybe a keyword-driven one or possibly by pragmas. I would probably write one if I didn't have so many projects going.
KarstenK 28-Sep-18 12:22pm
   
Use the debugger with setting a break point and figure it out.

1 solution

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900