loops

Hi, I think I need some help..I have wasted like 8 hours already just trying different lines of code..but since I don't really know what im doing wrong my program is still not running correctly.

Here is what I need to do:


Invent a survey question that has exactly three answers. (Example: "Do you prefer pizza, burritos or burgers?" But USE A DIFFERENT QUESTION THAN THIS FOR THE ASSIGNMENT.) Repeatedly ask the user to enter a "vote" for one of those three answers. Keep track of the total number of times each one was entered, until the user types "done". If they enter an invalid choice, print an error, but continue asking questions. At the end, report how many votes each answer received.

My questions:
1. Even if I enter "jazz" or " rap" or "rock" it still goes to this statement "Please choose one of the 3 options:1) rock, 2) jazz, 3) rap"
WHY?
2. if I enter "done" then it goes to the correct if statement and prints out what I want
3. I know in C++ you can do nested if statements and nested loops..
do they have to be like
if ()
{if()
{
}

}
basically if you are using "if" can you nest inside only "if"or it can be a nested loop inside of if statement or a while loop inside of for loop?
4. How would I count the ammount of times loop ran (but only for the correct answers, invalid answers should not be included and done should not be included)? I know I should be using running total most likely, but how would I take out the times when loop went through but answer was "done" or just "invalid answer"?

string usersanswer;
int i;
//i=amount of votes
int total=0;
cout<<"What kind of music do you like?\n";
cout<<"Please choose one of the 3 options:1) rock, 2) jazz, 3) rap\n";
cout<<"Please enter \"done\" to exit this program."<<endl;
usersanswer=GetLine();

while ((usersanswer!="done")&& (usersanswer!="jazz"||usersanswer!="rock"|| usersanswer!="rap"))

{ cout<<"Please choose one of the 3 options:1) rock, 2) jazz, 3) rap\n";
usersanswer=GetLine();
}
if (usersanswer=="done")
{
cout<<"Thank you for participating. Good bye!"<<endl;
}
else if (usersanswer=="jazz"||usersanswer=="rap"||usersanswer=="rock")
{

cout<<"Valid answer. You can choose again\n";
usersanswer=GetLine();
}
else if (usersanswer!="jazz"||usersanswer!="rock"||usersanswer!="rap")
{
cout<<"Invalid answer.Please choose one of the 3 options.\n";
usersanswer=GetLine();
}

1. If statements, if true, will run all the contents inside of it.
2. depends on your code
3. nested loops and stuff can be in any order any fashion as long as they have closing brackets {}
4. to count something create a static int and increase it in the loop with: counter++; this is outside the loop : int counter = 0;

You should just keep your while loop to while the user hasn't entered done yet, and then put your "thank you" outside of the loop. The way you have it now loops if the user hasn't entered done AND hasn't entered any keywords, if this is the case it will continue to loop. Yet if the user has entered done, they still haven't entered one of the keywords so it will fail because of the &&. As to test true both must be true.

To escape this loop you'd need useranswer to be more than one thing simultaneously, which is impossible.

To make coding easier on yourself don't think of loops as a place to bundle a bunch of tests. Think of the while loop as I want to go until ____. While conditions are rarely complicated, and usually a single test.

For the double &s think of it as: A && B as both A and B must be true for A && B to be true.
Last edited on


I got a little bit further and now it is running almost how I want it to run.
The only thing is with that counter. (BTW thanks for your answer oonej).

The only problem is my counter starts only from the inside of the loop (counts everything only AFTER the WHILE(USERSANSWER!="DONE") loop kicks in.

If in line 4, user enters "jazz" or "rock" or "rap" ..those are not counted. And I cannot initialize count variables to 1 because user might enter "ghjfhsk" or something else that is completely invalid.



string usersanswer;
int countjazz=0;
int countrap=0;
int countrock=0;
1. cout<<"What kind of music do you like?\n";
2. cout<<"Please choose one of the 3 options:1) rock, 2) jazz, 3) rap\n";
3. cout<<"Please enter \"done\" to exit this program."<<endl;
4. usersanswer=GetLine();
5. if (usersanswer=="done")
6. cout<<"Thank you for participating. You can try this program later.\n";


while (usersanswer!="done")
{
cout<<"Please choose one of the 3 options:1) rock, 2) jazz, 3) rap\n";
usersanswer=GetLine();
if (usersanswer=="jazz" && usersanswer!="rock" && usersanswer!="rap")
{
countjazz+=1;
}
else if (usersanswer=="rock"&& usersanswer!="jazz" && usersanswer!="rap")
{
countrock+=1;
}
else if (usersanswer=="rap"&& usersanswer!="jazz" && usersanswer!="rock")
{
countrap+=1;
}

while (usersanswer=="jazz"||usersanswer=="rap"||usersanswer=="rock")
{

cout<<"Valid answer. You can choose again.\n";
usersanswer=GetLine();
if (usersanswer=="jazz" && usersanswer!="rock" && usersanswer!="rap")
{
countjazz+=1;
}
else if (usersanswer=="rock"&& usersanswer!="jazz" && usersanswer!="rap")
{
countrock+=1;
}
else if (usersanswer=="rap"&& usersanswer!="jazz" && usersanswer!="rock")
{
countrap+=1;
}
}


if (usersanswer=="done")
{ cout<<"Thank you for participating. Good bye!"<<endl;
}

}
cout<<"You chose rock "<<countrock<<" times."<<endl;
cout<<"You chose rap "<<countrap<<" times."<<endl;
cout<<"You chose jazz "<<countjazz<<" times."<<endl;


}
Last edited on
Thank you, James. It took me only 4 hours, 3 cigaretttes and 5 candybars to figure out that the first line

while ((usersanswer!="done")&& (usersanswer!="jazz"||usersanswer!="rock"|| usersanswer!="rap"))
was wrong.
Now it is just that counter. Im thinking maybee I should use another separate counter after the line 6 and then sum it up with the counter in the loop. But it will be very bulky and I might have tried it already couple hours ago. Anyway, if anyone has any suggestions you are all welcome!
well, you can nested both the IF as well as loop according to the need.
"if" can be nested or ladder like
if{
if{}
}
IS nested,

if {
}
else if()
{}
is laddered

Nested "loop" is used to work on tables...


______________
http://www.igennie.net/microsoft-windows-vista-support.html
Topic archived. No new replies allowed.