how to input two variables from the same line?

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?
Last edited on
closed account (2UD8vCM9)
Yo wassup my nigga ajaymehul

There are a few ways to approach this

Me personally, because i'm super lazy, would approach it with strings.

so i'd be all like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>

using namespace 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
}
Last edited on
thanks alot, i can understand. I my sound weird, but im a beginner. if we enter a 3digit number will it be stored in UserInput[3]?
closed account (2UD8vCM9)
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]
yeah yeah, i typed 3 by mistake. it comes in 2 by indexing counting right? just like we count the value in arrays.
closed account (2UD8vCM9)
yessir
Topic archived. No new replies allowed.