im terrible at loops

I wrote this basic calculator program for school, and id like to loop it. its not required but i figure why not. I attempted a for look but its not working and i am now getting an incorrect answer in the math equation. heres the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

using namespace std;
int main()
{

    char op;
    int again = 1;
    double num1, num2, sum;

    cout << " Enter your first number: ";
    cin >> num1;
    cout << "\n Enter the operator: ";
    cin >> op;
    cout << "\n Enter your second number: ";
    cin >> num2;

    for (int again = 1; again <= sum; again++)

    if(op == '+')
        sum = num1 + num2;
    else if(op == '-')
        sum = num1 - num2;
    else if(op == '*')
        sum = num1 * num2;
    else if(op == '/')
        sum = num1 / num2;

    cout << "\n" << num1 << op << num2 << " = " << sum << endl;

    return 0;
}
In line 18, at first go through the loop: The variable 'sum' is being used without being initialized. Besides, the condition for for loop to end is weird.

Also, please use { } to enclose which lines exactly are expected to loop, because I bet you expect it differently than it actually is.
Last edited on
i want lines 11 to 29 i guess to continue... i should move line 18 to above 11 and im going to need more varibles i think as well to ask whether or not i want to go again or exit ?
What exactly are you expecting as an output? Work it out in your head each step that you want to do.

As for your for loop; I see a few issues to begin with: first of all, "again" is already initialized. You do not need to initialize it again in the for loop.

I'll walk you through how loops work:

A for loop works by taking the iterator (in your case, "int again = 1". This is where the loop will begin. Next, it looks at the boolean condition, yours is "again <= sum". As long as again is less than or equal to sum, it will continue to loop. If it is greater than sum, the loop will end. Next, it will run through the commands within the for loop. Something I noticed right away is that you have no conditions for your for loops except "if (op == "+")" Which as long as the sum is less than or equal to "again", it will keep doing "sum = num1 + num2.".

When your loop commands reach the end, it will increase "again" by 1 (again++).

Then the loop starts over with again now equal to 2, then 3, then 4, and so on.

Also, by the nature of how your code is going to work, I don't think a for loop is really the best path to take. If you do not want your sum to exceed some number, a while loop would be better.

while (again < sum)
{
do your stuff
}

I'm not exactly sure why your want to loop this anyway? If you're simply adding, subtracting, multiplying, or dividing your two numbers, you'd be best off just keeping to your if and else if statements.

The only situation in which I'd see a loop useful for what you're trying to do is if you want to error check. If the operator is not + - * or /, then prompt the user to re-enter the operator.

1
2
3
4
5
6
else
   while (op != "+" || op != "-" || op != "*" || op != "/")
   {
      cout << "Invalid operator.  Enter the operator: ";
      cin >> op;
    }


You'd have to also implement another while loop at the beginning of all those if and else if's. You'd be best actually error checking there.
Last edited on
thanks for the info kevin, i appreciate it .
Hope it helps, I had to wrap my head around loops for a while before I finally understood what they really did. Good luck!
well like i was saying for this assignment im not required to loop it , but i figured a basic calculator you can clear and put in another equation.... thought id try to do something like that rather then having to re run the program to input another equation.
Topic archived. No new replies allowed.