【发布时间】:2020-04-30 01:34:39
【问题描述】:
所以我仍然是这方面的初学者并且仍在练习。基本上我需要制作一个程序,继续要求用户输入除 5 以外的任何数字,直到用户输入数字 5。
我已经完成了,但我不知道如何检查用户是否输入了重复的数字。例如: 1 2 3 3 - 程序应该结束
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
int main() {
cout << setw(15) << setfill('*') << "*" << endl;
cout << "Number 5" << endl;
cout << setw(15) << setfill('*') << "*" << endl;
int num;
cout << "Enter a number: ";
cin >> num;
if (num == 5) {
cout << "\nWhy did you enter 5? :) " << endl;
_getch();
exit(0);
}
for (int i = 1; i < 10;i++) {
cin >> num;
if (num == 5) {
cout << "\nWhy did you enter 5? :) " << endl;
_getch();
exit(0);
}
}
cout << "Wow, you're more patient then I am, you win." << endl;
_getch();
}
【问题讨论】:
-
你显示的程序有什么问题?请花一些时间阅读有关how to ask good questions 和this question checklist 的信息。
-
嗨 Raitik,我不明白你的问题。如果明确要求用户不要输入
5,他将如何输入?循环应该在 5 次迭代后结束吗?那么输入5个数字后呢?然后只需检查计数器,即i的值 -
cplusplus.com/forum/articles/12974 对不起,我之前没有添加它,我正在做 While(user == gullible),我被困在它的最后一部分。
-
混合 conio.h 和 std::cin 可能会引起麻烦。 conio.h 也来自 80 年代,专为 MS-DOS 文本模式设计,在今天不是很有用(仅适用于玩具程序)。
标签: c++ loops if-statement input output