loop problem

Pages: 12
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;
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;
	else if (answer == "b")
		cout << "You pick up the phone, but there is no dial tone."<<endl <<endl;
	else if (answer == "c")
		cout << "You don't see a dead body anywhere."<<endl <<endl;
	else if (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

any ideas?
Last edited on
This doesn't do what you think it does: if(answer == "a","b","c","d")
See here under "comma operator" and operator &&:
http://www.cplusplus.com/doc/tutorial/operators/
Like this:

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;
}


Break if the answer is correct, don't break if the answer is incorrect. I think it works, I may be wrong though. Good luck.
This doesn't do what you think it does: if(answer == "a","b","c","d")


It has been working but IDE gives warnings for right hand operand has no effect, but it seems to be doing what i want it to do

could i put? if(answer ==a || b || c || d)

or

if(answer ==a && b && c && d) but wouldn't this ring true only if all are answered
Last edited on
You might want to think about using the || (or) operator instead of a comma.
No Athar is right, what the comma operator does is evaluate a list of expressions and then returns the value of the last expression.

So I believe (could be wrong though) that answer == "a","b","c","d" is the equivalent of answer == "d" .

But I don't know.

Anyways try out my code, I think it works, lawl.

:)
while (true){
if (cin >> answer){

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
Last edited on
while (true) is equivalent to for (;;) (although the latter should be preferred for stylistic reasons).
However, the loop condition should be while (cin >> answer) {
Ah don't worry.

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.

Something like that.
i feel like i will never remember everything to code lol
Last edited on
while (cin >> answer) {
how does this contue the loop?
That is an excellent question... I have no idea.

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>
using namespace 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.
Last edited on
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" ...
Last edited on
should I attempt to get it to work the way i want PERIOD? and deal with different (more efficient ways) as I learn more.

Or should i jump into all this at once?
Cuz right now i have 4 different ways to run it with the same results , but i only understand 1 of them.
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)
Last edited on
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.
Last edited on
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
Well if you want to do it your way I suggest doing it like this:

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
#include <iostream>
#include <string>
using namespace 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;
	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;}


I think it works.

GOOD LUCK WITH YOUR GAME

Assuming you're making one.
Pages: 12