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!

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? ";
        [b][u]cin.getline(name, 30);[/u][/b]
 
        cout << "Desc? " << endl;
        [b][u]cin.getline(desc, 256);[/u][/b]
 
        cout << "Name: " << name << endl;
        cout << "Desc: " << endl;
        cout << desc << endl;
 
        return 0;
}

(Also, if you want to be lazy and not change your code, you might be able to put " " around what you're typing - in the same way that you need double quotes on the command line to indicate a single parameter that has a space)

cin's operator>> uses whitespace as a delimiter:

string s;

cin>> s;   // Type "John Doe"
cout<< s; // Prints "John". "Doe" left in the stream

>I tried flushing the input buffer with cout.flush()
What made you think that calling the flush member function of an output stream would somehow "flush" the input stream? I think you're confused about the definition of flush, and the relationship of cin and cout.

To flush a stream is to write the contents of the stream buffer to the external device for an output stream. This in no way implies that flushing an input stream will somehow discard the contents of the stream. In fact, basic_istream doesn't even have a flush member function because it's nonsensical.

cin and cout are related, but only because they are tied together so that any request for input using cin will automatically flush cout. Notice that any unwritten characters in cout's stream buffer are written to the output device. No part of this has an effect on cin's stream buffer.

So calling cout.flush() will force the stream buffer for cout to be written to the output device. It won't affect cin in any way. The proper way to "flush" the input stream is with basic_istream's ignore member function:

cin.ignore(); // Discard one character
cin.ignore(80); // Discard up to 80 characters or EOF
cin.ignore(80, '\n'); // Discard up to 80 characters or a newline

A good way to "flush" the input stream without using arbitrary magic numbers is using numeric_limits, defined in <limits>:

cin.ignore(numeric_limits<streamsize>::max(), '\n');

>What am I to do here?
The general rule of thumb is that if you want to use whitespace as a separater, use operator>>. Otherwise use getline.

>you might be able to put " " around what you're typing
Then again, you might not. How the shell works is beyond the scope of C++, so it's hard to guarantee a correct and portable program when you rely on it.

Oh and please take note of the subtle hint that our dear friend Narue left us: stream input, NOT string input.

It's trully important as a developing programmer to fully understand and internalize such concepts. Do you know what a stream is? Do you know how a stream is implemented? Learning by example can only get you so far, insight takes you the rest of the way, IMHO.

Thanks everyone. I truely appreciate all the help. I'm even dumber than I thought! xD

>Do you know what a stream is?
That's a good concept to understand.

>Do you know how a stream is implemented?
Most programmers need not know or care how streams are implemented. It's sufficiently complicated enough to be abstracted away for everyone except those who have a need to write a custom stream.

>Do you know how a stream is implemented?
Most programmers need not know or care how streams are implemented. It's sufficiently complicated enough to be abstracted away for everyone except those who have a need to write a custom stream.

I disagree. I think it can seperate a good programmer from an expert programmer no matter what the language or how abstract it is. IMHO an expert programmer not only knows how to use the mechanism, they also know how the mechanism works. They have an intimate relationship with the language and the system of operation. It's my belief that all programmers should strive for this type of excellence. But... this is a C programmer talking :)

>I think it can seperate a good programmer from an expert programmer
No debate there.

>they also know how the mechanism works
We still agree.

>It's my belief that all programmers should strive for this type of excellence.
Yes, of course. But, not all programmers are as addicted to the craft as you and I, and it would be foolish of us to expect them to be. Most programmers couldn't care less how good they are as long as they're good enough to do the job. For them, "good" is good enough and "expert" requires too much work learning things that they just aren't going to use. So, for average programmers, who form the majority, there's no need to learn how streams are implemented unless they intend to implement a stream.

>But... this is a C programmer talking
And this is a C programmer responding. ;)

It's my belief that all programmers should strive for this type of excellence.

Agreed. Sadly, however, I don't believe that I actually have the capacity to get into the nitty-gritty of a language. So I may end up another who stagnates in the good pile, not the expert pile. I could be underestimating my intelligence, but at my current state, the idea of that level of expertise is something gone with the wind.

Agreed. Sadly, however, I don't believe that I actually have the capacity to get into the nitty-gritty of a language. So I may end up another who stagnates in the good pile, not the expert pile. I could be underestimating my intelligence, but at my current state, the idea of that level of expertise is something gone with the wind.

Perhaps. You're still young and for most people, including myself, learning takes time and patience. When learning how to program, learning to ask the _right_ questions will yield the most valuable answers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.