Making an if statement to display certain information for certain input

I am a beginner... and me and my friend are making little "manuals" based on a fantasy novel we are writing. He is doing it in java while I am doing it in C++. Enough backstory, here is the issue:

So, all I need to know is the proper way to do an if statement to use to answer questions and display info based on user input. Based on this source code, what would you suggest?

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
29
30
//The Magical Manual Main file
#include <iostream>
//Namespaces
using namespace std;

int main()
{
    string input = "";
    cout << "Mystrian Magic Manual 1.0" << endl;
	cout << "Programmed by James Bryce in C++..." << endl;
	cout << "the co-author of the Mystrian Fate series!" << endl;
	getline (cin, input);
	cout << "Welcome!\n" << endl;
        cout << "AI boot complete!\n" << endl;
	cout << "Hello! I am a digital copy of Xhalite," << endl;
	cout << "the White Wizard of the element: Earth." << endl;
	cout << "Pick a topic:\n" << endl;
        cout << "a) Magical Animals" << endl;
        cout << "b) Spell Lists" << endl;
        cout << "c) History" << endl;
        cout << "d) Alchemy" << endl;
        cout << "e) Herblore" << endl;
        cout << "f) Locations" << endl;
        cout << "g) Religions" << endl;
        cout << "h) People/Races" << endl;
        cout << "i) Exit" <<endl;
        getline (cin, input);

    return 0;
}


Also there will be many sub sections and a lot of information, but if you tell me how to do an if statement for any one of these I should be able to get the hang of it and figure out the rest.

And I need to be able to have a way for user to exit or return to beginning. So information on that would be helpful! Thanks!
I'd recommend a switch case, it will automatically cast the chars to ints so there is nothing to worry about there.
1
2
3
4
5
6
7
8
9
10
switch(input)
{
    case 'a': /*Do Something*/; break;
    case 'b': /*Do Something Else*/; break;
    case 'c': /*I'd like to suggest using functions to keep the code looking neat*/; break;
    case 'd': /*But if you don't know how then you can always come back and do it later*/; break;
    case 'e': /*It is more proper to use functions*/; break;
    
     default: /*This should repeat the menu*/; break;
}
Last edited on
Yes, Computergeek01 is right, a switch statement is the best way to go. Correct me if I am wrong, but string variables are not the same as a simple char variable.
1
2
3
4
switch(input.c_str()[0]) // <--- This is the change
{
       // All the case statements
}

What this is doing is turning the string into an array of chars, and then getting the first value of that array. BTW i did somthing kind of like that a while back and you might want to try
1
2
3
4
switch(tolower(input.c_str()[0])) // <--- This allows for capital letters too
{
       // All the case statements
}

Aside!

Do you know how load files and search strings??

Andy
Ah thank you guys, this'll be great for future reference! Unfortunately that program became a buggy mess after two little errors that caused a whole chain of errors. Something about "crosses intilization of 'int choice'"?

Anyway I sort of gave up, I am actually writing a true 3D RPG game based on that information!

I really just thought I'd throw that together instead of writing a game doc in Office, it failed somehow.
Last edited on
A tiny, retrospective correction to NanoBytes correction to Computergeek01...

Instead of

switch(input.c_str()[0])

lazy coders can write the following instead

switch(input[0])

as std::string defines operator[]() for accessing a char by index.
Last edited on
Hmm, I didnt know that, thanks
Topic archived. No new replies allowed.