I need to know what is wrong with this code

Joined
May 4, 2023
Messages
1
Reaction score
0
1683189151680.png
 
Joined
Sep 4, 2022
Messages
128
Reaction score
16
hi !

you forget to close the 'switch' command.
use one more }

it's a 'syntax error' .



C++:
#include <iostream>

using namespace std;

int main()
{
    int answer = -1; // out of case value.

    while (cin >> answer) {

        switch (answer) {

            case 1:
            cout << "Expresso" << endl ;
            break;

        case 2:
            cout << "American" << endl ;
            break;

        case 3:
            cout << "Cappucino" << endl ;
            break;

        case 4:
            cout << "Latte" << endl ;
            break;

        default:
            return false; // quit the loop
            break;

        }
    }
}


or with 3 lib :


C++:
// 3 resources files needed : IOstream / Vector / string

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{

    int answer = -1; // out of case value.

    vector<string> product = {"Expresso","Americano","Capucino","Latte"};

         
    while (cin  >> answer) {

        if ( answer > 4 || answer < 1 ) { return false; }

        cout << product.at(answer-1) << endl; // to keep the good array index for 'product' : "answer -1"

    }
 
}
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top