cin.getline
I am trying to use this command, 'cin.getline', in a loop, but it does not work properly; in details, it works the first time the loop is executed; the subsequent times, it is just ignored; now, in all the books I am using ther's no mention of this strange behavior; nor there is anything in the tutorial that comes with the Visual C++ that I have. Could you please help me overcome this problem, and explain to me why is behaving like that, done the fact that in my books, and in the documentation ovf Visual C++ it says that this command does NOT carry any linefeed?
thanks so much for the attention.
I include an example of the code where I have met this problem; but is not the only one time...
// size1.cpp
//
#include <iostream.h>
#include <conio.h>
#include <io.h>
#include <stdio.h>
int main()
{
char arr[10][30];
int a = 0;
char ind;
float cont = 0.0f;
for(a; a < 10; a++)
{
cout << "\nenter a word: ";
cin.getline(arr[a],30, '\n'); // quì non va; e non
// è la prima volta!!!
for(int i = 0; i <30; i++)
{
cout << arr[a][i];
if(arr[a][i] == '\0')
{
break;
}
}
//getchar();
cout << "do you want to enter another one?";
cout << "\n(Enter 0 to end, 1 to continue): ";
cin >> ind ;
if(ind == '0')
{
break;
}
else
{
cout << " " << endl;
}
}
cont = sizeof(arr)/sizeof(arr[0]);
cout << cont;
cout << endl;
return 0;
}
|