Click here to Skip to main content
15,902,938 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;
	cin >> select;

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

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.
Updated 24-Nov-22 22:30pm
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;

	// draw menu

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

	  case 2:
	  // do stuff for 2

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

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

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>.
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:  // ...
	cout << "\nInvalid Input. Try Again....\n";
Share this answer
k5054 23-Nov-22 11:12am    
As I understand this:,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,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:
	char select; // input a single character

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

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

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

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