1

Hi I'm a new programmer just learning the basics of C++. I have created a very small section of code simply to practice the syntax of C++ and reinforce certain concepts. The program is a vending machine replica using if, else if and else statements to read the user input and display what they have choosen.

But for some reason my entire block of If/Elses is being ignored. When I've tried the debugger stopping at any section of it the program simply doesn't stop and when I've put the debugger somewhere else and tried to insert the red stop dot into the if statements I get a message saying "no executable code is associated with this line."

Here is my code

#include <iostream>
using namespace std;

int main ()
{
//Greets the user
cout << "Welcome to the cola machine" << endl;
cout << "Please choose your beverage from the list below\n\n";

//Lists the options for the user
cout << "1 - Coca-Cola" << endl;
cout << "2 - A & W Root Beer" << endl;
cout << "3 - Sprite" << endl;
cout << "4 - Pepsi" << endl;
cout << "5 - Mountain Dew" << endl;

int pop;
cin >> pop;

if (pop == 1)
{
    "\nCoca-Cola";
}
else if (pop == 2)
{
    "\nA & W Root Beer";
}
else if (pop == 3)
{
    "\nSprite";
}
else if (pop == 4)
{
    "\nPepsi";
}
else if (pop == 5)
{
    "\nMountain Dew";
}
else
{
    "\nError. choice was not valid, here is your money back.";
}

//Pauses program at end because I'm lazy
system("pause");
return 0;
}

So yeah any help or suggestions would be appreciated.

P.S. Sorry if I made any mistakes on this post it's my first of what will hopefully be many posts. Any constructive criticism on how I made my post or even how I'm self-teaching my coding would be appreciated.

HaloAngel
  • 13
  • 3
  • You don't need to `system("pause")` search for `getchar()` – The Mask Mar 26 '14 at 02:27
  • Once you become more familiar with C++ constructs, consider using a switch...case statement instead of if-else. See [this](http://stackoverflow.com/questions/449273/why-the-switch-statement-and-not-if-else) discussion as to why switch...case is a better alternative. – DigitalEye Mar 26 '14 at 02:45
  • Thank you both for the criticism. The next step in the practice sheet I'm using is actually to use a switch/case instead. – HaloAngel Mar 30 '14 at 13:46

1 Answers1

3
"\nCoca-Cola";

should be

cout << "\nCoca-Cola";

the same for the other lines.

Currently your if\else get executed but you simply don't do anything (at least nothing useful in them). You got string literals in your code that you simply don't do anything with.

AliciaBytes
  • 7,300
  • 6
  • 36
  • 47