如何在 c++ 中简单地正确验证?

How to simply validate correctly in c++?

本文关键字:验证 简单 c++      更新时间:2023-10-16

我是 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
}