Need help with my text game C++ program

Hello everyone, this is my first time creating a topic on this forum and I could really use the help. The problem with this code is the execution. Meaning, the result that I get is wrong(as far I know, the compiler gave me no errors or warnings). The "error" starts when the program asks the user to input the name of the race they wish to be. The problem is that it accepts anything(integers, literals, etc) when I specifically wrote to only accept any of the choices from my list and output an error message whenever they input the wrong thing. I will be taking any suggestions. In case you want to know, the program that I'm using for this is CodeBlocks. Also, please comment on my coding style. If you see any room for improvement in that department, please don't hesitate to comment.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
using namespace std;

int main()
{
string fullName;
char gender = 'M' && 'F';
string race;
race = "Human" && "Dwarf" && "Elf" && "Titan" && "Dragon" && "Giant";
string origin;

cout << "What's your name young adventurer? ";
getline(cin, fullName);
cout << endl;

cout << "Hello " << fullName << ", and welcome to the land of Alstar. ";
cout << "\n" << endl;

cout << "Before we continue, I'm going to need you to answer ";
cout << "several personal questions.";
cout << " So without further delay, let's get started. \n" << endl;

    system("pause");

cout << "\nFirst off, are you male or female? ";
cout << "Enter either M or F: ";
cin >> gender;

    if(gender == 'M')
     {
       cout << "\nNow it's time to pick your race.\n";
     }

      else if(gender == 'F')
       {
        cout << "\nNow it's time to pick your race.\n";
       }

       else if((gender != 'M') || (gender != 'F'))
  {
     do
     {
      cerr << "\nError, invalid entry.";
      cerr << " You must type either 'M' or 'F' in order to continue. ";
      cin >> gender;
     }
     while((gender != 'M')&&(gender != 'F')&&(gender != 'm')&&
           (gender != 'f'));
      cout << "\nNow it's time to pick your race.\n";
  }

    system("pause");


cout << "\nFrom this list, please input the name of the race you wish";
cout << " to be.\n\n";
cout << "1.Human" << endl;
cout << "2.Dwarf" << endl;
cout << "3.Elf" << endl;
cout << "4.Titan" << endl;
cout << "5.Dragon" << endl;
cout << "6.Giant" << endl;
cin >> race;

     if(race == "Human" || "Dwarf" || "Elf" || "Titan" || "Dragon" ||
        "Giant")
    {
       cout << "\nYou have chosen to be a " << race << ".";
       cout << " Now it's time to pick your birth of origin.\n";
    }

     else if((race != "Human") || (race != "Dwarf") || (race != "Elf")
        || (race != "Titan") || (race != "Dragon") ||(race != "Giant"))
      {
           do
        {

        cerr << "\nError, invalid entry.";
        cerr << " You must pick and input the name of the race that";
        cerr << " appears on the list.";
        cin >> race;

        }
        while((race != "Human")&&(race != "Dwarf")&&(race != "Elf")&&
              (race != "Titan")&&(race != "Dragon")&&(race != "Giant"));

        cout << "\nWere halfway there. Now it's time to pick your";
        cout << " birth of origin.";

       }

    system("pause");
}
Last edited on
The lines

1
2
3
while((gender != 'M')&&(gender != 'F')&&(gender != 'm')&&
           (gender != 'f'));
      cout << "\nNow it's time to pick your race.\n";


need to be

1
2
3
while((gender != 'M')||(gender != 'F')||(gender != 'm')||
           (gender != 'f'));
      cout << "\nNow it's time to pick your race.\n";


similarly for the parts concerning the players race :)
Last edited on
Thanks for the quick reply. I'll try it out.
edit- Nope, it only made it worse(it needs && in order for the retry loop to work).
Last edited on
really, how was it worse? :P
Let me try to compile and run it. Welcome to the forums btw :)
OK,
1
2
if(race == "Human" || "Dwarf" || "Elf" || "Titan" || "Dragon" ||
        "Giant")


needs to be

 
if(race == "Human" || race =="Dwarf" || race=="Elf" || race=="Titan" || race=="Dragon" || race=="Giant")


and BTW, this line char gender = 'M' && 'F'; makes no sense. It needs only be char gender;
Last edited on
in my situation specifically, if I don't use && then the retry loop doesn't work. It will just keeping giving me the error message that I placed in the do- while loop and not move on to the next line which is "Now it's time to pick your race".
Last edited on
when I wrote char gender = 'M' && 'F', what I thought I was doing was assigning two "values" to one variable so then when I create my if statement, gender could equal to only M or F(meaning if the user inputs any of those two character literals, it would be correct).

you said it makes no sense. If what you're saying is true, should I leave like char gender; or char gender = 'M' || 'F'.

edit-and for some reason you repeat the same statement(the char statement). Sorry I don't see the difference.
Last edited on
It only needs to be char gender;. '&&' is a boolean operator, so it doesn't make sense to assign it to a char :)

EDIT: you can only assign one value to a char at any given time. The '&&' operator gives you a truth-value, i.e. it returns true if both sides of it are true. For example true && false yields false, whereas true && true yields true. When I repeated the same statement before, it was just a typo, I corrected it later on.
Last edited on
interesting, It seems that your suggestion might have worked. I will get back to you momentarily.
Thank you for your input. I was finally able to solve it. Turns out you were right to suggest making each string equal to race separately. You still haven't commentated on my coding style though.
Topic archived. No new replies allowed.