Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <iostream>
#include <string.h>
#include <string>
#include <iomanip>
#include <cctype>
#include <ctype.h>
#include <fstream>
#include <cstdlib>

using namespace std;

void mainmenu(){
cout << setw(45)  << "[I] " <<"Student Information System"     << " [I]" << endl;
cout << setw(43)  << "|"    <<"   What do you want to do?    " << "|"    << endl;
cout << setw(43)  << "|"    <<"                              " << "|"    << endl;
cout << setw(43)  << "|"    <<"   1. Add New Record          " << "|"    << endl;
cout << setw(43)  << "|"    <<"   2. Search Record           " << "|"    << endl;
cout << setw(43)  << "|"    <<"   3. Display All Records     " << "|"    << endl;
cout << setw(43)  << "|"    <<"   4. Display Specific Record " << "|"    << endl;
cout << setw(43)  << "|"    <<"   5. Delete Record           " << "|"    << endl;
cout << setw(43)  << "|"    <<"   6. Exit                    " << "|"    << endl;
cout << setw(74)  <<   "********************************"                << endl;

cout << "\t\t\t\t\t    Please type your selection:";
}

void border(){
cout <<"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////"<< endl;
}


struct student{
	int studentid,yearlevel;
	string fullname,birthday,address,gender,course;
};

int main(){
	int select;
	string u;
	bool system=false;
	
do{
	
	start:
	border();
	mainmenu();
	
	cin >> select;

	
	switch (select){
	case 1: {
		cout<<"Ran case 1\n";
		break;
	}
	
	case 2: {
		cout<<"Ran case 2\n";
		break;
	}
	
	case 3: {
		cout<<"Ran case 3\n";
		break;
	}
	
	case 4: {
		cout<<"Ran case 4\n";
		break;
	}
	
	case 5: {
		cout<<"Ran case 5\n";
		break;
	}
	
	case 6:{
		cout<<"Ran case 6\n";
		cout<<"Exiting Program Have a Good Day!";
		system = true;
		break;
	}
	default:{
		cout <<"\nInvalid Input... \n Try Again...\n";
		break;
	}
	} //end switch
}while(!system);

return 0;
}	// end main


I want to make a trap for wrong inputs and condition
thus if user input a char even though the choices are only 1-6 it would output "wrong input try again" but it goes to a loop.

What I have tried:

I have tried searching for what I did wrong in my conditions nothing had worked.
Posted
Updated 24-Nov-22 22:30pm
v3
Comments
jeron1 23-Nov-22 10:20am    
How about you just check the integer range, isdigit() expects a character that is casted to an int you don't have characters. I am not going to speak to your use of goto's and the structure of your logic, as I am sure others will.

if ((select >= 1) && (select <= 6))
MrMonsieur 23-Nov-22 10:35am    
Hello would to know how could I have done this differently currently studying c++ would like to your thoughts
Richard Deeming 23-Nov-22 11:02am    
Your title says you are checking "if var is <= num and isdigit var"; but the code you've shown is checking "if var is <= num and NOT isdigit var". So the first thing you need to do is work out which condition you want to use.

There are several ways, one being something along the lines of this.

int entry = 0;
bool quit = false;

do
{
	// draw menu

	// get entry
	
	switch (entry)
	{
	  case 1:
	   // do stuff for 1 
	  break;

	  case 2:
	  // do stuff for 2
	   break;

	  .
   	  .
 	  .
	  case 6:
	  // do stuff for 6 (the exit condition)
	  quit = true;
	   break;

	  default:	// this happens for none of the above
	  // do stuff for invalid entry	
	  break;	

	}
while (!quit);
 
Share this answer
 
If cin expects an int but gets a letter you would have to clear the buffer and clear the error flag. It would be easier to read the complete line with getline() and then check the string.
Note: <string> should be included as includes and not <string.h>.
C++
long select;
// cin >> select;
char *endptr = nullptr;
std::string userinput;

do {
	std::getline(std::cin, userinput);
	select = std::strtol(userinput.c_str(), &endptr, 10);
	if (endptr == userinput) 
		puts("Not a valid number! Try again");
} while (endptr == userinput);

switch (select) {
case 1:  // ...
default:
	cout << "\nInvalid Input. Try Again....\n";
}
 
Share this answer
 
v2
Comments
k5054 23-Nov-22 11:12am    
As I understand this: https://en.cppreference.com/w/cpp/io/basic_istream/getline#:~:text=count%2D1,is%20executed). you do not need to subtract 1 for the length of the input buffer. Still, better safe that sorry. But better yet, why not use the std::string version of getline() and not worry about buffer overflows?
merano99 23-Nov-22 12:26pm    
You're right, according to the documentary, count is fine without -1. With std::string you would have to ensure the capacity and write it directly into the buffer. And then you would have to specify this length. Methods that could automatically increase the string are not used with getline(), in my opinion. I would not want to read multiple lines here.
k5054 23-Nov-22 14:47pm    
when using std:string you can just call getline(std::cin, mystring). See the example at cppreference.com: https://en.cppreference.com/w/cpp/string/basic_string/getline#:~:text=%27)%3B-,Example,-The%20following%20example
merano99 23-Nov-22 17:45pm    
OK. I guess I had forgotten about this version. Makes sense, of course. Have improved it.
A simple change to character input would do what you want:
C++
	char select; // input a single character

// ...
	
	std::cin >> select;
	
	switch (select){
	case '1':
        // action this case
        break;
	case '2':
        // action this case
        break;
// ...

	default:
        std::cout << "Error: \'" << select << "\' is not a valid choice, please try again" << std::endl; 
        break;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900