cin.getline()

Oct 1, 2011 at 3:40pm
I'm having trouble with this:

1
2
3
4
5
for(int i = 0; i < n; i++)
	{
	cout << "Enter the name of student " << (i + 1) << std::endl;
	cin.getline(pa,n);
	}


To be more precise, it's this
 
cin.getline(pa,n);


Apparently there aren't enough arguments, is there any way to get this to work?

If any more code is needed, I will give it to you.
Last edited on Oct 1, 2011 at 4:03pm
Oct 1, 2011 at 4:53pm
plz elaborate your problm
Oct 1, 2011 at 5:22pm
closed account (zb0S216C)
Assuming pa is of type char[], that call should be fine. According to this site's reference, all arguments are accounted for.

Hmmm...

Wazzak
Oct 1, 2011 at 6:04pm
Framework, you have it right and I thought the same thing too.
But, when I put my mouse over the red squiggly line on the ., the error message is:


Error: No instance of overloaded function "std::basic_istream<_Elem,_Traits>::getline[with_Elem=char,_Traits=std::char_traits<char>]" matches the argument list


And with the closing brace, it reports:


Error: too few arguments in function call


Does anyone know of the reason it may be doing this?
Oct 1, 2011 at 6:26pm
not sure if this helps


Using cin.getline

The getline member function is similar to the get function. Both functions allow a third argument that specifies the terminating character for input. The default value is the newline character. Both functions reserve one character for the required terminating character. However, get leaves the terminating character in the stream and getline removes the terminating character.

The following example specifies a terminating character for the input stream:

#include <iostream.h>

void main()
{
char line[100];
cout << " Type a line terminated by 't'" << endl;
cin.getline( line, 100, 't' );
cout << line;
}


Maybe just use cin.get? Maybe your compiler is requiring a terminating character.

-BHill
Oct 1, 2011 at 8:40pm
Using cin.get() does help me with the finishing brace - so thank you for that - but the first error is still there; that is:


Error: No instance of overloaded function "std::basic_istream<_Elem,_Traits>::getline[with_Elem=char,_Traits=std::char_traits<char>]" matches the argument list
Last edited on Oct 1, 2011 at 8:41pm
Oct 1, 2011 at 8:48pm
Did you try giving it a 3rd argument as a terminating character just to see if the error goes away?

-BHill
Oct 1, 2011 at 8:50pm
Maybe it needs to be:
 
std::cin.getline(pa,n);


I don't really know, but it's something to try.
Oct 1, 2011 at 9:06pm
I've tried all of that, but still I'm doing something wrong.

This is really getting annoying.
Oct 2, 2011 at 10:03am
closed account (zb0S216C)
If you're using VC++, try rescanning the project. That will force Intellisense to relocate all used symbols, functions, etc.

Wazzak
Last edited on Oct 2, 2011 at 10:04am
Oct 2, 2011 at 12:11pm
Here:
Hope it this helps ;)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

#define n 7 

int main(){
    
    char pa[n];


for(int i = 0; i < n; i++)
	{
	cout << "Enter the name of student " << i <<
     endl;
	cin.getline(pa,n);
	}
	
	system("PAUSE");
	return 0;
}
Topic archived. No new replies allowed.