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.