Question

 #include <iostream>


using namespace std;

int main()
{   int a;
    int b;
    int sum;
    string ans = "";
    cout << "Input a directive. Upon it finishing it will terminate. Codes are: \n Calc \n Exit \n";
    cin >> ans;
    if(ans == "Calc")
    {
        cout << "Welcome to Calculator! Put in a number. Press Enter to put in number. \n";
        cin >> a;

        cout << "Next number \n";
        cin >> b;
        sum = a + b;

        cout << sum << " Is the amount! \n";
        cout << "Input a directive. Upon it finishing it will terminate. Codes are: \n Calc \n Exit \n";
        ans = "";    
    }

    if(ans != "Calc")
    {    
        cout << "Okay";    
    }
}

This works, but say if I don't enter Calc, it does nothing, but print out, but if I don't input Calc again, it closes. and if I do put in calc, I can run through it, and when it gives answer, it is fine, then I press any key and it closes out. I am new to this forum/site, not sure if right place.

Was it helpful?

Solution

Well, if you want the program to take again input from the user, then you should write so that it does it:

#include <iostream>
using namespace std;
int main()
{   int a;
    int b;
    int sum;
    while(true) 
    {
         string ans = "";
         cout << "Input a directive. Upon it finishing it will terminate. Codes are: \n Calc \n Exit \n";
         cin >> ans;
         if(ans == "Calc")
         {
             cout << "Welcome to Calculator! Put in a number. Press Enter to put in number. \n";
             cin >> a;

             cout << "Next number \n";
             cin >> b;
              sum = a + b;

              cout << sum << " Is the amount! \n";
          }
          else if(ans == "Exit") 
          {
              cout << "Bye!\n";    
              return 0;
          }
          cout << "Okay\n";    
     }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top