-2

Made my first C++ program ever. A basic calculator. Got stuck at this part where I was suppose to make the program repeat.

So at "Point A" you select if you want to count division/addition etc. and it takes you there. When you run it once, it asks if you want to repeat the function (e.g division) or go back to the "Point A" (you just type y[yes] (repeats the division) or n[no](goes to "point A")). I'm new to C++, haven't got really familiar with loops yet. Also the code structures make my head spin, so Google didn't help me much. I've heard about the "goto" function (or whatever you call it) but I was told that i shouldn't be used in this case.

Take a look. The texts, and most of the comments are in Finnish, but I hope you'll get the point from English comments.

#include <iostream>
using namespace std;

float addition(float num1, float num2)
{
    return num1 + num2;
}
float substraction(float num1, float num2)
{
    return num1 - num2;
}
float multiplication(float num1, float num2)
{
    return num1 * num2;
}
float division(float num1, float num2)
{
    return num1 / num2;
}
//This function should throw you back to point 'A'
int valinta2{
    while (valinta2 == y){

    }
}


int main(void)
{
    //Point A
    float number1;
    float number2;
    int valinta;
    cout << "\n-*-*-*-*-*-*-*-*-*-*\nClaudion Laskin\n-*-*-*-*-*-*-*-*-*-*";
//Select what you want to count
    cout << "\n\n\nValitse mita haluat laskea.  \n\nVaihtoehdot: " << endl;
    cout << "1. Plus-laskut\n 2. Vahennys-laskut\n  3. Kerto-laskut\n   4. Jako-laskut \n\nValinta: ";
    cin >> valinta;

    if (valinta == 1){

        //Addition
        cout << "\n\n\n===============\n||Plus laskut||\n=============== \n\nSyota ensimmainen numero: ";
        cin >> number1;
        cout << "\n\n+\n\nSyota toinen numero: ";
        cin >> number2;
        cout << "\nTulos: " << addition(number1, number2) << "\n----------\n" << endl;
        cin.get();
        //if 'y' run the task again, if 'n' goto start
        cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\nKirjoita 'y' jos haluat jatkaa, 'n' jos haluat valikkoon\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n";
        cin.get();
    }

    else {
        if (valinta == 2){
            //Subtraction
            cout << "\n\n\n===================\n||Vahennys laskut||\n=================== \n\nSyota ensimmainen numero: ";
            cin >> number1;
            cout << "\n\n-\n\nSyota toinen numero: ";
            cin >> number2;
            cout << "\nTulos: " << substraction(number1, number2) << "\n----------\n" << endl;
            cin.get();
            //if 'y' run the task again, if 'n' goto start
            cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\nKirjoita 'y' jos haluat jatkaa, 'n' jos haluat valikkoon\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n";
            cout << "Valinta: ";
            cin >> valinta2;
        }
        else {
            if (valinta == 3){
                //Multiplication
                cout << "\n\n\n================\n||Kerto laskut||\n================ \n\nSyota ensimmainen numero: ";
                cin >> number1;
                cout << "\n\n*\n\nSyota toinen numero: ";
                cin >> number2;
                cout << "\nTulos: " << multiplication(number1, number2) << "\n----------\n" << endl;
                cin.get();
                //if 'y' run the task again, if 'n' goto start
                cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\nKirjoita 'y' jos haluat jatkaa, 'n' jos haluat valikkoon\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n";
                cin.get();
            }
            else {
                if (valinta == 4){
                    //Division
                    cout << "\n\n\n===============\n||Jako laskut||\n=============== \n\nSyota ensimmainen numero: ";
                    cin >> number1;
                    cout << "\n\n/\n\nSyota toinen numero: ";
                    cin >> number2;
                    cout << "\nTulos: " << division(number1, number2) << "\n----------\n" << endl;
                }
            }
        }
    }
    system("pause");
    return 0;
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
user2907241
  • 265
  • 2
  • 7
  • 15
  • Sorry, but I'm not going to look at this until the indentation is fixed, as a minimum. You should really parse it down to just the relevant code too... but at least fix the indentation since the question is about while loops and your brackets don't line up at all. – nhgrif Oct 22 '13 at 13:36
  • Actually... I don't see any loops in this code at all... – nhgrif Oct 22 '13 at 13:37

3 Answers3

2

You need to restructure your code to include the while loop in the main method. There would be ways to "go to" your "Point A", but they are messy and shouldn't be used (goto is the keyword here).

So at "Point A", insert

do {
    float number1;
    // ...

and then down at where you would like to call valinta2 (notice that you don't even call the function at the moment - I'm guessing it would have to be just before the call to system("pause")), do the check of the condition, like so:

} while (...);

And best revisit the chapter on flow control/loops in the C++ tutorial of your choice, e.g. one of those mentioned here: The Definitive C++ Book Guide and List

Community
  • 1
  • 1
codeling
  • 11,056
  • 4
  • 42
  • 71
1

Alternative to nyarlathotep's perfectly good answer, one method I like using follows this form:

while(true) {
    //do stuff
    //then when ready to check for exit condition
    if(exitCondition == true) { //the ==true part is redundant
        break;
    }
    //do more stuff
    //if you need to go to the beginning of the loop and 
    //skip any code following a point, do this:
    if(skipRestOfLoopCondition) {
        continue;
    }
    //you can always check other exit conditions or check 
    //the same one at multiple places
    if(someOtherExitCondition) {
        break;
    }
    //do even more stuff
}
system("pause");
return 0;

As a caveat, you must provide a way for at least one of the if's that result in break; to actually execute, because while(true) loop condition never gets you out of the loop, you can only get out of the loop with a break;.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
-1

I am pretty new to this and I don't know if I am right. I can hardly understand your code. However, when I am programming in c for the arduino and I want my code to loop forever I just put void loop() {// my code follows from here

Chad
  • 159
  • 1
  • 3
  • 15
  • 1
    That doesn't even make sense. Maybe `void loop() { //code` is something that works in Arduino, I don't know, I have no experience with Arduino, but all you're doing there in standard C++ is making a `void return` function called `loop` which doesn't actually guarantee anything will actually `loop`. – nhgrif Oct 22 '13 at 13:43