4 Replies - 249 Views - Last Post: 18 September 2011 - 02:03 PM Rate Topic: -----

#1 tim9009  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 73
  • Joined: 01-October 10

C++ cin problem

Posted 18 September 2011 - 12:42 PM

Hello :)

I have a problem when using the cin comand. here is some code for you:
int login()
{
	string choice;

	cout << "Login" << endl << endl << endl;
	cout << "1) New game" << endl;
	cout << "2) Load game" << endl;
	cout << "3) Dev mode" << endl;
	cout << endl << "Please make a choice: ";
	cin >> choice;

	if (choice == "1" || "new game")
	{
		login_new_game();
	}
	else
	if (choice == "2" || "load game")
	{
		cout << "load";
		system("pause");
	}
	else
	if (choice == "3" || "dev mode")
	{
		cout << "dev";
		system("pause");
	}

	return 0;
}

void login_new_game()
{
	system("cls");
	cout << "Login" << endl << endl << endl;

	cout << "Enter save name: ";

	string filename = "default";
	cin >> filename;

	ofstream makefile;
	makefile.open(filename + ".txt");
	
	if (makefile.is_open())
	{
		makefile.clear();

		makefile.close();
		cout << "File " << filename << " was successfully created/overwrited" << endl;
		system("pause");
	}
	else 
	{
		cout << "Error: Could not create file, please try again." << endl;
		system("pause");
		return;
	}
	
}



I know that's alot of code but hey, I need help :P
so the problem is: when I run the program I get asked to enter a choice, lets say I write "new game" then I get to the "login_new_game" function, but somehow the data from last time I wrote have been stored so that I get a .txt file called game.

The "login" function only see that I wrote "new" then stores "game" and enters it in the "login_new_game" function... how do I avoid this? and how to I make the "login" function check the whole string so that it will only enter the "login_new_game" function if I write "new game" and not just "new".

Hope you understand what I'm trying to say ;)

-Tim

This post has been edited by tim9009: 18 September 2011 - 12:43 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ cin problem

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,298
  • Joined: 25-December 09

Re: C++ cin problem

Posted 18 September 2011 - 12:50 PM

To get a string that has spaces you need to use getline(). The extraction operator>> stops when it encounters a white space character.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,362
  • Joined: 27-December 08

Re: C++ cin problem

Posted 18 September 2011 - 12:52 PM

Moved to C/C++. Please reserve the C++ Forum for topical, non-help related discussion.
Was This Post Helpful? 0
  • +
  • -

#4 tim9009  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 73
  • Joined: 01-October 10

Re: C++ cin problem

Posted 18 September 2011 - 01:56 PM

Thank you Jim, and I'll keep that in mind mac :)

Oh, and while we are here, is there a way for me to write a " inside a string? :)
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6032
  • View blog
  • Posts: 23,414
  • Joined: 23-August 08

Re: C++ cin problem

Posted 18 September 2011 - 02:03 PM

Use a backslash to escape special characters, like quotes, inside a string.

const char *titleString = "The title is \"Revenge of the Nerds\"."

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1