Not Working for some reason

I'm new to c++ but i just try to make a simple text based game and the cin command wasnt working for the second time i was using it here is a chuck of my code.



#include <iostream>

using namespace std;
int sticks = 0;
int health = 100;//players health
int rocks = 0;
int pickaxehealth = 50;
int swordhealth = 50;
char name;


int main()
{
int direction1;
cout << "Welcome to Classic Text 1.0." << endl;
cout << "____________________________" << endl;
cout << " Enter your name: ";
cin >> name;
cout << "You wake up in the middle of the night, In a" << endl <<
"clearing of a forest. There is a path to the" << endl <<
"left and the right. Which way do you want to go" << endl;
cout << "_______________________________________________" << endl;
cout << " Type 1 to go right. Type 2 to go left"<< endl;
cout << "Enter which way you want to go: " << endl;
cin >> direction1;








What do you mean by "it wasn't working"?
Try char* name; instead of char name;. You're using a single character instead of a character array.

(Or at least I think that's the problem. Zhuge, correct me if I'm wrong.)
no thats working but cin >> direction1; isnt
every thing works zhuge but it doesn't allow the user to type in direction1. it shut down the program with out asking you what you what to import for direction1.
Even if it's not the problem you are currently having, you should change it still because your variable name is only withholding the first character you input.

http://www.cplusplus.com/forum/articles/6046/

This may be addressing your other problem.
well right now i am reading c++ primer 5th edition, so it will probably show up in there soon.
You send a stream (std::cin) to a char pointer (for one or more chars but pointer) Stream need a buffer.
Declare name as: char name[32]. Now if std::cin data are less than 32 std::cin frees its space and send new data.
In your program std::cin access to direction but still have char data because name holds only the 1st byte.
Last edited on
Topic archived. No new replies allowed.