Program closes on If then statement

I'm wanting to take some programming classes at my college next semester, so I figured I would practice a bit before hand. I've never done any kind of programming before, so I thought it would be fun.

I've been trying to make this little text-rpg-like program just for fun and to practice various concepts, but these if then statements are giving me trouble.

I'm basically asking the user if the information is correct. If it is, the game will move on, if not, then it will return to the previous question so they may put in the correct answer... and then it will move on after that.

Right now, the code inside the if then statements are very bare, and I only put those there so I can test answering yes or no.

I really want to continue asking some more questions when it moves on eventually.

My problem, is that when putting either yes or no in as an answer... my program just closes down upon hitting enter. =/

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
#include <iostream>

using namespace std;

int main()
{
	 char play[30];
	 char name[40];
	 int age;
	 char city[30];
	 char answer;
	
	    cout << "Welcome to Bobby's stupid RPG game\n\nType 'play' to begin. ";
	    cin >> play;
		cout << "\n";
		
		cout << "Before you begin your quest, we must\nfirst know a few things about you...\n\nWhat is your name? ";
		cin >> name;
		cout << "\n";

		cout << "Your name will be " << name << ".\nIs this correct? (yes or no) ";
		cin >> answer;
		
		if ( answer == 'no' ) 

			cout << "Sorry." << endl;
		
		else if ( answer == 'yes' )

			cout << "Ok great.";

	 return 0;   

}
Last edited on
u forgot to declare the answer length

char answer[4];

u should declare it like that, always leave an extra character (no has 2, yes has 3, so the length is 4)

hope it helps
So I tried that change up at the top... but now the variable is getting an error.

error C2446: '==' : no conversion from 'int' to 'char *'\

error C2040: '==' : 'char [4]' differs in levels of indirection from 'int'

... on both lines 24 and 28.
Last edited on
mmm, i see

try "no" instead of 'no' and "yes" instead of 'yes'
Ok the error went away... but now we are back to the beginning. =)

As soon as I input yes or no and hit enter, command window just closes.
It's because as soon as it does what you told it to, it closes because it hits the return 0; Read the "My Console Window is Closing" topic at the top of this section.
But for instance, if I add more questions under the 'yes' answer (because if it's correct it will continue with more questions)..

.. it never reaches any of the questions (that require inputted answers), and will exit just the same.
try this, include the conio.h library, and instead of using return 0 use getche()
Instead of using char*, I would suggest upgrading to the C++ std::string, as it is a lot easier to use (assuming you can). Also, try using { } around the ifs/else ifs and see if that fixes your problem.
Ok, I did both the getche(), and tried putting in the brackets, and it fixed the closing problem.

But, now after entering in yes or no, the cursor goes to a new line but doesn't give me the statements inside the if's/if then'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
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
	 char play[30];
	 char name[40];
	 int age;
	 char city[30];
	 char answer[4];
	
	    cout << "Welcome to Bobby's stupid RPG game\n\nType 'play' to begin. ";
	    cin >> play;
		cout << "\n";
		
		cout << "Before you begin your quest, we must\nfirst know a few things about you...\n\nWhat is your name? ";
		cin >> name;
		cout << "\n";

		cout << "Your name will be " << name << ".\nIs this correct? (yes or no) ";
		cin >> answer;
		
		if ( answer == "no" ) {

			cout << "Sorry." << endl;
		}
		else if ( answer == "yes" ) {

			cout << "Ok great.";
		}
	 getche();   

}
u mean it doesnt show "sorry" or "ok great"?
Hmm I am guessing that it is because your char* has a bunch of \0s or garbage data in the rest of it, causing your if statements to be:

 
if("no<GARBAGEDATA>" == "no")


Which it doesn't...Try using std::strings.
Right... it doesn't show the cout's.


I'll try using the strings and see what happens.
@ firedraco. Great! Using strings worked well, the answers came up and everything!

Thanks guys, I appreciate it!
No problem. Good luck with your endeavors :)
guess I was late, I got the code working without strings :D

but slayer, a little detail that doesnt look good in ur program:

"Type 'play' to begin."

in that part, no matter what u type, the game continues, u may want to change it for something like "Press enter to continue" or use another string to compare the play variable
True that homie. I just realized that a bit ago... and I was too lazy to fix it; thanks for the tip. =)
anyway, i'm glad u got it right :D
Yeah, it has been stumping me for a few days now and I was trying to figure it out by way of google + error log.

I finally found this website, and decided to register, because I knew I would have future problems haha.
Topic archived. No new replies allowed.