Click to See Complete Forum and Search --> : Multiple String Inputs
Rue
March 11th, 2005, 11:55 AM
Hi there, I’m currently writing a piece of software that will take an input from a user terminal and then sends it to another process using a memory mapped file. Well that’s all coded and I though that would be the hard part.
However I’m having problems with the user input, I’m just using a basic cin >> statement and if the user types in
“hello world”
Then program thinks that’s 2 arguments and will send “hello” then “world” to the other process.
Is there away to get the program to read in the command prompt line as one single zero terminated string?
Here is the basic code im using
while(loop != 1){
cout << "Please enter the detials you want to add to the list\n";
cin >> userInput;
strcpy(SharedVirtualMemory2, userInput);
sleep(1000);
}
Edit:
If you have VS.Net installed then i have attached the full project in .rar format, the code is very well commented and my problem is in UserProducer.cpp.
Cheers for any help in advance.
Ross
dro873
March 11th, 2005, 02:40 PM
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.
Rue
March 11th, 2005, 04:25 PM
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.
void main()
{
int loop = 1;
while(loop == 1){
char userInput[24];
cin.getline(userInput, 24, '\n');
cout << userInput << cin.gcount() << "\n";
getch();
}
}
dro873
March 11th, 2005, 05:13 PM
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.