Why, after selecting the amount of the contribution, would suggest a new amount of contribution instead of a menu display?


  • QA Engineer

    In selecting 1 paragraph, the user is requested to show the current value of the contribution. When selecting two, insert a new amount of contributions accordingly. If you choose 3, you're leaving the program.

    The code I used:

    //Program writed with error when choose 1 after it progrum should output menu instead entering new sum of conribution
    #include <iostream>
    #include <string>
    

    using namespace std;

    int main() {
    int contribution;
    char choose;

    cout &lt;&lt; "Hello I don't know what I'm doing but still" &lt;&lt; endl;
    cout &lt;&lt; "Enter sum of contribution in bank:";
    cin &gt;&gt; contribution;
    cout &lt;&lt; endl;
    while (true) {
        system("cls");
        cout &lt;&lt; "What you what to do" &lt;&lt; endl;
        cout &lt;&lt; "1.Show sum of contribution" &lt;&lt; endl;
        cout &lt;&lt; "2.Change sum of contribution" &lt;&lt; endl;
        cout &lt;&lt; "3.Exit" &lt;&lt; endl &lt;&lt; endl;
        cin &gt;&gt; choose;
    
        switch (choose) {
        case '1':
            cout &lt;&lt; "Sum of cintribution:" &lt;&lt; contribution &lt;&lt; endl;
            system("pause");
        case '2':
            cout &lt;&lt; "Enter new sum of contribution:";
            cin &gt;&gt; contribution;
            cout &lt;&lt; "New sum of of contrubution is " &lt;&lt; contribution &lt;&lt; endl;
        case '3':
            cout &lt;&lt; "Have a good day!";
            break;
        default:
            cout &lt;&lt; "choose 1 2 or 3";
        }
    }
    

    }

    Why, after selecting the amount of the contribution, would suggest a new amount of contribution instead of a menu display?



  • because case We need to split through break Otherwise they'll be carried out one by one.

        case '1':
            cout << "Sum of cintribution:" << contribution << endl;
            system("pause");
            break;
        case '2':
            cout << "Enter new sum of contribution:";
            cin >> contribution;
            cout << "New sum of of contrubution is " << contribution << endl;
            break;
        case '3':
            cout << "Have a good day!";
            break;
        default:
    

    By the way, 3 There is no exit and at least there is a need to implement:

    while (true) {
        // ...
        bool isExit == false;
        switch (choose) {
        case '3':
            cout << "Have a good day!";
            isExit = true;
            break;
        default:
            cout << "choose 1 2 or 3";
        }
        if (isExit == true)
            break;
    }
    

Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2