Ok, so please before you get anywhere else do note I am very new to c++. Anyways, just for the hell of it I'm trying to make a kind of command line. All I want it to do is read my cin >> input; and then search for a string/int/whatever and display it. But all I have is a bunch of if() statements. Can I shorten this down?
All I want it to do is read my cin >> input; (sorry I couldn't find how to put that in the code format) and then search for a string/int/whatever and display it. But all I have is a bunch of if() statements. Anyway I can shorten this down?
Can you describe your problem by posting your current code?
Switch statements are like this (FYI ignore angle brackets):
1 2 3 4 5 6 7 8 9 10 11 12 13
switch (variable you want to check cases for)
{
case <constant expression>:
// Code here
break; // To get out of switch statement
case <constant expression>:
// Code here
break;
// Etc...
default:
// Code here
break;
}
Example of switch statements:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
int main()
{
int x;
std::cin >> x;
switch (x)
{
case 1:
std::cout << "You entered 1";
break;
case 2:
std::cout << "You entered 2";
break;
default:
std::cout << "You didn't enter 1 or 2";
break:
}
return 0;
}
@SakurasouBuster Yes I tried 1 earlier and it worked. And I know cin >> y; is looking for an int, but that's the only way I could get the program to run.