| | |
Multiple data being read in
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2004
Posts: 4
Reputation:
Solved Threads: 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.
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.
>"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:
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.
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)
string name, date; int age; cout<<"Enter your name, age, and date of birth: "; cin>> name >> age >> date;
C++ Syntax (Toggle Plain Text)
string record; if ( getline ( cin, record ) ) { if ( !is_valid ( record ) ) { // Handle the error } else { // Parse the record } }
I'm here to prove you wrong.
![]() |
Similar Threads
- MySQL Checkbox Multiple data Delete? (PHP)
- multiple data insertion (Oracle)
- How to insert data read from text file into linked list? (Java)
- Data Read Error at startup after Power Failure (Windows NT / 2000 / XP)
- Using data i read from *.* files (C++)
Other Threads in the C++ Forum
- Previous Thread: file access problem(access and oracle)
- Next Thread: Using the STL LIst Container, how do I create, write,read, and store in file.
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






