cout & printf (c++)

Sep 2, 2014 at 1:10pm
printf("student's name/score: ");
scanf("%s/%e", &m1, &n1);

if I wrote this than I can input John/99, John will be in variable m1 and 99 will be in variable n1. How do I do this with cout and cin?
Sep 2, 2014 at 1:14pm
Sep 2, 2014 at 1:19pm
not how to put a data in a variable but how to get input of several data in a sentence and separate it into several variable.
Sep 2, 2014 at 1:24pm
Have you even read that link?
Extractions on cin can also be chained to request more than one datum in a single statement:
cin >> a >> b;
Last edited on Sep 2, 2014 at 1:24pm
Sep 2, 2014 at 1:30pm
yes. oh found it. I just read the example code. Sorry... Quick question, so, using cout and cin we can only separate data using space, tab, and new line. We can't customize it as in printf?
Last edited on Sep 2, 2014 at 1:33pm
Sep 2, 2014 at 1:41pm
You can customize it, unlike in scanf (did you run the code from your first post?)
Last edited on Sep 2, 2014 at 1:49pm
Sep 2, 2014 at 1:42pm
You can separate the data anyway you want with cout.
cout does not provide any implicit separators. You provide what you want.
Note that printf does not provide any implicit separators either.

For delimited input, you probably want to use getline.
http://www.cplusplus.com/reference/string/string/getline/


Sep 2, 2014 at 1:55pm
yes I run the code. But error occur due to using std::string. After a while I decided to change it to cout and cin. Sorry, I'm completely new to this, just got my first lesson today. If it's not too bothering, can I get an example using cout and cin with / as the separator of the data? Sorry, if my English is weird, it's not my first language
Last edited on Sep 2, 2014 at 2:05pm
Sep 2, 2014 at 3:15pm
The first form of getline in AbstractionAnon's link takes three parameter.
When text up to (and including) the '/' has been read from the stream, you can use >> to get the number.
Topic archived. No new replies allowed.