0

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.

2
Contributors
1
Reply
2
Views
13 Years
Discussion Span
Last Post by Narue
0

>"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:

string name, date;
int age;

cout<<"Enter your name, age, and date of birth: ";
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.

string record;

if ( getline ( cin, record ) ) {
  if ( !is_valid ( record ) ) {
    // Handle the error
  }
  else {
    // Parse the record
  }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.