C++ "Vending Machine" task Task:


  Write a program that presents the user w/ a choice of your 5 favorite
  beverages (Coke, Water, Sprite, ... , Whatever). Then allow the user
  to choose a beverage by entering a number 1-5. Output which beverage
  they chose.
  
  ★ If you program uses if statements instead of a switch statement,
  modify it to use a switch statement. If instead your program uses a
  switch statement, modify it to use if/else-if statements.
  
  ★★ Modify the program so that if the user enters a choice other than
  1-5 then it will output "Error. choice was not valid, here is your
  money back."




#include &lt;iostream&gt;
using namespace std;

int main() {

int drink = 0;
string ifswitch;
string again = "y";


while( again == "y" || again == "Y")
{
    cout &lt;&lt; "Would you like to use if or switch statements?: ";
    cin &gt;&gt; ifswitch;
    cout &lt;&lt; endl;

    if(ifswitch == "switch" || ifswitch == "Switch")
    {
        cout &lt;&lt; "Now using switch statements..." &lt;&lt; endl &lt;&lt; endl;

        cout &lt;&lt; "Your choices of drinks are: " &lt;&lt; endl;
        cout &lt;&lt; "1 - Coke" &lt;&lt; endl;
        cout &lt;&lt; "2 - Sprite" &lt;&lt; endl;
        cout &lt;&lt; "3 - Water" &lt;&lt; endl;
        cout &lt;&lt; "4 - Orange Juice" &lt;&lt; endl;
        cout &lt;&lt; "5 - Apple Juice" &lt;&lt; endl;

        cout &lt;&lt; "What drink would you like (1/5): ";
        cin &gt;&gt; drink;
        cout &lt;&lt; endl;

        switch(drink)
        {
            case 1 :
                cout &lt;&lt; "You chose Coke." &lt;&lt; endl;
                break;
            case 2 :
                cout &lt;&lt; "You chose Sprite." &lt;&lt; endl;
                break;
            case 3 :
                cout &lt;&lt; "You chose Water." &lt;&lt; endl;
                break;
            case 4 :
                cout &lt;&lt; "You chose Orange Juice." &lt;&lt; endl;
                break;
            case 5 :
                cout &lt;&lt; "You chose Apple Juice." &lt;&lt; endl;
                break;  
            default :
                cout &lt;&lt; "Error. Choice was not valid, Here is your money back.";

        }   
    }
    else if(ifswitch == "if" || ifswitch == "If")
    {
        cout &lt;&lt; "Now using if statements..." &lt;&lt; endl &lt;&lt; endl;

        cout &lt;&lt; "Your choices of drinks are: " &lt;&lt; endl;
        cout &lt;&lt; "1 - Coke" &lt;&lt; endl;
        cout &lt;&lt; "2 - Sprite" &lt;&lt; endl;
        cout &lt;&lt; "3 - Water" &lt;&lt; endl;
        cout &lt;&lt; "4 - Orange Juice" &lt;&lt; endl;
        cout &lt;&lt; "5 - Apple Juice" &lt;&lt; endl;

        cout &lt;&lt; "What drink would you like (1/5): ";
        cin &gt;&gt; drink;
        cout &lt;&lt; endl;

        if(drink == 1)
            cout &lt;&lt; "You chose Coke." &lt;&lt; endl;
        else if(drink == 2)
            cout &lt;&lt; "You chose Sprite." &lt;&lt; endl;
        else if(drink == 3)
            cout &lt;&lt; "You chose Water." &lt;&lt; endl;
        else if(drink == 4)
            cout &lt;&lt; "You chose Orange Juice." &lt;&lt; endl;
        else if(drink == 5)
            cout &lt;&lt; "You chose Apple juice." &lt;&lt; endl;
        else
        cout &lt;&lt; "Error. Choice was not valid, Here is your money back.";        

    }   

    cout &lt;&lt; "Would you like to go again? (Y/N)" &lt;&lt; endl;
    cin &gt;&gt; again;
    cout &lt;&lt; endl;

}

    cout &lt;&lt; "Ending...";

}
Do not use namespace std;
Put your code into functions if possible. For example the Question for the user should be a single function.

void printOptions() {
     std::cout &lt;&lt; "Your choices of drinks are: " &lt;&lt; "\n";
     std::cout &lt;&lt; "1 - Coke" &lt;&lt; "\n";
     std::cout &lt;&lt; "2 - Sprite" &lt;&lt; "\n";
     std::cout &lt;&lt; "3 - Water" &lt;&lt; "\n";
     std::cout &lt;&lt; "4 - Orange Juice" &lt;&lt; "\n";
     std::cout &lt;&lt; "5 - Apple Juice" &lt;&lt; "\n";
     std::cout &lt;&lt; "What drink would you like (1/5): ";


}

Note that i use "\n" instead of std::endl as this doesnt flush the stream.
Use a dedicated input function

int chooseBeverage() {
    int beverage;
    std::cin &gt;&gt; beverage;
    return beverage;
}

Now define a function that takes the choice and give the beverage.

enum choice {
    NONE = 0;
    COKE,
    SPRITE,
    WATER,
    ORANGE_JUICE,
    APPLE_JUICE
};

void serveBeverageIf(choice beverage) {
     if(beverage == COKE)
        std::cout &lt;&lt; "You chose Coke." &lt;&lt; "\n";
     else if(beverage == SPRITE)
        std::cout &lt;&lt; "You chose Sprite." &lt;&lt; "\n";
     else if(beverage == WATER)
        std::cout &lt;&lt; "You chose Water." &lt;&lt; "\n";
     else if(beverage == ORANGE_JUICE)
        std::cout &lt;&lt; "You chose Orange Juice." &lt;&lt; "\n";
     else if(beverage == APPLE_JUICE)
        std::cout &lt;&lt; "You chose Apple juice." &lt;&lt; "\n";
     else
        std::cout &lt;&lt; "Error. choice was not valid, here is your money back.";
 }

 void serveBeverageSwitch(choice beverage) {
     switch(beverage)
     case COKE:
        std::cout &lt;&lt; "You chose Coke." &lt;&lt; "\n";
        break;
     case SPRITE:
        std::cout &lt;&lt; "You chose Sprite." &lt;&lt; "\n";
        break;
     case WATER:                
        std::cout &lt;&lt; "You chose Water." &lt;&lt; "\n";
        break;
     case ORANGE_JUICE:                
        std::cout &lt;&lt; "You chose Orange Juice." &lt;&lt; "\n";
        break;
     case APPLE_JUICE:                
        std::cout &lt;&lt; "You chose Apple juice." &lt;&lt; "\n";
        break;
     default:
        std::cout &lt;&lt; "Error. choice was not valid, here is your money back.";
        break;
     }
 }

What you could also do is use a third option utilizing a map.

void serveBeverageMap(choice beverage) {
map&lt;choice, std::string&gt; nameMap = {
    std::make_pair(COKE, "Coke"),
    std::make_pair(SPRITE, "Sprite"),
    std::make_pair(WATER, "Water"),
    std::make_pair(ORANGE_JUICE, "Orange juice"),
    std::make_pair(APPLE_JUICE, "Apple juice")            
};
if(nameMap.find(beverage) == nameMap.end()) {
   std::cout &lt;&lt; "Error. choice was not valid, here is your money back.";
} else { 
    std::cout &lt;&lt; You chose " &lt;&lt; nameMap[beverage] &lt;&lt; ".";
}
}