I am trying to create a basic switch statement that you enter a number ant it tells you the number. but if you enter something other than a integer to does a loop of the code on default.(run it to see for yourself) I tried changing it to string but it says
line 16
error: switch quantity is not an integer
Here it the code
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <stdio.h>
#include <windows.h>
using namespace std;
void menu()
{
string imput;
system("CLS");
cout << "enter the number 1,2,3 or 4." << endl;
cin >> imput;
If you want to allow the user to enter as many numbers as they wish, I might consider using a while loop instead of creating additional instances of the same function.
You should look up the ascii table and understand the difference between numbers and characters. For example the number 1 is different from the character "1". You can fix this problem by taking in an Integer rather than a string.
In this specific case, you could change int imput; to char imput;
and then change case 1: to case'1': and so on. A more general-purpose solution is possible, but needs a little more code (though not a lot more).
I want to have the user enter a number(1,2,3,4) and it will run some code. if the number they entered is other than 1,2,3,4 it says something like "sorry the number you entered it invalid". But if the user enters a letter(s) the code that says "sorry the number you entered it invalid" will rerun infinitely...try it for yourself.
Thank you Chervil that did work.
there is still one problem, if they enter more than one letter it runs the default code depending on how many letters were entered...how do I fix this
You could clear any remaining characters from the input buffer after getting cin >> imput, by using cin.ignore(1000, '\n'); which will ignore up to 1000 characters, or until the newline character is found.
As an alternative viewpoint, you might look at this: