【发布时间】:2018-10-16 20:59:37
【问题描述】:
我是 CS 专业的一年级学生。今天在我们的实验室中,我们必须调试一些代码并使其工作。结果如下。
#include <iostream>
using namespace std;
int main() {
int x = 3, y;
char myanswer;
int val= 1;
int num;
y = x;
cout << "y is set to: " << y << endl;
bool again = true;
int ans;
while (again) {
cout << "Please input a number: ";
cin >> y;
if (x > y)
cout << "X is greater than Y\n";
else {
cout << "X is less than Y" << endl;
cout << "would you like to input another number?" << endl;
cin >> ans;
if (ans != 1)
break;
}
cout << "would you like to input another number ?" << endl;
cin >> ans;
if (ans != 1)
again = false;
}
for (x = 0; x < 10; x++)
cout << x << endl;
cout << "What number would you like to find the factorial for? " << endl;
cin >> num;
cout << num;
for (int x = num; x > 0; x--) {
val *= x;
}
cout << "Are you enjoying cs161? (y or n) " << endl;
cin >> myanswer;
if (myanswer == 'y')
cout << "Yay!" << endl;
else
cout << "I hope you will soon!" << endl;
return 0;
}
在关于阶乘的 cout 之后,cin 不起作用,用户不再能够输入输入。到目前为止,我的实验室 ta 和朋友还没有找到问题所在。该代码已在我学校的工程服务器和我的本地计算机上编译和执行。在这两个错误仍然存在。
【问题讨论】:
-
gdb 显示发生了什么?
-
我无法复制错误。除了每次我被问到是否要输入一个新数字时,我都会连续两次被问到这个问题。我必须同意 pm100。你为阶乘的 num 输入了什么数字?
-
我没有提示输入数字,这就是我遇到的问题
-
@jackattack825 你在运行什么编译器?当然,您的代码中存在逻辑错误,我可以轻松指出。但是在我的终端中运行它并用 g++ 编译我可以一直到你的程序结束。
-
使用 g++,我从下面这个人那里找出了错误,我输入 y/n 而不是 1/0 来回答以前的 cin 以某种方式破坏了程序的其余部分