GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-Sep-2004, 03:34
Kay Chan Kay Chan is offline
New Member
 
Join Date: Sep 2004
Posts: 15
Kay Chan is on a distinguished road

Cin string


I want to input sth and store it by string. However, when I input the words that contains space, the variable only store the characters after the space
eg...Good Boy <--only store the Boy in the variable

string word;
cin >> word;

How to solve this problem? Also, I want to hold the screen in sub menu? When user click enter, the sub menu go back to main menu
  #2  
Old 05-Sep-2004, 07:34
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by Kay Chan
I want to input sth and store it by string. However, when I input the words that contains space, the variable only store the characters after the space
eg...Good Boy <--only store the Boy in the variable

string word;
cin >> word;

Use cin.getline(word) for char *, or getline(cin, word) for string

CPP / C++ / C Code:
string word;
cout << "\nEnter word \n";
getline(cin, word);
cout <<  word.c_str();

Don't forget using namespace std;

Regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 13-Oct-2004, 07:53
wc3promet wc3promet is offline
New Member
 
Join Date: Oct 2004
Posts: 7
wc3promet is on a distinguished road

Luci's code doesn't work


Quote:
Originally Posted by LuciWiz
Use cin.getline(word) for char *, or getline(cin, word) for string

CPP / C++ / C Code:
string word;
cout << "\nEnter word \n";
getline(cin, word);
cout <<  word.c_str();

Don't forget using namespace std;

Regards,
Luci

It still only reads Boy.
  #4  
Old 13-Oct-2004, 09:09
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by wc3promet
Luci's code doesn't work.

Really

What compiler did you use? It works fine under Visual C++ 6.0. Maybe it's not correct for other compilers?
There is a certain problem with *older* VC compilers (including 6), but this does not affect the reading of your line.
There is also a problem with using getline after getline. Maybe you did this? Because there is a newline pushed in by the environment - nobody knows why . You can solve this by using a "dummy" variable to "catch" the newline:

CPP / C++ / C Code:
	string word1, word2, dummy;

	cout << "Enter first word " << endl;
	getline(cin, word1);
	cout <<  word1.c_str() << endl;

	cout << "Enter second word" << endl;

//	Get rid of the newline inserted by the environment
	getline(cin, dummy);
	
	getline(cin, word2);
	cout << word2.c_str() << endl;

Or, you could use ignore to get rid of this annoying "feature":

CPP / C++ / C Code:
	string word1, word2, dummy;

	cout << "Enter first word " << endl;
//	cin >> word1;
	getline(cin, word1);
	cout <<  word1.c_str() << endl;


	cout << "Enter second word" << endl;
	
//	Get rid of the newline inserted by the environment
	cin.ignore(1,'n');
	
	getline(cin, word2);
	cout << word2.c_str() << endl;


Hope this solves your problem. If not, please let me know!

Kind regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #5  
Old 13-Oct-2004, 10:16
wc3promet wc3promet is offline
New Member
 
Join Date: Oct 2004
Posts: 7
wc3promet is on a distinguished road

I'm using Dev C++ 4.9.9.0


Quote:
Originally Posted by LuciWiz
Really

What compiler did you use? It works fine under Visual C++ 6.0. Maybe it's not correct for other compilers?
There is a certain http://support.microsoft.com/default.aspx?scid=KB;en-us;q240015 with *older* VC compilers (including 6), but this does not affect the reading of your line.
There is also a problem with using getline after getline. Maybe you did this? Because there is a newline pushed in by the environment - nobody knows why . You can solve this by using a "dummy" variable to "catch" the newline:

CPP / C++ / C Code:
	string word1, word2, dummy;

	cout << "Enter first word " << endl;
	getline(cin, word1);
	cout <<  word1.c_str() << endl;

	cout << "Enter second word" << endl;

//	Get rid of the newline inserted by the environment
	getline(cin, dummy);
	
	getline(cin, word2);
	cout << word2.c_str() << endl;

Or, you could use ignore to get rid of this annoying "feature":

CPP / C++ / C Code:
	string word1, word2, dummy;

	cout << "Enter first word " << endl;
//	cin >> word1;
	getline(cin, word1);
	cout <<  word1.c_str() << endl;


	cout << "Enter second word" << endl;
	
//	Get rid of the newline inserted by the environment
	cin.ignore(1,'n');
	
	getline(cin, word2);
	cout << word2.c_str() << endl;


Hope this solves your problem. If not, please let me know!

Kind regards,
Luci


I'm using Dev C++ 4.9.9.0.
I was trying to find one easy way to read in the User's Input: "First Middle Last" or "First Last" as 1 complete String instead of splitting them into 3 Strings, then joining them back.
  #6  
Old 14-Oct-2004, 02:00
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
I'm sorry, I don't have this compiler, nor can I install it on this machine.... Hope someone who works with it will help you out. My solutions work correct under Visual C. I don't know what the problem is with your compiler.

Regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a .html file, check that file for links salemite C Programming Language 10 17-Jan-2008 08:56
Re: Conversion: Binary, Decimal, Hexadecimal WaltP C Programming Language 1 10-May-2006 07:49
Including Maps and strings?? maddie C++ Forum 17 05-Jul-2004 07:25
storing a token pointer as a string CoreLEx C Programming Language 1 07-Oct-2003 12:33

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:21.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.