#include <iostream>
#include <string>
usingnamespace std;
int main(){
string answer;
for(;;)
{
//question 1
cout << "question 1" << endl << endl;
cout << "Do you:" << endl;
cout << "A) answer1" << endl;
cout << "B) answer2" << endl;
cout << "C) answer3" << endl;
cout << "D) answer4" <<endl <<endl;
cin >> answer;
if (answer == "a")
cout << "You go to pick up your pack of cigerettes, but they are no longer there." <<endl <<endl;
elseif (answer == "b")
cout << "You pick up the phone, but there is no dial tone."<<endl <<endl;
elseif (answer == "c")
cout << "You don't see a dead body anywhere."<<endl <<endl;
elseif (answer == "d")
cout << "You throw your bloody clothes in the garbage."<<endl <<endl ;
else
cout << "That is not a correct answer, please try again."<<endl <<endl;
if(answer == "a","b","c","d")
{
break;
}
}
//question2
cout << "question 2";
return 0;
}
i am trying to figure out how to loop back to question 1 if anything other than a,b,c,d is entered AND break loop if a,b,c,d is enter and move one to question 2 I modified answer 1-4 so you don't think i am crazy lol
Pretty much the setup is it gives question 1, lists possible answers, input one of the multiple choice answers, if input is not one of the choices it loops back to the same question, input onechoice it gives and it gives a response to your input ,then it proceeds to question 2 and repeats all over again
is this pretty much meaning loop if true? I'm not sure what this means? It works, I would just like to know why it does.
I might just be reading too much into it
while (true) is equivalent to for (;;) (although the latter should be preferred for stylistic reasons).
However, the loop condition should be while (cin >> answer) {
Basically how it works is the while condition is set to true. So it loops forever.
But the body of code inside the while loop is only ever executed if (cin >> answer) meaning if the user inputs something and presses enter. The stuff user enters, enters the variable answer and the condition is fulfilled, then the body of code is executed.
Thankfully if the user enters an incorrect answer (not a b c d) the if statement ends, but it can be executed again because we're in an infinite while loop. The only way to break out of the infinite while loop is to enter a correct answer (a b c d). Which then has a break statement, which breaks out of the while loop.
It seems to work though. Athar how does the condition (cin >> answer) ever become false? Is there a way to close the input stream?
Seems to work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include<iostream>
usingnamespace std;
int main() {
int answer = 0;
cout << "Enter thy answer, either a 1, 0 or 2." << endl;
while (cin >> answer) {
if (answer == 2) {
cout << "Num nuts." << endl;}
if (answer == 1) {
cout << "Lawl." << endl;}
if (answer == 0) {
cout << "Lol." << endl;}}
return 0;}
Oh I got it, the while condition becomes false when what the user enters is something that is not an integer (only in the above example), because in the above example answer is a variable of type integer, so if you try putting a character like 'a' it won't cin into answer, so the condition fails and the next body of code is executed, I think.
ok so i'm trying to figure out multiple ways to get this to work right and try to figure out why those ways work right.
so far creekist reply worked
and my original one worked but gave warnings
then i used my original one with if(answer == "a"||"b"||"c"||"d") and it worked without errors or warnings
technically i'm probably using my original forum post with the change of the comma to the the or operator....just because i understand where control is going from and to. I am very new and this whole thing and it is a self test... to see what i've read in numerous books to see if i can remember/use what i learned.
but what way should i go? which way would run program faster? Should i stick with a less efficient way (but something i know how it works) until i get farther into it Or should i always do the most efficient regardless.???
It seems to work though. Athar how does the condition (cin >> answer) ever become false? Is there a way to close the input stream?
Yes, by entering EOF (Ctrl+D or Ctrl+Z). However, I assumed you knew this, because you also used this conversion in: if (cin >> answer)
Edit: if(answer == "a"||"b"||"c"||"d") won't do what you want either. && and || expect two bool values - but "a", "b", etc. are always converted to true.
It should be if (answer=="a" || answer=="b" ...
Your previous variants aren't correct, so there aren't 4 different ways.
Both if(answer == "a","b","c","d") and if(answer == "a"||"b"||"c"||"d") are equivalent to if (true)
I suggest you research anything you don't understand, look up examples using whatever you don't understand and if you're still sketchy then stick with what you know for now and optimize it later.
Your previous variants aren't correct, so there aren't 4 different ways.
Both if(answer == "a","b","c","d") and if(answer == "a"||"b"||"c"||"d") are equivalent to if (true)
yeah your right, i wasn't checking both breaking the loop and continuing loop