loop problem
Jul 20, 2011 at 10:13pm Jul 20, 2011 at 10:13pm UTC
ok, creekist code works, ends loop if a,b,c,d is entered and repeats if anything else is entered
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <iostream>
#include <string>
using namespace std;
int main(){
string answer;
//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;
while (true ){
if (cin >> answer){
if (answer == "a" )
{cout << "You go to pick up your pack of cigerettes, but they are no longer there." <<endl <<endl;
break ;}
else if (answer == "b" )
{cout << "You pick up the phone, but there is no dial tone." <<endl <<endl;
break ;}
else if (answer == "c" )
{cout << "You don't see a dead body anywhere." <<endl <<endl;
break ;}
else if (answer == "d" )
{cout << "You throw your bloody clothes in the garbage." <<endl <<endl;
break ;}
else
{cout << "That is not a correct answer, please try again." <<endl <<endl;}}}
//question2
cout << "question 2" ;
return 0;
}
but i don't understand , would'nt this say continue loop as long as 'answer' is true, but answer is what the user inputs?
1 2
while (true ){
if (cin >> answer){
Jul 20, 2011 at 10:14pm Jul 20, 2011 at 10:14pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <iostream>
#include <string>
using namespace std;
short question(short );
short question(short a){
string answer;
for (;;)
{
//question 1
cout << "question " << a << 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" || answer == "A" ){//||=Mentally Or
cout << "You go to pick up your pack of cigerettes, but they are no longer there." <<endl <<endl;
a++;
}
else if (answer == "b" || answer == "B" ){
cout << "You pick up the phone, but there is no dial tone." <<endl <<endl;
a++;
}
else if (answer == "c" || answer == "C" ){
cout << "You don't see a dead body anywhere." <<endl <<endl;
a++;
}
else if (answer == "d" ||answer == "D" ){
cout << "You throw your bloody clothes in the garbage." <<endl <<endl ;
a++;
}
else
cout << "That is not a correct answer, please try again." <<endl <<endl;
if (answer == "a" ,"b" ,"c" ,"d" )
{
break ;
}
}
//next question
return question(a);
}
int main(){
short a=1;
question(a);
}
I wrote this for u it's doing what u wanna do better i guess
it is using an
'a'
as
short
it will help u to pass next question and i add
||
it told compiler mentally or(and one more unnecessery information
&&
mentally and)
Jul 20, 2011 at 10:30pm Jul 20, 2011 at 10:30pm UTC
thanks everyone
Topic archived. No new replies allowed.