[c++] help please input error

Started by
4 comments, last by daviangel 14 years, 10 months ago
hi guys i need help i was wondering why this code doesn't work basically after entering the name it doesn't accept any input from that point on and just goes straight to termination.


#include <iostream>
using namespace std;


int main()
{	
char quit_choice;
	do {
	int choice;
	
	char name;
	cout << " to start this adventure please type in your name ";
	cin >> name;
	cout << endl;
	cout << " hello " << name;

	cout << " you are a warrior and a boar attacks you ";
	cout << endl << " what do you do? " << endl
		<< " 1) accurate lunge" << endl
		<< " 2) crushing blow" << endl
		<< " 3) quick blow" << endl;
	
	(cin >> choice).get();

	if (choice == 1)
	{	
		cout << " you go for the accurate lunge, and manage to skewer the boar ";
		cout << endl;
		cout << " congratulations you have defeated the boar! ";
	}
	else if (choice == 2)
	{	
		cout << " you go for the crushing blow and miss! ";
		cout << endl;
		cout << " the boar kills you! ";
		cout << " better luck next time " << name;

	}
	else
	{	
		cout << " you go for the quick lunge and luckily you manage to kill the boar! ";
		cout << endl;
		cout << " congratulations you have defeated the boar! ";

	}

	cout << endl;
cout << " would you like to play again ? <Y/N> ";

cin >> quit_choice;

if (quit_choice == 'y' || quit_choice == 'Y')
{	
	cout << " hope to see you again soon! ";
	
}
else if (quit_choice == 'n' || quit_choice == 'N')
{	
	cout << " Nothing else to do so will quit anyways! ";



}
else 
{	
	cout << " you have not entered a valid option -- program will now quit! ";



}
	}while (quit_choice == 'n' || quit_choice == 'N');

	
	cin.get();
	cin.get();
	return 0;

}

please help me guys i know you are all very kind :D

Game Development Tutorials - My new site that tries to teach LWJGL 3.0 and OpenGL to anyone willing to learn a little.

Advertisement
never mind it works it was just that there was a null character in between the name i used. sorry

Game Development Tutorials - My new site that tries to teach LWJGL 3.0 and OpenGL to anyone willing to learn a little.

You probably want to support names longer than a single character. Use std::string for text in C++ (you will need to #include <string>). If you want people to be able to enter names with spaces, you can use the free function std::getline(std::cin, name) rather than operator >> (again, name will be a std::string instance).

Other than that, I would point out that this line seems suspicous:
(cin >> choice).get();

You can replace it with just:
cin >> choice;

If you still want an artificial pause, better to do that on a separate line. This makes your code clearer.

You can use the character literal '\n' rather than std::endl when you want a newline to appear in your program output. The difference is that std::endl flushes the buffers, which is usually unnecessary.

At the very end of your program, you only need to do cin.get() once for a pause.
i went with

#include <iostream>using namespace std;int main(){	char quit_choice;			// outside game loop or else quit_choice not recognised	do {					// game loop hopefully	int choice;		char name[20];	cout << " to start this adventure please type in your name ";	cin.getline(name, 20);	cout << "\n";	cout << " hello " << name;		// add a bit of depth to game by including personal names	cout << " you are a warrior and a boar attacks you ";	cout << endl << " what do you do? \n " 		<< " 1) accurate lunge \n" << 		<< " 2) crushing blow \n" << 		<< " 3) quick blow  \n" << ;		cin >> choice;				// used this to vary my input methods	if (choice == 1)	{			cout << " you go for the accurate lunge, and manage to skewer the boar ";		cout << "\n";		cout << " congratulations you have defeated the boar! ";	}	else if (choice == 2)	{			cout << " you go for the crushing blow and miss! ";		cout << "\n";		cout << " the boar kills you! ";		cout << " better luck next time " << name;	}	else	{			cout << " you go for the quick lunge and luckily you manage to kill the boar! ";		cout << " \n";		cout << " congratulations you have defeated the boar! ";	}	cout << "\n";cout << " would you like to play again ? <Y/N> ";cin >> quit_choice;			// simpe do you want to quit with play again option	}while (quit_choice == 'y' || quit_choice == 'Y');					// end of loop		cin.get();		// added to keep program running at finish	return 0;}


and thanks for the input :D much appreciated

Game Development Tutorials - My new site that tries to teach LWJGL 3.0 and OpenGL to anyone willing to learn a little.

What if my name is longer than 19 characters? Use std::string, rather than become infected with the C Programmer's Disease.
Quote:Original post by rip-off
What if my name is longer than 19 characters? Use std::string, rather than become infected with the C Programmer's Disease.

Agreed!
Why are you even bothering with a character array unless you are learning from an outdated tutorial or book?

[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe

This topic is closed to new replies.

Advertisement