I am making a program which consists of making a menu to register and delete products from a store, I am just designing the menu with a switch, everything works fine up to there, the problem is that when I enter something other than a number as data ( letter or symbol) the console goes crazy; all the text starts to blink and it won't let me do anything (as if it was in a loop) and I have to close it.
Is there any way to avoid this? So when I enter a letter or symbol, it automatically detects it as invalid and shows me the message without the console going crazy?
By the way, I use Visual Studio.
Thanks in advance :)
#include<iostream>
#include<locale.h>
using namespace std;
int main()
{
int opc;
cout << " WELCOME TO THE STORE \"Happy Shopping\" ";
cout << endl;
cout << "\n1.- Create Order.";
cout << "\n2.- Delate Order.";
cout << "\n3.- list of orders created.";
cout << "\n4.- Exit";
cout << endl;
cout << "\n¿what do you want to do?: "; cin >> opc;
switch (opc)
{
case 1:cout << "\nCreate Order"; break;
case 2:cout << "\nDelate Order"; break;
case 3: cout << "\nlist of orders created"; break;
case 4:exit(EXIT_SUCCESS);
default:
if ((opc != 1) && (opc != 2) && (opc != 3) && (opc != 4))
{
system("cls");
cout << "the option entered is not valid, try again";
return main();
}
}
}
CodePudding user response:
Maybe don't return main?
switch (opc)
{
case 1:cout << "\nCreate Order"; break;
case 2:cout << "\nDelate Order"; break;
case 3: cout << "\nlist of orders created"; break;
case 4:exit(EXIT_SUCCESS);
default:
system("cls");
cout << "the option entered is not valid, try again";
}
CodePudding user response:
If you make opc
a string
and take input as a string, you can manually check if the string is a number like so:
cin >> opc;
if (!isdigit(opc[0]) // since you're dealing with single-digit numbers, this should be fine
{
cout << "You didn't enter a number!\n";
// Any other error handling
}
isdigit
is a function located in the cctype
header file, so you should add #include <cctype>
at the beginning of the file.
After this, you can convert opc
to an integer with stoi()
:
int opnum = stoi(opc);
// opnum is now the number, write the rest of the code such that it uses opnum
CodePudding user response:
As already said, you should not return main()
. You may use a loop by testing your input for instance (surely one of the many solutions that may exist).
Also, you do not need the portion of code below :
// you do not need the if as you are already in the case !=1,2,3, or 4
if ((opc != 1) && (opc != 2) && (opc != 3) && (opc != 4))
I provided you with an attempt, taken from your code, that may help to improve your code. There might still be some bugs but it is a good starting point
#include<iostream>
#include<locale.h>
int main()
{
int opc=0;
std::string input;
std::cout << " WELCOME TO THE STORE \"Happy Shopping\" ";
std::cout << std::endl;
// first display
std::cout << "\n1.- Create Order.";
std::cout << "\n2.- Delate Order.";
std::cout << "\n3.- list of orders created.";
std::cout << "\n4.- Exit";
std::cout << std::endl;
std::cout << "\n¿what do you want to do?: "; //cin >> opc;
std::cin >> input;
while ( input.length() > 0 )
{
// if we enter a string of more than length 1
try
{
opc = std::stoi( input );
}
catch (...)
{
// mainly if the converted argument is not a number =>std::invalid_argument
std::cout << "invalid value " << opc << std::endl;
}
std::cout << "you entered the value " << opc << std::endl;
switch (opc)
{
case 1:
std::cout << "\nCreate Order";
break;
case 2:
std::cout << "\nDelate Order";
break;
case 3:
std::cout << "\nlist of orders created";
break;
case 4:
exit(EXIT_SUCCESS);
default:
// you do not need the if as you are already in the case !=1,2,3, or 4
//if ((opc != 1) && (opc != 2) && (opc != 3) && (opc != 4))
//{
system("cls");
// if you type things other than
std::cout << "\n1.- Create Order.";
std::cout << "\n2.- Delate Order.";
std::cout << "\n3.- list of orders created.";
std::cout << "\n4.- Exit";
std::cout << std::endl;
std::cout << "\n¿what do you want to do?: "; //cin >> opc;
}
std::cin >> input;
}
}