如何在 c++ 中简单地正确验证?
How to simply validate correctly in c++?
我是 c++ 的新手 - 我无法验证用户输入必须是整数,我只想要基本的 - 更简单的方法,让我理解。(只是基本方式(验证用户的输入 在下面的代码之前,我怎样才能让它工作。谢谢你的帮助,你的时间。例如:如果我收到输入正数并输入字母x的提示,则表示它将显示"无效输入"。谢谢!
这是我的代码:
更新
if (cin >> x && x < 0) {
} else {
cout << "Invalid entry, Try again." << endl;
cin.clear();
while (cin.get() != 'n');
}
#include <iostream>
#include <fstream> // for file stream.
using namespace std;
int main() {
// Variables
int x, reversedNumber, remainder;
// Creating String variables and setting them to empty string.
string even = "", odd = "";
// Creating a file.
ofstream out;
out.open("outDataFile.txt");
// creating a character variable
// for the user input if they want to use the program again.
char ch;
do {
// Even number.
even = "";
// Odd number.
odd = "";
// Reversed number
reversedNumber = 0;
// Prompt the user to enter a positive integer.
cout << "nEnter a positive integer and press <Enter> ";
// Validate user input.
while (cin >> x || x < 1) {
// Clear out the cin results.
cin.clear();
// Display user that it is not a positive integer.
cout << "Invalid entry, Try again. ";
}
// Display Results to the screen
cout << "the original number is " << x << "n";
// Display results in the text file.
out << "the original number is " << x << "n";
// Display number reversed.
cout << "the number reversed ";
// Display number reversed in text file.
out << "the number reversed ";
//Reversing the integer.
while (x != 0) {
remainder = x % 10;
reversedNumber = reversedNumber * 10 + remainder;
// Display on screen
cout << remainder << " ";
// Display in text file.
out << remainder << " ";
x /= 10;
}
// Display the results on screen and in the text file.
cout << "n";
out << "n";
// Reading the reverse numbers result.
while (reversedNumber != 0) {
remainder = reversedNumber % 10;
// Checking if the number is even or odd.
if (remainder % 2 == 0) {
// even = even * 10 + remainder;
} else {
// odd = odd * 10 + remainder;
}
reversedNumber /= 10;
}
//Displaying the even numbers.
if (even != "") {
cout << "the even digits are " << even << "n";
out << "the even digits are " << even << "n";
}
// If it is not even then display..
else {
cout << "There are no even digits n";
out << "There are no even digits n";
}
//Display the odd results.
if (odd != "") {
cout << "the odd digits are " << odd << "n";
out << "the odd digits are " << odd << "n";
}
// If its not odd then display.
else {
cout << "There are no odd digits n";
out << "There are no odd digits n";
}
// just a divider to divide the results inside text file.
out << "----------------- n";
// Prompt the user if they want to use the program again.
cout << "nDo you like to continue/repeat? (Y/N):";
// get the input from user.
cin >> ch;
if ((ch == 'Y') || (ch == 'y')) {
} else {
cout << "nGoodbye!" << endl;
}
} while (ch == 'y' || ch == 'Y');
// close the text file.
out.close();
return 0;
}
您可以从cin
中读取一个字符串并检查每个字符以确保在传递给isdigit()
时它返回 true。
std::string x;
std::cin >> x;
for(char c : x){ //for each char c in string x
if(!isdigit(c)) //Invalid
}
相关文章:
- 在c++中用vector填充一个简单的动态数组
- 正在尝试了解输入验证循环
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的简单if-else语句是如何无法访问的代码
- 使用简单类型列表实现的指数编译时间.为什么
- 如何在C++中检查2D数组中负值的输入验证
- 如何在BST的这个简单递归实现中消除警告
- 一种在C++中读取TXT配置文件的简单方法
- LibGit2 SSH身份验证失败
- 简单的 HTML 验证器
- 如何在 c++ 中简单地正确验证?
- 如何使用GSOAP初始化服务器上下文以启用简单身份验证(仅服务器身份验证)
- 简单的密码验证
- 如何仅验证int类型的输入..以一种非常简单但有效的方式?在c++中
- 验证给定C++编译器是否将源代码读取为 C++11 的快速简单代码是什么?
- 验证 IP 地址是否已颁发的简单方法
- 如何简单地在C++中进行输入验证
- 函数验证输入c++简单
- 简单的代码验证
- 最简单的C++示例来验证我的编译器是否符合 c++14