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 06-May-2008, 10:30
Mrehman Mrehman is offline
New Member
 
Join Date: Mar 2008
Posts: 23
Mrehman is on a distinguished road
Question

is it possible to use cin.getline in strings


hey guyz what's up? my question is same as the title above. is it any way possible for you to use cin.getline in strings.if not then how do i read a whole line in this code. it's all the way at the bottom.
CPP / C++ / C Code:
#include <iostream>
#include <string>
using namespace std;
 
struct Information
{
	string name;
	string age;
	string state;
	
};


void main()
{  
	Information Member[100];
	
	cout<<"Data for Members :"<<endl;
	Member[0].name = "Yun Li";
	Member[0].age  = "19";
	Member[0].state = "California";
	
	Member[1].name = "Rashad Brooks";
	Member[1].age  = "22";
	Member[1].state = "New York";
	
	Member[2].name = "Anthony Miller";
	Member[2].age  = "27";
	Member[2].state = "Vermont";
	
	Member[3].name = "Andre Mitchell";
	Member[3].age  = "20";
	Member[3].state = "Texas";
	
	Member[4].name = "Kevin Lucas";
	Member[4].age  = "33";
	Member[4].state = "Mississippi";
	int i;
	for (i=0;i<5;i++)
	{
		cout << "Enter a name for user #" << i+1 << ": ";
		cin >> Member[i].name;
		

	}		
} 


when i run the program it asks me to enter a name for member # 1. it did so but if i put the whole name in like Kevin Lucas and press enter it will come like this
please enter the data for member # 1
//i entered the whole name
it will display this:
please enter the data for member# 2
please enter the data for member #3
so basically it will skip the 2nd one. whenever i enter the whole name it will skip. how can i fix this.
  #2  
Old 06-May-2008, 11:30
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: is it possible to use cin.getline in strings


[quote=Mrehman is it any way possible for you to use cin.getline in strings.[/QUOTE]

CPP / C++ / C Code:
        cout << "Enter a name for user #" << i+1 << ": ";
        getline(cin, Member[i].name);

I respectfully suggest that you to get a book or check an online reference such as http://www.cplusplus.com/reference/string/getline.html

I mean, it's OK to ask, but wouldn't either of these be quicker than posting a request for help and waiting for a helpful response?

Regards,

Dave

Footnote:
Note that cin.getline() is for char arrays. The global function getline() for std::strings is used as I show. In my opinion, there is rarely a (good) reason to use char arrays for things like this in a C++ program.
Last edited by davekw7x : 06-May-2008 at 12:01.
  #3  
Old 06-May-2008, 11:35
L7Sqr L7Sqr is online now
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 119
L7Sqr has a spectacular aura about

Re: is it possible to use cin.getline in strings


cin takes input as space delimited.
example:
CPP / C++ / C Code:
string a, b, c;
cout << "Enter three strings..." << endl;
cin >> a;
cin >> b;
cin >> c;
cout << "a : '" << a << "'" << endl;
cout << "b : '" << b << "'" << endl;
cout << "c : '" << c << "'" << endl;
Reacts the exact same way on either of the following inputs:
Code:
$ ./a.out Enter three strings... one two three a : 'one' b : 'two' c : 'three' $ ./a.out Enter three strings... one two three a : 'one' b : 'two' c : 'three'
You may want to investigate cin.getline. Also, be sure to check the state of the stream. It is entirely possible to have bad things happen on malformed user input.
  #4  
Old 06-May-2008, 12:45
Mrehman Mrehman is offline
New Member
 
Join Date: Mar 2008
Posts: 23
Mrehman is on a distinguished road

Re: is it possible to use cin.getline in strings


Thanks guyz.
 
 

Recent GIDBlogToyota - 2008 September Promotion by Nihal

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
strings?!!!! :S nooranshasi C Programming Language 17 18-Nov-2006 19:58
binary search on vector of objects sorted by strings qboy C++ Forum 8 13-Oct-2006 20:14
Menu of character strings aijazbaig1 C Programming Language 21 27-Jul-2006 03:07
need help - questions about strings Benayoun C Programming Language 6 24-Jan-2005 02:15
C++ style strings and STL dexter C++ Forum 14 04-Jan-2005 07:46

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

All times are GMT -6. The time now is 22:54.


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