if(cmd=="new"){ string name,desc; cout<<"Name? "; cin>>name; Setcursor(Conloc); cout<<"Desc? "; cin>>desc; Setcursor(Bodyloc); Invitem Item(name,desc);When the values of the string desc is set through cin, if a space is used, all text after that space isn't used. Also, If two words are used when setting name, it will give the second word to desc and skip that. I tried flushing the input buffer with cout.flush(), but it didn't work. What am I to do here?
Thanks!
Hello,
I could barely decipher what the heck you were saying. In the future could you please be a bit more articulate. That's the least you could do in return for help.
Now, let's see.... You have two variables,name and desc and you're having the problem where you might want to specify a first and last name as well as a description containing more then one word.
My understanding is that by default C++ cin streams ignore whitespace, carriage returns, and tabs.
So, let's say your input is "John Doe", with your code we'll first do a "cin>>name". You will read the stream, one character at a time, filling "name". Once you come upon an ignored character (the whitespace right after the 'n' in "John") you'll stop, making name equal to "John". Then, when you read from the stream again with "cin>>desc", you will read the stream, one character at a time, filling desc. Once you come upon an ignored character (carriage return right after the 'e' in "Doe") you'll stop, making desc equal to "Doe".
A simple solution to your problem is to use getline() by which you can specify "read the stream up to N characters or until we reach our delimiter" By default, our delimiter is "\n" (newline)
#include <iostream>
using namespace std;
int main()
{
char name[30];
char desc[256];
cout << "Name? ";
<strong>cin.getline(name, 30);</strong>
cout << "Desc? " << endl;
<strong>cin.getline(desc, 256);</strong>
cout << "Name: " << name << endl;
cout << "Desc: " << endl;
cout << desc << endl;
return 0;
}