Input Multi-word "Strings"

Hi!

I'm pretty new to programming. I'm learning to use classes. My class has a private array of structures which includes character arrays.

Part of my assignment is to be able to input multi-word strings into these arrays. I can't figure out how to do this. Help, please?
I've tried using both of those and neither of them work. I've included the string header file and everything. I'm thoroughly confused.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Prog1Class::setStructData()
{
	for(int i=0; i<5; i++)
	{
	
		cout << "Please enter an integer value: " << endl;
		cin >> privArray[i].m_iVal;
	
		for(int j=0; j<5; j++)
			{
				cout << "Please enter a double value: " << endl;
				cin >> privArray[i].m_dArray[j];
			}

		cout << "Please enter a multi-word string: " << endl;
		cin.getline(privArray[i].m_sLine, 79);
		
	}


And I did it the other way replacing the "cin.getline" line with this:

 
 getline (cin, privArray[i].m_sLine);


Neither will work. For cin.getline it crashes at that point, for getline(cin, blahblah) it doesn't compile and gives me these error messages:

"error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Alloc> &' from 'char [80]'"

and

"error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : expects 3 arguments - 2 provided"


Any idea?
std::getline is only meant to work with std::strings, while you have a char array.

As for cin.getline(), how does it crash? Try adding cin.ignore() before the getline.
I ran the program with the cin.getline code. It didn't crash this time, but it didn't let me input any characters. Then, when it output the character array, it was a weird symbol repeated over a few lines.

How would I incorporate cin.ignore() in this situation? I know of the function but I'm not TOO familiar with it.
The ignore would go immediately before getline. Its purpose is to remove a newline from input stream, left there by "cin >> ..."
Though I am not sure that is your problem. Even without it, you should receive a clean empty string, instead of garbage. If adding ignore() doesn't work, post all/more of your code.
Topic archived. No new replies allowed.