Whats wrong with the "If" statement here?

Pages: 12
@cyzero

That's good, You got it a lot better, and it looks like you understood most of my code.

OK, here's the first thing that sticks out to me:
The while loops. A while loop can do many things, but what I had it doing, was executing a bit of code over and over While the condition was False. In other words, While Ans does NOT equal This, do This stuff over and over, until Ans does equal This. while (Ans != this){ do this } '!=' represents IsNotEqualTo . So it basically asks the user for the correct answer until he puts the right answer, then continues with the rest of the program. It executes a block of code while the condition is true, or false, depending on what you put in the condition... and it doesn't have to be a condition within the (), but that's not relevant here, you will learn about that later.

Your while's will only perform the actions while the condition is True, meaning only while Ans has the value of "play", or whatever. So it will never execute the block of code, because the string is empty until the user inputs to it, but that's within the loop, which is only executed when the strings got the correct word. So that's why that's not going to work... You are using them as you would an IF condition. It would work if you replaced while with if, and put cin << Ans; before the if, but then it wouldn't keep asking for the correct answer if it's wrong.

Does that make sense? Did you understand it?

So what you need to do is make while's condition (NOT TRUE). That way, While Ans IsNotEqualTo "play" (meaning, "play" hasn't been input as the answer), do { ThisStuff }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Output the paragraph (or question, i guess)
cout<< "\n" << "Before sarcasm, man made due with OPPOSITES...\n"
       << "Sarcasm has become increasingly popular due to figures in society, \n"
       << "The most recent notability being Hugh Laurie. \n"
       << "His WORK being not so much Inspirational as Entertaining. \n"
       << "Yet he still manages to captivate and hold. A True Ambassador for Sarcasm.\n"
// Prompt for Answer until "play" is input
while (Ans != "play") {    // While the answer isn't "play", repeat the following
cout << "Answer: ";
cin >> Ans;
}

// Once answer becomes "play", then do the stuff below.
//It "falls through" to the next code once the loop is broken by a true condition. 
Ok, here is your code, with the stuff i talked about. I commented on everything that I changed, and explained the what's and why's of it all.

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <cstdlib>
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

char play, PLAY, CorrectUser[100], Play, ans, user[100];
int iD, iD2;
string Ans, Ans2, Ans3, Ans4, Ans5, AnsX;

int main()
{
//Begining of the Chaos
	cout<< "ChAoS ReIgN \tV0.99 \tTHE ANSW7R LIES W7THIN" << "\n"
		<< "Please type in username to receive custom serial or Identify Yourself" << "\n";		//Username Insert point
		cin >> user;																				//Username Input point
			cout<< "If " << user << " is your username please Input 1 and press enter." << "\n"			//Username Display
				<< "If this is not your username please Input 0 and press enter." << "\n";
				cin >> iD;																			//Begining of Question-Is Input Correct?
				if(iD==1)goto Correct;
				else if(iD==0)goto Wrong;
				else if(iD==77)
				{

					cout<< "\nNothing is as it seems," << "\n"
						<< "Believe no one, Trust no one." << "\n"
						<< "What is 'Life'?" << "\n"
						<< "The answer lies Within the Game...." << "\n";

					// As mentioned before, we put the "question" outside the loop
					cout<< "\n" << "Before sarcasm, man made due with OPPOSITES...\n"
						<< "Sarcasm has become increasingly popular due to figures in society, \n"
						<< "The most recent notability being Hugh Laurie. \n"
						<< "His WORK being not so much Inspirational as Entertaining. \n"
						<< "Yet he still manages to captivate and hold. A True Ambassador for Sarcasm.\n";
					// Here is the loop, While Ans IS_NOT_EQUAL_TO "play", do this:
					while (Ans != "play") {
						cout << "Answer:";
							cin >> Ans;
					}
					// Once Ans IS_EQUAL_TO "play", do this
					// as i said before, the loop breaks once the condition is true and falls through to the next thing
					cout<< "\n" << "Life, A constant Revolution Through Time.\n"
						<< "From dusk till dawn, The light comes out only to hide again.\n"
						<< "'For the night is long, wont you come out and play?' \n"
						<< "The foolish will walk the alleys and face the consequences.\n";
						while(Ans2 != "no") {
							cout << "Answer:";
								cin >> Ans2;
						}

						cout<< "\n" << "106BC-43BC.\n"
							<< "In times of war, The law falls silent.\n"
							<< "Lots said is mis-understood, So was Cicero.\n"
							<< "Inter arma enim silent leges.\n"
							<< "Sometimes you musn't look past the actual words.\n";
						while(Ans3 != "silent enim leges inter arma") {
								getline(cin, Ans3);
						}

						cout << "Heres where i've reached.";
						/*
						 * Now it falls through and puts out the random number
						 * and says bye... because of the goto's
						 * It executes 'Correct', 'Finish' and 'END' with no discresion ...
						 * Bad stuff ...  lol, but I'll let you fix that.
						 *
						 * In programming terms, that's what we would call
						 * "ugly", "evil", and "downright dispicable" code.
						 * Just so ya know, lol.
						 *
						 * If you want it to stop here, then use a return, like:
						 * return 0; (0 usually means 'successful')
						 * OR
						 * return 1; (1 usually means 'not successful')
						 *
						 * In this case you would use 'returen 0;'
						*/
						cout<<"Press any key to exit ...";
						getch();
						return 0;
			}

//If the username is Correct the User lands here.
Correct:
					cout <<"\n" << "Your Serial number is:" << rand() << "-" << rand() << "-" << rand() << "-" << rand() << "\n";
					goto Finish;
//If the username is Wrong the User lands here.
Wrong:
					cout << "Please input correct username," << "\n";
						cin >> CorrectUser;
							cout<< "Is " << CorrectUser << " correct?" << "\n"			//Username Display
								<< "Input 1 for yes, 0 for no and Press enter." << "\n";
							cin >> iD2;													//Begining of Question-Is Input Correct?
									if(iD2==1)goto Correct;
									else if(iD2!=1)goto Wrong;

//If all goes according to plan then here's the finsh line.
Finish:
					cout <<"\n" << "Enjoy The Random Number :D";
										goto END;


END:
						cout << "\nBye";
						cout <<"\n";
_getch(); 		/******  What is the underscore ( _ ) for?? *******/
return 0;

}

// I'll let you fix the goto's, but it should work now.

/*
 * Also, you might consider re-doing all the variables...
 * You can re-use the same one over and over, as long as it's the same type.
 * For example, you can use 'ans' for all the cin's where you have 'Ans', 'ans', 'Ans1', 'Ans2', etc...
 * But if you want to input to an int or a char then you have to use a different var.
 */


// Oh by the way, You wanna see something cool? Run the program and
// when it asks if you put in the correct name, type a letter instead of a number.
// LoL!

/* That's why i suggest you replace the int with a string... you can do:
	 . . .
	string iD;
	 . . .
	if (iD == 'y') goto Correct;
	if (iD == 'n') goto Wrong;

Or you can do:

	while (iD != 'y') {
		ask for correct answer;
	}

	give out a not-so-random number; (it gives me the same number every time lol)

	getch();
	return 0;

 */
Last edited on
So, now it should compile just fine, and run pretty much as expected. I left most of the code alone, so it's still got a lot of work that needs to be done, lots of things that aren't quite right, but it works.

Hope that helps!
/****** What is the underscore ( _ ) for?? *******/
Thats what Visua studio recommended, when i left it as getch() it wouldnt compile. I'll make the changes when i get home. Thanks for clearing the while statement up for me.
http://gunsmokeandknitting.files.wordpress.com/2009/11/1893_edvard_munch_the_scream-wr400.jpg

LOL ... Although that is a very odd link to post, where did you find that?
It was the first result on Google.
I Managed to remove all goto's.
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <cstdlib>
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

char play, PLAY, CorrectUser[100], Play, ans, user[100];
int iD, iD2;
string Ans, Ans2, Ans3, Ans4, Ans5, AnsX;

int main()
{
//Begining of the Chaos
	cout<< "ChAoS ReIgN \tV0.99 \tTHE ANSW7R LIES W7THIN" << "\n"
		
		<< "Please type in username to receive custom serial or Identify Yourself" << "\n";				
		cin >> user;																					
			cout<< "If " << user << " is your username please Input 1 and press enter." << "\n"			
				<< "If this is not your username please Input 0 and press enter." << "\n";
				cin >> iD;																				
				
				if(iD==1)
				{
						cout <<"\n" << "Your Serial number is:" << rand() << "-" << rand() << "-" << rand() << "-" << rand() << "\n";				
						cout <<"\n" << "Enjoy The Random Number :D";
				}

				else if(iD==0)
				{
					while(iD2==0)
					{
						cout << "Please input correct username," << "\n";
							cin >> CorrectUser;
								cout<< "Is " << CorrectUser << " correct?" << "\n"					
									<< "Input 1 for yes, 0 for no and Press enter." << "\n";	
									cin >> iD2;														
										cout <<"\n" << "Your Serial number is:" << rand() << "-" << rand() << "-" << rand() << "-" << rand() << "\n";				
										cout <<"\n" << "Enjoy The Random Number :D";
					}
				}

				else if(iD==77)	
				{	
	
					cout<< "\nNothing is as it seems," << "\n"
						<< "Believe no one, Trust no one." << "\n"
						<< "What is 'Life'?" << "\n"
						<< "The answer lies Within the Game...." << "\n";			
						
						while(Ans!="play") {
						cout<< "\n" << "Before sarcasm, man made due with OPPOSITES...\n"
							<< "Sarcasm has become increasingly popular due to figures in society, \n"
							<< "The most recent notability being Hugh Laurie. \n"
							<< "His WORK being not so much Inspirational as Entertaining. \n"
							<< "Yet he still manages to captivate and hold. A True Ambassador for Sarcasm.\n"
							<< "Answer:"; 
								cin >> Ans;
						}
						while(Ans2!="no") {
						cout<< "\n" << "Life, A constant Revolution Through Time.\n"
							<< "From dusk till dawn, The light comes out only to hide again.\n"
							<< "'For the night is long, wont you come out and play?' \n"
							<< "The foolish will walk the alleys and face the consequences.\n"
							<< "Answer:";
								cin >> Ans2;
						}
						while(Ans3!= "silent enim leges inter arma") {	
						cout<< "\n" << "106BC-43BC.\n"
							<< "In times of war, The law falls silent.\n"
							<< "Lots said is mis-understood, So was Cicero.\n"
							<< "Inter arma enim silent leges.\n"
							<< "Sometimes you musn't look past the actual words.\n"
							<< "Answer:";
								getline(cin, Ans3);
						}

						cout << "Heres where i've reached.";
				}
					
						cout << "\nBye";
cout <<"\n";
cout <<"Press any key to exit";
_getch(); //View Progress
return 0;

}



the problem im having now is that in the third question it displays the paragraph twice before letting me answer, in any case everything works, even the last paragraph( if i answer it after it displays twice it still runs as it should-going to the next question(if there was one.))..

Any ideas on that.
And thanks so much, i do think i understand while, if and code blocks better now :).
It's because you're leaving newline('\n') characters in the input stream. You should be using this line of code after every use of std::cin: std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Topic archived. No new replies allowed.
Pages: 12