如果用户试图在C++中输入意外类型的信息,如何处理异常
How to handle the exceptions if the user tries to enter information of the unexpected type in C++
我正在编写一个程序,如果用户试图输入,我应该处理异常意外类型的信息(例如,如果输入的是数字,而所需的是字符串,则要求他重新输入有效的输入(。
问题是,我的代码适用于所有(int(类型(比如,如果用户在(int(文件中输入字符串,则会给出重新输入的消息(。但是,它不适用于(字符串(类型。
这是我的代码:
void TA::AddnewTA(TA TA_Arr[], TA New_TA_Arr[], int &TA_Arr_Num, int &New_TA_Arr_Count)
{
// Prompt the user to enter the TA ID
while (true)
{
cout << "Please Enter TA ID:n";
cin >> New_TA_Arr[New_TA_Arr_Count].Student_ID;
while (1)
{
// Checking if the ID entered is of an unexpected type (handle the ID exception)
// Using (cin.ignore) to ignore data entered to the end of line along with clean
if (cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
cout << "Unexpected Type is entered, Please Enter once more a valid ID input: " << endl;
cin >> New_TA_Arr[New_TA_Arr_Count].Student_ID;
}
// If the type is correct, break & go check if the ID entered is duplicated or not
if (!cin.fail())
break;
}
// Use Duplicate_Student_ID Function to check on the ID existence
// If the ID is already there, then request from the user to enter another ID
if (Duplicate_Student_ID(TA_Arr, New_TA_Arr, TA_Arr_Num, New_TA_Arr_Count, New_TA_Arr[New_TA_Arr_Count].Student_ID) == true)
{
cout << "Unfortunately, Your Entered ID is Already Exist. Please Enter Another Valid IDn";
continue;
}
// If it is a New ID, then complete to the First Name Data!!
else {break;}
}
// Prompt the user to enter the TA First Name
cout << "Please Enter TA First Name: ";
cin >> New_TA_Arr[New_TA_Arr_Count].First_N;
while (1)
{
// Checking if the First Name entered is of an unexpected type (handle the First Name exception)
// Using (cin.ignore) to ignore data entered to the end of line along with clean
if (cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
cout << "Unexpected Type is entered, Please Enter once more a valid First Name input:" << endl;
cin >> New_TA_Arr[New_TA_Arr_Count].First_N;
}
// If the type is correct, break & go to Last Name Entry Data
if (!cin.fail())
break;
}
// Prompt the user to enter the TA Last Name
cout << "Please Enter TA Last Name: ";
cin >> New_TA_Arr[New_TA_Arr_Count].Last_N;
while (1)
{
// Checking if the Last Name entered is of an unexpected type (handle the Last Name exception)
// Using (cin.ignore) to ignore data entered to the end of line along with clean
if (cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
cout << "Unexpected Type is entered, Please Enter once more a valid Last Name input:" << endl;
cin >> New_TA_Arr[New_TA_Arr_Count].Last_N;
}
// If the type is correct, break & go to Start Hire year Entry Data
if (!cin.fail())
break;
}
// Prompt the user to enter the TA Start Year of Hire
cout << "Please Enter TA Start Hire year: ";
cin >> New_TA_Arr[New_TA_Arr_Count].Start_Hire_Year;
while (1)
{
// Checking if the Start Hire year entered is of an unexpected type (handle the Start Hire year exception)
// Using (cin.ignore) to ignore data entered to the end of line along with clean
if (cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
cout << "Unexpected Type is entered, Please Enter once more a valid Start Hire year input:" << endl;
cin >> New_TA_Arr[New_TA_Arr_Count].Start_Hire_Year;
}
// If the type is correct, break & go to End Hire year Entry Data
if (!cin.fail())
break;
}
// Prompt the user to enter the TA Class (Grad/Alum)
cout << "Please Enter TA Class (Grad/Alum): ";
cin >> New_TA_Arr[New_TA_Arr_Count].Class;
while (1)
{
// Checking if the TA Class entered is of an unexpected type (handle the Class exception)
// Using (cin.ignore) to ignore data entered to the end of line along with clean
if (cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
cout << "Unexpected Type is entered, Please Enter once more a valid Class input:" << endl;
cin >> New_TA_Arr[New_TA_Arr_Count].Class;
}
// If the type is correct, break & go to TA Working Hours Entry Data
if (!cin.fail())
break;
}
// Prompt the user to enter the TA Working Hours
cout << "Please Enter TA Working Hours: ";
cin >> New_TA_Arr[New_TA_Arr_Count].Working_H_Num;
while (1)
{
// Checking if the TA Working Hours entered is of an unexpected type (handle the Working Hours exception)
// Using (cin.ignore) to ignore data entered to the end of line along with clean
if (cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
cout << "Unexpected Type is entered, Please Enter once more a valid Working Hours input:" << endl;
cin >> New_TA_Arr[New_TA_Arr_Count].Working_H_Num;
}
// If the type is correct, break & call the function to print all attributes to the main function in Test Driver
if (!cin.fail())
break;
}
// Count Up the New TA Array Counter to add the New Added TA Info to the TAs.txt
New_TA_Arr_Count++;
}
所以,ID&Working_H_Num正在工作,但名字、姓氏和类不起作用!!请帮忙!!
你试过if(isalpha(吗?如果你通过一个类似的程序,你会在(http://www.cplusplus.com/reference/cctype/isalpha/)你可以得到我认为你想要的。作为一个例子,我写了一个小东西来演示isalpha。祝你好运。
#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;
main()
{
string str;
cout <<"enter a string or int"<<endl;
cin >>str;
cout<<endl;
while (str[0])
{
if (isalpha(str[0])){
cout<<"its a string";
return 0;
}
else{
cout << "its a number";
return 0;
}
}
return 0;
}
相关文章:
- 警告处理为错误这里有什么问题
- 正在查找文档以获得PS4平台的C++中的设备信息
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 使用流处理接收到的数据
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- VisualC++ 2010 有没有办法找出有关未处理异常错误的更多详细信息
- 编译器在遇到提取或插入运算符时处理信息(字符串、操纵器等)的顺序是什么?
- 如何处理在 c++ 中读取时从文件中获取的信息
- 堆栈异常处理和析构函数展开.如何使用这些信息
- C++ partition_point处理索引信息
- 如何对文本文件中的信息进行哈希处理
- 如何将提取的光栅信息转换为可由opencv库处理的格式
- 在LINUX中处理工作集信息
- 如何处理依赖扩展信息的无扩展源文件和工具
- 如何从矢量异常处理程序获得更详细的异常信息
- 如何使用set_unexpected(my_unexpected())获取有关未处理异常的信息