Search and Cout

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?
Last edited on
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?
Instead of using if statements how about switch statements?

If statements are like this:
1
2
3
4
5
6
7
8
9
if (condition)
{
}
else if (condition)
{
}
else
{
}


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;
}


I would make your command line like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
void runCommand(std::string cmd)
{
    switch (cmd)
    {
        case insertHere:
          // Code
          break;
        case insertHere2:
          // Code
          break;
        // Etc...
        default:
          // Code
          break;
    }
}
int main()
{
    std::string cmd;
    for (;;)
    {
        getline(std::cin, cmd);
        runCommand(cmd);
        cmd.clear();
    }
}
Last edited on
ok @Boost Lexical Cast my code for something related just a new project is: #include
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<iostream>       //basic code
#include <thread>         //sleep_for
#include <chrono>         //seconds
#include <windows.h>      //Windows Functions
#include <string>         //string

using namespace std;

int main() {

    string y;

	cout << "Welcome: ";
	cin >> y;

	switch(y) {
case Scott:
    cout << "Welcome Scott!!";
    break;
	}

}


but in my build log I get:
C:\Users\Scott\Desktop\New folder\Testing\main.cpp|18|error: the value of 'Scott' is not usable in a constant expression|

Last edited on
What C++ compiler are you using?
@sakurasouBusters CodeBlocks
Well,
const int Scott = 1;

Put it before the function main().
@SakurasouBusters so even to get it to run I had to edit the code a little, and now I have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main() {

    int y;
    const int Scott = 1;

	cout << "Welcome: ";
	cin >> y;

	switch(y) {
        case 1:
        cout << "Welcome Scott!!";
        break;
	}

}

but when the cmd pops up and I enter "Scott" I just get a return(0) output.
Well you are entering a string. cin >> y; requires an int.
Did you try entering 1?
@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.
@Scoot445

In a switch statement the expression/condition has to be a constant expression. So it cannot be a variable.

If you were making a command line then for each of the "cases" would be a command.

Like this:
1
2
3
4
5
6
7
8
9
switch (cmd)
{
    case "exit":
      // Code for command exit
    case "print letter a":
      std::cout << "a";
    default:
      std::cout << "Invalid command";
}


To use a variable like "Scott" when declaring the variable you could use the keyword const.
const std::string Scott = "example";
Last edited on
Like this :

This does not compile.
@SakurasouBusters

Obviously it does not... it's a snippet of code. It's an example.

Would you like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
int main()
{
    std::string cmd;
    std::cout << "Command: ";
    getline(std::cin, cmd);
    switch (cmd)
    {
        case "exit":
          // Code for command exit
        case "print letter a":
          std::cout << "a";
        default:
          std::cout << "Invalid command";
    }
    return 0;
}
Is it a dream or I am able to use c-string literal in switch-constant expression now?
@Scoot445

Above you posted this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<iostream>       //basic code
#include <thread>         //sleep_for
#include <chrono>         //seconds
#include <windows.h>      //Windows Functions
#include <string>         //string

using namespace std;

int main() {

    string y;

	cout << "Welcome: ";
	cin >> y;

	switch(y) {
case Scott:
    cout << "Welcome Scott!!";
    break;
	}

}

Fixed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<iostream>       //basic code
#include <thread>         //sleep_for
#include <chrono>         //seconds
#include <windows.h>      //Windows Functions
#include <string>         //string

using namespace std;

int main() {

        string y;

	cout << "Welcome: ";
	getline(cin, y); // cin stops getting input when it encounters whitespace

	switch(y) {
case "Scott": // A literal like the string literal "Scott" should
                       // be enclosed in quotation marks
    cout << "Welcome Scott!!";
    break;
	}

}
@SakurasouBusters

My bad. Sorry instead of strings use integars to represent the commands.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
int main()
{
    int _cmd;
    std::string cmd;
    std::cout << "Command: ";
    getline(std::cin, cmd);
    if (cmd == "insertCommand")
    {
        _cmd = insertNum;
    }
    // More else ifs and etc.
    switch (_cmd)
    {
        case insertNum:
          // Code for command exit
        case insertNum:
          std::cout << "a";
        default:
          std::cout << "Invalid command";
    }
    return 0;
}


But this is more work than before so you should actually just use if statements.

Silly me...

Probably should go back to if statements.
1
2
3
4
5
6
if ("command")
// Code
else if ("command2")
// Code
else
std::cout << "Invalid";

Last edited on
@boost Lexical Cast, thanks so much for replying, I'm probably just gonna go back to if statements that call functions to do the work, thanks :)
1
2
3
4
5
6
7
8
9
10
11
12
void run () {
    if (input == "Readme") {cout << readme << endl; start();}
        if (input == "VapeNation") {cout << VapeNation << endl; start();}
            if (input == "Egg") {cout << Egg << endl; start();}
                if (input == "Umbrella") {cout << Umbrella << endl; start();}
                    if (input == "Shutdown") {shutdown();}
                        if (input == "logOut") {logOut();}
                            if (input == "Reboot") {cout << reboot();}
                                if (input == "FlipACoin") {FAC(); start();}
                                    if (input == "Bank") {bank(); start();}

}
http://www.cplusplus.com/forum/general/198184/#msg949679
Topic archived. No new replies allowed.