Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Restarting/Closing a Program in C++

So I'm EXTREMELY new to C++, and have just started learning it about 2 days ago.
I'm trying to make a very basic calculator, and this is what I've got.

Code
#include <iostream>
using namespace std;

int main()
{
    int firstnumber;
    int secondnumber;
    string check;
    string operation;

    cout << "Enter your first number: \n";
    cin >> firstnumber;

    cout << "Enter your second number: \n";
    cin >> secondnumber;

    cout << "NOTE: Your numbers will not show decimals, because they will be rounded. \n";

    cout << "Enter your operation. Enter +,-,*, or /. \n";
    cin >> operation;
    if(operation == "+"){
        cout << firstnumber << " added with " << secondnumber << " is equal to " << (firstnumber + secondnumber) << ". \n";
    }
    else if(operation == "-"){
        cout << secondnumber << " subtracted from " << firstnumber << " is equal to " << (firstnumber - secondnumber) << ". \n";
    }
    else if(operation == "*"){
        cout << firstnumber << " multiplied with " << secondnumber << " is equal to " << (firstnumber * secondnumber) << ". \n";
    }
    else if(operation == "/"){
            cout << firstnumber << " divided by " << secondnumber << " is equal to " << (firstnumber / secondnumber) << ". \n";
    }
    else if(operation != "+" or "-" or "*" or "/"){
        cout << "You have entered an unrecognized operation. \n";
    };
    cout << "Would you like to complete another calculation? [yes or no.] \n";
    cin >> check;
    if(check == "yes"){
        cout << "You picked yes. \n";
        [b]RESTART PROGRAM[/b]
    }
    else if(check == "no"){
        cout << "You picked no. \n";
        [b]CLOSE PROGRAM[/b]
    }
    else if (check != "yes" or "no"){
        cout << "You have entered something unrecognized. \n";
    }

}

Any help for this?
How about instead of "restarting" the program, you just loop the code that you have? Investigate a while loop, or probably even better depending on how you implement it... A do-while loop.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
Loop the entire calc operations in a do-while loop. Also instead of several else if conditions, try using a switch statement instead.
I agree with Canonsx130, better using switch statement, also try using different variables.

Code
double a, b, answer;
char operation;

    std::cin >> a >> operation >> b;
    switch (operation)
    {
        case '-':
            answer = a - b;
            std::cout << "answer: " << answer << std::endl;
            break;
        case '+':
            answer = a + b;
            std::cout << "answer: " << answer << std::endl;
            break;
        case '*':
            answer = a * b;
            std::cout << "answer: " << answer << std::endl;
            break;
        case '/':
            answer = a / b;
            std::cout << "answer: " << answer << std::endl;
            break;
    }
Simply:
1. Create a boolean(bool) variable on the top of the main function
Code
int main(){
    bool continue_looping = true;
    //code...
    return 0;
}

2. Enter all the other code in a while loop, which will continue until the variable continue_looping is set equal to false

Code
int main(){
    bool continue_looping = true;
    while(continue_looping){
        //code..
    }
    return 0;
}

3. if the user don't want to another calculation, set continue_looping to false, else do nothing:
Code
int main(){
   bool continue_looping = true;
   while(continue_looping){
        //code..
        cout << "Would you like to complete another calculation? [yes or no.] \n";
        cin >> check;
        if(check == "yes"){
            cout << "You picked yes. \n";
        }
        else if(check == "no"){
            cout << "You picked no. \n";
            continue_looping = false;
        }
        else if (check != "yes" || check != "no"){
            cout << "You have entered something unrecognized. \n";
        }
   }
   return 0;
}

my recommendation: If the user enters something unrecognized, ask him for input again, you can do this by another while loop, before asking him for input, which will loop until you break it, you can do the same when you're asking about the operator. here is the code:
Code
int main(){
   bool continue_looping = true;
   while(continue_looping){
        //code..
        while(true){
            cout << "Would you like to complete another calculation? [yes or no.] \n";
            cin >> check;
            if(check == "yes"){
                cout << "You picked yes. \n";
                break;
            }
            else if(check == "no"){
                cout << "You picked no. \n";
                continue_looping = false;
                break;
            }
            else if (check != "yes" || check != "no"){
                cout << "You have entered something unrecognized. \n";
            }
        }
   }
   return 0;
}
Here is the total code
Code
int main()
{
    bool continue_looping = true;
    while(continue_looping){
      int firstnumber;
      int secondnumber;
      string check;
      string operation;

      cout << "Enter your first number: \n";
      cin >> firstnumber;

      cout << "Enter your second number: \n";
      cin >> secondnumber;

      cout << "NOTE: Your numbers will not show decimals, because they will be rounded. \n";

      while(true){
      cout << "Enter your operation. Enter +,-,*, or /. \n";
      cin >> operation;
      if(operation == "+"){
            cout << firstnumber << " added with " << secondnumber << " is equal to " << (firstnumber + secondnumber) << ". \n";
            break;
      }
      else if(operation == "-"){
            cout << secondnumber << " subtracted from " << firstnumber << " is equal to " << (firstnumber - secondnumber) << ". \n";
            break;
      }
      else if(operation == "*"){
            cout << firstnumber << " multiplied with " << secondnumber << " is equal to " << (firstnumber * secondnumber) << ". \n";
            break;
      }
      else if(operation == "/"){
      cout << firstnumber << " divided by " << secondnumber << " is equal to " << (firstnumber / secondnumber) << ". \n";
      break;
      }
      else if(operation != "+" || "-" || "*" || "/"){
            cout << "You have entered an unrecognized operation. \n";
      }
      }
      while(true){
      cout << "Would you like to complete another calculation? [yes or no.] \n";
      cin >> check;
      if(check == "yes"){
      cout << "You picked yes. \n";
      break;
      }
      else if(check == "no"){
            cout << "You picked no. \n";
            continue_looping = false;
            break;
      }
      else if (check != "yes" or "no"){
            cout << "You have entered something unrecognized. \n";
      }
      }
    }
    return 0;
}

Exercise: If you want to you can do this exercise. Write check for the other inputs(the numbers) and check for illegal arithmetic operations(for example division by zero).
Hint: cin.fail()


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by XerifApr 2, 2014 04:11 AM 9 309 Apr 2, 2014 04:11 AM
Last Post: Nichole
[Python] by W R TJan 13, 2014 08:57 PM 10 239 Jan 13, 2014 08:57 PM
Last Post: Tirus
[.NET] by Moon RockDec 10, 2013 05:01 PM 1 72 Dec 10, 2013 05:01 PM
Last Post: Moon Rock