beginner array

why it skipped the first input?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <iostream>
using namespace std;

int main()
{
	char name[51][100];
	int a;
	cin>>a;

	for ( int i = 0; i < a; i++ )
	{
		cout << "\nEnter sentence: ";
		cin.getline( name[i], 99 ); //this null-terminates the array of chars by placing a '\0' at the end of the stream
	}

}
What exactly are you trying to do? You might need to post a little more information for help.
sorry, just let a= no. of sentence, but it skipped the first input sentence if i run that.
just want to know where's the prob
Trying adding a cout statement to tell you that you need to input a number for a. Also you may want to flush your input buffer since you are using getline. Here is what I would do in that for loop:

1
2
3
4
cin.ignore(cin.rd_buf()->in_avail());
cin.getline(name[i], 99);
cin.clear();
cin.ignore(cin.rd_buf()->in_avail());


If you go over the bounds for the getline, that extra stuff will be pushed into the next input line, so use this to prevent that.
Topic archived. No new replies allowed.