Sign in to follow this  

[c++] help please input error

This topic is 3112 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

This topic is 3112 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this