cin.getline

Started by
6 comments, last by XDarkScar 20 years, 11 months ago
man I forogt what someone said about the cin.getline..

void new_game()
{
	cout << "Enter Diffacalty (1-10): ";
	cin >> diff;
	cout << "Enter The Armies Name: ";
	cin.getline(aname, 20, ''\n'');
	cout << "Enter The Commanders Name: ";
	cin.getline(cname, 20, ''\n'');
	game_write();
}
 
I have the strings aname, and cname, and the int diff up top. I forgot the word to clear or stop the get line, or something Can someone help? Easy way of programming: Code, Graphics, Swearing....
Easy way of programming: Coding, Debugging, Swearing.
Google / Game Tutorials / GameDev.Net / My Project server. Check it out.
Project: N/A / Percent Complete: 0% / Due Date: N/A
Advertisement
I''m sorry, but I don''t really know what you mean. Did you want to clear the whole screen or just your variables?

Anyway don''t mind me I''ve been up > 24hr and can barley tipe.

------------------------------
p.s. excuse me english. im from L.A.
lol
well someone told me the code it was like, when it retches the ''\n'' it stops, im getting errors without it and I forgot the code it sucks..

Easy way of programming: Code, Graphics, Swearing....
Easy way of programming: Coding, Debugging, Swearing.
Google / Game Tutorials / GameDev.Net / My Project server. Check it out.
Project: N/A / Percent Complete: 0% / Due Date: N/A
Yes, I remember the topic: Link.

Here is one of my posts on the topic. I think this is the one that you are talking about:

My post:


O.K., I didn't realise that you were using std::cin to get user input for an integer before.

For your particular code, use this:


std::cout << "Enter Difficulty (1 - 10)\n";
std::cout << "|: ";
std::cin >> Player.diff;
std::cin.get();

std::cout << "\nDifficulty Mode " << Player.diff << 'n';

std::cout << "\nEnter your Force's name\n";
std::cout << "|: ";
std::getline(std::cin, Player.name, '\n');

std::cout << "Squad Name: " << Player.name << "\n";


If you wanted to know, using std::cin to get an integer leaves a newline character ('\n') in the input queue. std::getline() reads keyboard input until it reaches a newline character. Because a newline character is left in the input queue, std::getline() terminates straight away and the user has no chance to enter input. The line:


std::cin.get();


"removes"/"throws away" the newline character from the input queue, allowing you to use std::getline() to aquire the name.

Good luck.


I think this is the one that you are talking about. If not, it is somewhere in the post that I provided a link to.

[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on April 28, 2003 12:38:54 PM]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
um, the way cin works, is that it will actually go until it hits any form of white space, space, new line, etc... what cin.getline forces is that you get the entire line, until the character of your choice, in your case the escape sequence for a new line. in your example, you are storing 19 characters into cname, or aname, or until you reach the and basically, if you don''t specify, it looks like it is ''\n'' anyway clicky for all of your getline needs.
tx man

Easy way of programming: Code, Graphics, Swearing....
Easy way of programming: Coding, Debugging, Swearing.
Google / Game Tutorials / GameDev.Net / My Project server. Check it out.
Project: N/A / Percent Complete: 0% / Due Date: N/A
A yea secoind squestion why is this switching

void game_write(){	ofstream fout;	fout.open("Game Data.txt");	fout << "Gold: " << gold << endl;	fout << "Rank: " << rank << endl;	fout << "Men: " << men << endl;	fout << "A Men: " << attack_men << endl;	fout << "D Men: " << defense_men << endl;	fout << "Spys: " << spys << endl;	fout << "Army Tired: " << army_tired << endl;	fout << "Army Name: " << aname << endl;	fout << "Commander Name: " << cname << endl;	system("cls");	game();}void new_game(){	cin.get();	cout << "Enter Diffacalty (1-10)\n";	cout << "|: ";	cin >> diff;	cout << "Enter The Armies Name\n";	cout << "|: ";	getline(cin, aname, ''\n'');	cout << "Enter The Commanders Name\n";	cout << "|: ";	getline(cin, cname, ''\n'');	game_write();} 

iv checked the strings there correct the out come on Game
Data.txt

typed in Commander name: bla bla
typed in Army name: bla bla bla

Gold: 0
Rank: 0
Men: 50
A Men: 0
D Men: 0
Spys: 0
Army Tired: 0
Army Name:
Commander Name: bla bla bla

why is it switching??

Easy way of programming: Code, Graphics, Swearing....
Easy way of programming: Coding, Debugging, Swearing.
Google / Game Tutorials / GameDev.Net / My Project server. Check it out.
Project: N/A / Percent Complete: 0% / Due Date: N/A
You need to call cin.get() everytime between

cin << something;

AND // cin.get() goes here

getline(cin,etc...);


otherwise cin << something; leaves the '\n' in buffer and getline returns immeadiately without actaully getting a line.


So something like this..
void new_game(){	cout << "Enter Diffacalty (1-10)\n";	cout << "|: ";	cin >> diff;        cin.get(); // clears the '\n' from input stream	cout << "Enter The Armies Name\n";	cout << "|: ";	getline(cin, aname, '\n');	cout << "Enter The Commanders Name\n";	cout << "|: ";        getline(cin, cname, '\n'); 	game_write();}  



[edited by - Illumini on April 28, 2003 5:48:12 PM]

This topic is closed to new replies.

Advertisement