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

this is what i did but its not working.
21 Replies
Eakam
Eakam3y 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
RevanOP3y ago
yeah its gonna take in a month/year and they are seperated by a /
Eakam
Eakam3y ago
So read until the / to get the month And then read until the end of the line to get the year
Revan
RevanOP3y ago
so this?
Revan
RevanOP3y ago
yeah no that does not work
Eakam
Eakam3y 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
RevanOP3y ago
So I have to use cin.ignore(‘\n’) in the same line to ignore the end of the line ?
Eakam
Eakam3y ago
ignore does not ignore one thing. It ignores everything until it find what you specify
Revan
RevanOP3y ago
So I don’t want it to ignore the first two numbers before /
Eakam
Eakam3y ago
Yea, you want to read those number before you ignore the '/'
Revan
RevanOP3y ago
cin>>variable1>>cin.ignore(‘/‘)>>variable2 ?
Eakam
Eakam3y 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
RevanOP3y ago
Revan
RevanOP3y 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
Eakam3y ago
Haven't done c++ in a while but you can't chain these?
Revan
RevanOP3y ago
my c++ knowledge is about 1 month. whats a chain? unless chaining is c program thing as well then its my fault
Eakam
Eakam3y 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
RevanOP3y ago
Revan
RevanOP3y ago
yeah i get this when i try that
Eakam
Eakam3y ago
Ah, so it isn't possible to do that. Unfortunate
Revan
RevanOP3y ago
oh well it works the other way so its all good thanks for all the help
Want results from more Discord servers?
Add your server