How do i store a cin value to two different variables?

this is what i did but its not working.
21 Replies
Eakam
Eakam2y ago
I assume you don't actually want both the variables to contain the same thing? Depending on what exactly you are trying to read here, you could set a delimiter to read what you want
Revan
Revan2y ago
yeah its gonna take in a month/year and they are seperated by a /
Eakam
Eakam2y ago
So read until the / to get the month And then read until the end of the line to get the year
Revan
Revan2y ago
so this?
Revan
Revan2y ago
yeah no that does not work
Eakam
Eakam2y ago
Oh, that's not what I meant The thing with using cin.ignore('/') first is that it will read and discard everything until it finds the '/'
Revan
Revan2y ago
So I have to use cin.ignore(‘\n’) in the same line to ignore the end of the line ?
Eakam
Eakam2y ago
ignore does not ignore one thing. It ignores everything until it find what you specify
Revan
Revan2y ago
So I don’t want it to ignore the first two numbers before /
Eakam
Eakam2y ago
Yea, you want to read those number before you ignore the '/'
Revan
Revan2y ago
cin>>variable1>>cin.ignore(‘/‘)>>variable2 ?
Eakam
Eakam2y ago
I am not sure if that syntax will work but that is what you want to do Actually that might not work since the first argument to ignore() is supposed to be the number of characters you want to ignore
Revan
Revan2y ago
Revan
Revan2y ago
this works for me but the assignment says i need to do this all in one line is there a short form for this?
Eakam
Eakam2y ago
Haven't done c++ in a while but you can't chain these?
Revan
Revan2y ago
my c++ knowledge is about 1 month. whats a chain? unless chaining is c program thing as well then its my fault
Eakam
Eakam2y ago
Like this cin >> tempExpMon >> cin.ignore(...rest of code Same way you have put the ignore and tempExpYear on one line Though if that doesn't work, you could read that character into something else I suppose
Revan
Revan2y ago
Revan
Revan2y ago
yeah i get this when i try that
Eakam
Eakam2y ago
Ah, so it isn't possible to do that. Unfortunate
Revan
Revan2y ago
oh well it works the other way so its all good thanks for all the help