0

I am coding a program and I should handle the exceptions if the user tries to enter information of the unexpected type (Like if the enters number and the required is a string, then required him to re-enter a valid input).

The problem is my code works for all the (int) types (like if the user enters a string in an (int) filed this gives the message to re-enter). However, it does not work for the (string) types.

Here is my code:

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 ID\n"; 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++; } 

So, ID & Working_H_Num is working, but First, Last names, and Class are not!! Please help!!

3
  • You might want to learn about functions first, there is a lot of duplicated code. Please show a minimal reproducible example. What does it mean "not working"? Does it crash? Does it skip? Or what? Did you try to debug the program? What did you see? How do you ensure that the arrays do not overflow? Commented Mar 27, 2021 at 23:16
  • @Quimby, no the code is working fine. But the problem is when I start inputting the TA data, I want to test if I put a number in the name field, for example, this will give an error message telling me that this is not the type of name, it should be a string. So this error message works when I put a string in the ID, for example, a message telling me to re-enter an appropriate number. Commented Mar 27, 2021 at 23:32
  • One method is to: prompt the user; and then read the input data as string (std::string usually). Then check the input and convert to the appropriate type if valid. If not valid tell the use what input was expected and why what they entered was incorrect. Put the above process in a loop (and/or function) and repeat until the user enters valid data, Commented Mar 27, 2021 at 23:48

1 Answer 1

1

Have you tried if (isalpha) ? If you pass it though a program like one you will find at (http://www.cplusplus.com/reference/cctype/isalpha/) you can get what I think you want. As an example I wrote a small thing to demo isalpha. Best of luck.

#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; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.