944,149 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3982
  • C++ RSS
Oct 28th, 2004
0

Multiple data being read in

Expand Post »
Hi folks,

I'm some difficulty getting my program to read in multiple data types off one like of data being input.

The way it works is someone enters a name, age, and date of birth, and I then have to process this so that it'll read in these different data types off one line.

I've tried using cout to prompt for the data and then reading the data type in with cin but it keeps giving me an error saying "no operator defined which takes a right-hand operand of type 'const char' (or there is no acceptable conversion)".

I can't seem to lose this error no matter what I try.

The other thing is, is it possible, using cin, to have spaces defined as part of the entry from cout i.e. when you type name you hit space and that cin will acknowledge the space being there?

I'm not sure how well I've explained myself so please let me know.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Deiwos is offline Offline
4 posts
since Aug 2004
Oct 28th, 2004
0

Re: Multiple data being read in

>"no operator defined which takes a right-hand operand of type 'const char'
Post your code.

>when you type name you hit space and that cin will acknowledge the space being there?
cin's >> operator ignores whitespace unless you tell it to do otherwise. The best option for single line input is to use getline and parse the string once your program has it saved. The reason for reading an entire line is because the user may not know how to format their input such that your program understands. My reason for mentioning this is because dates are fomatted differently all over the world. It's easier just to read it as a string:
C++ Syntax (Toggle Plain Text)
  1. string name, date;
  2. int age;
  3.  
  4. cout<<"Enter your name, age, and date of birth: ";
  5. cin>> name >> age >> date;
Because users are rarely able to format input properly even when they do know how, dealing with an error is easier if you have the string in memory and can go over it again. Handling an error from the stream is not as simple because it is a read-once affair.
C++ Syntax (Toggle Plain Text)
  1. string record;
  2.  
  3. if ( getline ( cin, record ) ) {
  4. if ( !is_valid ( record ) ) {
  5. // Handle the error
  6. }
  7. else {
  8. // Parse the record
  9. }
  10. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: file access problem(access and oracle)
Next Thread in C++ Forum Timeline: Using the STL LIst Container, how do I create, write,read, and store in file.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC