c++ - 检查用户输入是否输入了重复的数字
问题描述
所以我仍然是这方面的初学者,还在练习。基本上我需要制作一个程序,继续要求用户输入除 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();
}
解决方案
之前的答案不符合链接文章中的要求,提问者本人似乎没有掌握:
★★ 修改程序,使其要求用户输入除被要求输入数字的次数以外的任何数字。(即在第一次迭代中“请输入除 0 以外的任何数字”和在第二次迭代中“请输入除 1 之外的任何数字”等等。当用户输入他们被要求不输入的数字时,程序必须相应地退出至。)
此变体符合:
#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";
}
推荐阅读
- android - 如何保存在内部存储中录制的音频
- model-view-controller - 如何在web api中使用join并显示两个表数据
- javascript - 带有 Javascript 的 Youtube 数据 API:直接上传视频
- android - 带有 MaterialDrawer 的汉堡包图标
- php - PHP/SQL 脚本意外停止,在调试中工作
- ruby-on-rails - 使用“find_by”和类似方法时,我的 Ruby on Rails 应用程序如何连接到数据库?
- sql - 正则表达式负前瞻不排除所需的字符串
- ios - 在图像上添加文本,例如在 WhatsApp 和 facebook 中创建状态
- scala - 如何修复 Scala (2.11) 中路径相关类型的类型不匹配错误?
- bash - 无限重复 bash 命令,但仅在前一个 bash 命令完成时