Re: Multiple String Inputs
You have to do cin.getline(const char* buf, int size, char delim) where buf is the addresss of a character array, size is the size of buf, and delim is the terminating character that denotes the end of the string. If you use this, the function will read the user-inputted string and automatically append a null terminator '\0' to the end of it. The delim should be '\n', and it is not put into buf.
Re: Multiple String Inputs
Thanks for that, it works fine in taking the full string in.
the problem i have now is that if the user enters over the max amount of characters, it doesnt seem like the data is leaving the cin object. It doesnt let me enter another input. Here is a test program that im using.
Code:
void main()
{
int loop = 1;
while(loop == 1){
char userInput[24];
cin.getline(userInput, 24, '\n');
cout << userInput << cin.gcount() << "\n";
getch();
}
}
Re: Multiple String Inputs
Assume for a moment that you don't have getch(). If you enter a string that's too big, cin.getline just puts the first 23 letters of it into userInput (the 24th character in userInput is the null terminator). However, the remaining characters from the inputted string remains in the cin stream. As a result, when your while loop repeats, it automatically puts the remaining characters in userInput and displays them (i.e., it doesn't give you a chance to enter a new string). It will keep doing this until it has gotten and displayed the entire inputted string, at which point it will again prompt the user to enter a new string.
Now, when getch() is called, it doesn't pause the program. This means that your while loop is still repeating while the program waits for you to press a key. As a result, getch() ends up being called multiple times, so you have to press several keys before your program will display userInput.
For example, let's say you entered a 30 character string. Getline would put the first 23 characters in userInput (leaving the remaining 7 in the input stream). It would then output the first 23 characters and call getch(). However, regardless of whether you press a key, the while loop will repeat and get the remaining 7 characters from the input string, put them in userInput, output them, and call getch(). Then the while loop will repeat again, but since you've gotten all the characters of the inputted string, cin.getline will pause and wait for user input. However, at this point, getch() has been called twice, therefore you need to press two keys before the program will continue.
I think this is your problem. Try entering a 30 character string in your test program and see if pressing two keys solves the problem.