是否可以使整数仅收到一个单个数字而不是两个接收输入

Is it possible to make an integer only recieve input for one single number instead of two?

本文关键字:两个 输入 数字 单个 整数 可以使 一个 是否      更新时间:2023-10-16

我需要能够在同一行上输入一系列整数,我不能使它们成为字符,因为我需要添加整数。

#include <iostream>
#include<string>
using namespace std;
int main(){
int a; //1
int b;     //2
bool sit = true;
cout << "Enter a ten digit date" <<endl;
cin>>a>>b;
cout<<a<<b<<endl;
  if (sit == true){
    b = b+a;
    cout << b<<endl;
  }
    return 0;
}

所以如果我输入

12

它等到我输入两个数字,然后添加两个数字。

12
45
57

我该怎么办?谢谢

我不太了解您的问题,但这是您阅读一系列整数的方式。

int x;
cin>>x;
//keep reading x until 0 is entered
while(x!=0)
{
//do whatever you need here
cin>>x;
}

如果您知道必须阅读多少个整数,则可以使用循环。

for (int i=0;i<n;i++)
{
 cin>>number;
}