how to input two variables in the same line?
like when i type 12 and press enter, 1 must be assigned to a, and 2 must be assigned to b. I want it to be entered on the same line, and press enter only once. How do i do that?
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
while (true)
{
int a;
int b;
string UserInput;
cout << "Yo dawg enter dat two digit number dayum:";
getline(cin,UserInput);
//So if you enter 13, UserInput[0] = 1, UserInput[1] = 3, if you enter 50, UserInput[0] = 5, UserInput[1] = 0
cin.clear();
if (UserInput.size() != 2) //if user enter an input dat aint 2 digits
{
cout << "what are you thinking i told u a 2 digit number why u typing in something else" << endl; //they done messed up
} else //if user enters a 2 digit input
{
a = (UserInput[0] - '0');
b = (UserInput[1] - '0'); //gotta subtract the char value '0' so we get that integer digit u know what im sayin?
cout << "Yo check this out son, your a was " << a << " and your b was " << b << endl; //let them know
}
}
return 0; //gotta have that return homie so u know when the program ends u know what im sayin
}
3rd digit would be stored in UserInput[2] you know what i'm saying?
here is why
so like
instead of string UserInput think of it like this
char UserInput[100]; of course it's not 100, i'm just saying for this example
Well if we put "123" into that array, the 1 will go in the first element
the first element is UserInput[0]
the 2 will go into the second element which is UserInput[1]
and the 3 will go into the third element UserInput[2]
so that being said
with a 3 digit input the 3rd digit will be in UserInput[2]