多输入失败C ++

时间:2018-02-16 19:23:08

标签: c++ input cin

我的代码存在问题,我需要变量" y"存储a的先前值,但我找不到解决方案

#include <iostream>
using namespace std;

int main() {

int a;
int b;
int x = 0;
int y;

cout << "please enter two numbers then press <Enter>" << endl;
cin >> a && y;
cin >> b;
while (a <= b){
    cout << a << endl;
    a++;
    x++;
}
cout << "There were " << x << " numbers between " << y << " and " << b << endl;

return 0;

}

1 个答案:

答案 0 :(得分:0)

你需要这样的东西:

cout << "please enter two numbers then press <Enter>" << endl;
cin >> a >> y;

请注意,这两个值应以空格分隔。

从代码中看,您输入的是3个变量:

  cin >> a;
  cin >> y;
  cin >> b;

如果您想将副本放入y,请尝试以下方法:

  cin >> a; y = a;
  cin >> b;