while loop w/ switch case defaults sends me to space

Hello everyone. I am new to this forum and this is my first post. (I know, redundant introduction.)
I'm a noob to code in general, this is week 4 of class for me and I cannot figure out how to get this to work. I'm most likely over complicating this whole project, but I've got this much put together and built, so, I'm gonna find the solution darn it!!!! I read the "Read this first" and I will say that the few friends I do have would look at me funny or just laugh if I asked them, I can't even figure out how to properly word it for any decent google or forum search returns. As you will see in the code below, I have a switch-case statement inside a while loop to count data as a type of survey prog. The problem is, when I have an opportunity for the user to re-enter their answer in the default section of the switch case, the counter doesn't count the vote. When I pull the cin>>selection; out of the default, when the user (me) enters a number that is not part of the set the whole thing just keeps going round and round and round and round and round and.... sorry, been staring at this thing for a while.

Anyone wanna offer a clue to a newbie??

#include <iostream>
#include <string>
using namespace std;

int main()
{
int selection=0, user=1, coffee=1, tea=1, coke=1, oj=0;//Defining the items to be counted.

string Thanks="Thank you for participating.";//Saving some time typing.


while (selection != -1)//Start of the loop

{
if (selection>=0&&selection<=4)
{
cout<<"Soft Drink Survey\n"
<<"\t1.\tCoffee\n"
<<"\t2.\tTea\n"
<<"\t3.\tCoke\n"
<<"\t4.\tOrange Juice\n\n";

cout<<"Please input the number associated with your favorite beverage.\n"
<<"You are user number "<<user<<".\n\n"<<endl;

cout<<"Enter -1 to exit program and "//No choice number to avoid user confusion.
<<"calculate totals.\n\n";
cin>>selection;
}

switch (selection)
{
case (1):
coffee++;
cout<<Thanks<<"\n\n";
user++;
break;
case(2):
tea++;
cout<<Thanks<<"\n\n";;
user++;
break;
case (3):
coke++;
cout<<Thanks<<"\n\n";
user++;
break;
case (4):
oj++;
cout<<Thanks<<"\n\n";
user++;
break;
case (-1):
cout<<"Exiting program and calculating results....\n";
break;
default:
cout<<"Please make a valid selection of 1, 2, 3 or 4 or "
<<"input -1 to end program.\n";
user++;
}

}
cout<<"The total number of people surveyed is "<<user;
cout<<" and the totals are as follows:\n"
<<"*******************************************************\n\n"
<<"Coffee "<<coffee<<endl
<<"Tea "<<tea<<endl
<<"Coke "<<coke<<endl
<<"Orange Juice "<<oj<<endl;
system ("pause");
return 0;
}
Last edited on
Sorry for the waste of forum space. I just changed the if statement before the survey question to !=-1 instead of the range and cleared it right up.
Topic archived. No new replies allowed.