如何将两个单独输入的数字存储到一个变量中,该变量将第一个数字存储在十位,然后存储第二个数字?例如......
int num1 = 0;
int num2 = 0;
cout<<"first number: ";
cin>>num1;
cout<<"second number: ";
cin>>num2;
//now the variable both should store the number 12
int both = ?????????;
答案 0 :(得分:2)
int result = num1 * 10 + num2;
答案 1 :(得分:2)
尝试:
int both = (num1*10) + num2;
示例案例:
如果num1
为4,而num2为5:
both = (4*10) + 5; // 45
如果您想要超过2个数字,您可以选择将这些输入数字存储到数组中,然后使用for循环来获得结果:
int res = 0;
for (int i=0; i<N; i++) { // N is your number of inputs
res += num[i] * pow(10, (N-1)-i); // num is your array of numbers and pow() is a function from math library
}