How to use a switch statement with int variable

May 16, 2013 at 10:25pm
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 May 16, 2013 at 10:25pm
May 16, 2013 at 10:29pm
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 May 16, 2013 at 10:30pm
May 16, 2013 at 10:29pm
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;
}
May 17, 2013 at 12:12am
What's the purpose of your if statement?
May 17, 2013 at 7:44pm
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?
May 17, 2013 at 7:46pm
And thanks everypne for the help!
May 17, 2013 at 7:59pm
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
May 17, 2013 at 9:29pm
Ohh, thats right, IF statements dont loop?

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

May 17, 2013 at 10:27pm
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;
    }
}
May 17, 2013 at 11:23pm
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 ) 
May 18, 2013 at 1:41am
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.