1

im new to coding in general and was wondering how to make my little program loop, I can input 1 equation before it just doesn't do anything and was wondering how to fix that

#include<iostream>
using namespace std;
int main()
{
    for ( ; ; ) {
        cout << "INPUT 1 for div, INPUT 2 for mult THEN input 2 numbers";
        int sign;
        cin>>sign;
            if ( sign == 1 ) {
            int num1;
            cin>>num1;
            int num2;
            cin>>num2;
            cout << num1/num2;
        }
        else {
            int num1;
            cin>>num1;
            int num2;
            cin>>num2;
            cout << num1 * num2;
        }

        return 0;
    }
} 
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Merch4nt
  • 29
  • 2
  • 5
    I took the liberty of adding indentation to your code! With this it is quite clear that your `return 0` statement is ***inside*** the `for` loop - so that loop will only run once (the program will exit when the `return` statement is encountered). – Adrian Mole Dec 26 '19 at 16:23
  • 1
    … try moving that `return 0;` statement to *after* the next `}` character - then your loop will run 'forever'. To make the code better, change `else {` to `else if (sign == 2) {` and add another option (say `0`) to exit. – Adrian Mole Dec 26 '19 at 16:28
  • 4
    Please note that the title should _summarize the question_. A better one could have been: _Why does my loop only run once?_ – Jaideep Shekhar Dec 26 '19 at 16:28
  • 1
    The emphasize the first comment: the compiler couldn't care less about how you format your code (at least in c++: there are language where formatting is part of the syntax), but consistently using a good style and will help *you*. Do it. Everytime. And use an editing environment that helps you with that. – dmckee --- ex-moderator kitten Dec 26 '19 at 16:33
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5910058) – Jesper Juhl Dec 26 '19 at 16:38

2 Answers2

3

Welcome to coding :)

You have placed your return 0; statement inside your for-loop.

Return 0; will make your main-function (and in this case your entire program) finish and exit immediately, no matter where it is encountered inside the function.

You should move the return 0 statement outside the for loop like so:

int main()
{
...
    for ( ; ; ) {
        ...
            int num2;
            cin>>num2;
            cout << num1 * num2;
        }

    }

    return 0;
}

This way, the program should never hit the return statement and keep looping correctly.

2

You can simplify your program in several ways, first as @Kasper suggest, move your return statement, next you can avoid duplicating the cin>>num1 and cin>>num2 and third add an exit condition,

to summarize

#include<iostream>
using namespace std;
int main()
{
    for ( ; ; ) {
        cout << "INPUT 1 for div, INPUT 2 for mult THEN input 2 numbers";
        int sign;
        int num1;
        int num2;
        cin>>sign;
        cin>>num1;
        cin>>num2;
        if ( sign == 1 ) {
            cout << num1/num2;
        }
        else if ( sign == 2 ) {
            cout << num1 * num2;
        }
        else  {
            cout << "Neither 1 or 2 entered as sign, bye bye";
            return 0;
        } 
    }
} 

you should also consider what happens if the user enters 0 (zero) as num2, especially when sign = 1

MikNiller
  • 1,242
  • 11
  • 17