string input not working as expected

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2004
Posts: 24
Reputation: SquirrelProdigy is an unknown quantity at this point 
Solved Threads: 0
SquirrelProdigy's Avatar
SquirrelProdigy SquirrelProdigy is offline Offline
Newbie Poster

string input not working as expected

 
0
  #1
May 11th, 2005
  1. if(cmd=="new"){
  2. string name,desc;
  3. cout<<"Name? ";
  4. cin>>name;
  5. Setcursor(Conloc);
  6. cout<<"Desc? ";
  7. cin>>desc;
  8. Setcursor(Bodyloc);
  9. 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!
Squirrel Prodigy
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Solved Threads: 1
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: string input not working as expected

 
0
  #2
May 11th, 2005
Originally Posted by SquirrelProdigy
  1. if(cmd=="new"){
  2. string name,desc;
  3. cout<<"Name? ";
  4. cin>>name;
  5. Setcursor(Conloc);
  6. cout<<"Desc? ";
  7. cin>>desc;
  8. Setcursor(Bodyloc);
  9. 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? ";
        cin.getline(name, 30);
 
        cout << "Desc? " << endl;
        cin.getline(desc, 256);
 
        cout << "Name: " << name << endl;
        cout << "Desc: " << endl;
        cout << desc << endl;
 
        return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: string input not working as expected

 
0
  #3
May 11th, 2005
(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)
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: string input not working as expected

 
0
  #4
May 11th, 2005
cin's operator>> uses whitespace as a delimiter:
  1. string s;
  2.  
  3. cin>> s; // Type "John Doe"
  4. 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:
  1. cin.ignore(); // Discard one character
  2. cin.ignore(80); // Discard up to 80 characters or EOF
  3. 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>:
  1. 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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Solved Threads: 1
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: string input not working as expected

 
0
  #5
May 11th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 24
Reputation: SquirrelProdigy is an unknown quantity at this point 
Solved Threads: 0
SquirrelProdigy's Avatar
SquirrelProdigy SquirrelProdigy is offline Offline
Newbie Poster

Re: string input not working as expected

 
0
  #6
May 11th, 2005
Thanks everyone. I truely appreciate all the help. I'm even dumber than I thought! xD
Squirrel Prodigy
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: string input not working as expected

 
0
  #7
May 11th, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Solved Threads: 1
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: string input not working as expected

 
0
  #8
May 11th, 2005
>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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: string input not working as expected

 
0
  #9
May 11th, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 24
Reputation: SquirrelProdigy is an unknown quantity at this point 
Solved Threads: 0
SquirrelProdigy's Avatar
SquirrelProdigy SquirrelProdigy is offline Offline
Newbie Poster

Re: string input not working as expected

 
0
  #10
May 12th, 2005
Originally Posted by subtronic
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.
Squirrel Prodigy
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC