How to use a switch statement with int variable

So, I've just figured out about switch statements.

I am having problems on lines 14, 17. (no ERRORS, just how to execute)
I know, to use a case with a char you use,

case 'A':

correct?

if I want to use an int,
is it supposed to be, case '1': , case "1": , case 1: , case = 1: ?

thanks.


(this code is not finished yet, I am going to make a cola machine, and I wanted to try to use switch statements, to learn how to use them)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;


int main ()
{
    int pick;
    if(true)
    {
        cout << "\n1. PEPSI  $1.25\n2. SPRITE  $1.25\n3. ARIZONA TEA  $1.00\n4. WATER  $1.00\n\n"; //prices
        cin >> pick;
        switch(pick)
        {
            case '1':
                cout << "You picked PEPSI";
                break;
            case '2':
                cout << "You picked SPRITE";
                break;
        }
    }

}
Last edited on
case '1': is for switching on any integer type, though char is most common for this
case 1: is for switching on any integer type, char less common
case "1": is illegal syntax
case = 1: is illegal syntax

Note that '1' is not the same value as 1.

In this case, you want to use case 1:
Last edited on
Try using the cases with out the apostrophe.

1
2
3
4
5
6
7
8
9
switch(pick)
{
       case 1:
                cout << "You picked PEPSI";
                break;
       case 2:
                cout << "You picked SPRITE";
                break;
}
What's the purpose of your if statement?
I plan on making it capable of giving you change back depending on how much money you put in the machine.

Is there a better way of doing this without an if statement?
And thanks everypne for the help!
I meant what was the purpose of if( true ) that is the same as not even putting an if statement so yeah the better way would be no if statement
Ohh, thats right, IF statements dont loop?

Anyways, Im gong to try to finish the project now, and I will post it.

Here is my code, Ive included everything ive learned from the question so far. This is a simple program, is there any way to improve on it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
using namespace std;

int main ()
{
    //VARIABLES//
    int pick;
    float money = 2.00;//START MONEY
    float price[4] = { 1.25 , 1.00 , 0.99 , 0.75 };//PEPSI, SPRITE, JUICE, WATER


    //BUYING SYSTEM//
    cout <<"you have $"<< money <<" to spend on a drink."<<endl<<endl;//states how much money you have
    cout << "\n1. PEPSI     $" << price[0] << "\n2. SPRITE    $" << price[1]  << "\n3. JUICE     $" << price[2] << "\n4. WATER     $" << price[3]  <<  endl << endl; //prices

    repick://<---//if choice is unavailable, come back to here, and retry entry.///////

    //DRINK PICKER//
    cin >> pick;
    switch(pick)
    {
        case 1:
            cout << "\nVending PEPSI...  Finished!\nYour change is, $"<<money-price[0]<<endl;
            break;
        case 2:
            cout << "\nVending SPRITE...  Finished!\nYour change is, $"<<money-price[1]<<endl;
            break;
        case 3:
            cout << "\nVending JUICE...  Finished!\nYour change is, $"<<money-price[2]<<endl;
            break;
        case 4:
            cout << "\nVending WATER...  Finished!\nYour change is, $"<<money-price[3]<<endl;
            break;
        default:
            cout << "\nThis choice is unavailable, please pick again.\n";
            goto repick;
    }
}
Don't use goto. Instead use a do/while loop( since you will run the program a minimum of one time.) ex:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool runProgram = true;
do
{
    cin >> pick;
    switch( pick )
    {
    case 1:
        std::cout << "Pepsi" << std::endl;
        break;
     //do the rest of case
    default:
        std::cout << "Invalid choice." << std::endl;
        runProgram = false;
        break;
    }
} while( runProgram ); //that means do it while runProgram is true it is
//the same as while( runProgram == true ) 
http://www.cplusplus.com/doc/tutorial/control/

Last section on this page.

As you learn write down the notes in your own words and fill a notebook with condensed rules and syntax examples for reference. It will reinforce everything and be a handy reference at the same time.
Topic archived. No new replies allowed.