Menu

Help with restarting a program

2012-04-25
2012-09-26
  • vincent dominguez

    Hello today I wanted to make a simple calculator and understand how it works
    and here's what I got so far but I can't get it to restart itself when I press
    one key.

    The Code so far is :

    include <iostream>

    using namespace std;

    int main()
    {
    char rept;
    do
    {

    int a;

    int b;

    int sum;

    cin >> a;

    cin >> b;

    sum = a*b;

    cout << "Awnser: " << sum << endl;

    cout << "The answer is solved by Vincent Dominguez your awnser correct. "<<
    endl;

    cin >> rept;

    while (rept == 'y' || rept == 'Y');
    return 0;
    }

    Then I get a error at the } end.
    can someone tell me how to fix this and explain it to me thanks.

     
  • Aris

    Aris - 2012-04-28

    Just do what compiler says to you. You missed an }. you must to find out
    where.. Its really easy to find it. I'm not going to tell you... I just tell
    you that if you open an { some where you must close it with an }

    One trick to help you avoid this kind of errors is to write your code like
    this

    #include <iostream>
    #include <cstdlib> //windows OS
    
    using namespace std;
    
    int main(void)
    {
       int a, b, sum;
       cin >> a;
       cin >> b;
       sum = a + b;
    
       if(sum >= 0)
       {
          cout << "Sum>= 0" << endl;
       }
       else
       {
          cout << "Sum <  0" << endl;
       }
    
       system("pause");  //windows OS
       return 0;  
    }
    

    Its better to use mean full variables like: sum = a + b and product = a * b.
    In programs like this its not easy to notice how much useful is, but in a
    program of 2000 lines (a student program) is very useful.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.