gpt4 book ai didi

c++ - 检查用户输入是否输入了重复的数字

转载 作者:行者123 更新时间:2023-12-02 10:07:04 26 4
gpt4 key购买 nike

所以我仍然是一个初学者,并且仍然在练习。基本上,我需要制作一个程序,继续要求用户输入除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();

}

最佳答案

先前的答案不符合链接文章中的要求,而查询者本人似乎并不了解:

★★ Modify the program so that it asks the user to enter any number other than the number equal to the number of times they've been asked to enter a number. (i.e on the first iteration "Please enter any number other than 0" and on the second iteration "Please enter any number other than 1"m etc. etc. The program must behave accordingly exiting when the user enters the number they were asked not to.)



该变体符合:
#include <iostream>
using namespace std;

int main()
{
for (int i = 0; i < 10; i++)
{
cout <<"Please enter any number other than " <<i <<": ";
int num;
cin >>num;
if (num == i)
return cout <<"Hey! you weren't supposed to enter " <<i <<"!\n", 0;
}
cout <<"Wow, you're more patient then I am, you win.\n";
}

关于c++ - 检查用户输入是否输入了重复的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59714916/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com