Game Project: Problem with calling out string variables from a function

So, I'm doing a RPG text based game and I need somewhere in the game for the person to choose their gender and role:

- I have a variable called int userInput that is used to store the user's choice based on the number written like:
1. Go west
2. Go east


- I have an aray called int playerInfo[2], what it does is that i ask the user whats his gender (1. Boy 2. Girl) and it is stored in playerInfo[0] like

1
2
cin >> userInput;
playerInfo[0]=userInput;

and the role (1. Warrior 2. Archer) is stored in playerInfo[1] the same way.

My problem
I need to, after the user submits their gender and role and they are stored in the array, have an ouput like:

"So, you're a *gender* and *role* is that correct?"

I've put two int variables called genderNr and roleNr to store the userInput to help me like this:

1
2
3
4
cout << " For starters, are you a boy or a girl?\n 1:Boy\n 2: Girl\n";
cin>>userInput;
playerInfo[0]= userInput; //Stores the character's gender
genderNr=userInput;


And then I wanna run it through a function that says that if genderNr == 1 then string gender='boy'. So far this is what I've done to try to make it work:


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
#include <iostream>
#include <string>

(...)
//the rest of the variables don't matter

int genderNr = 0;
int roleNr = 0;
string gender;

string genderFunc;

//Just so you can see i've declared everything

(...)

cout << "So you say you are a " << genderFunc(genderNr) ;

(...)

string genderFunc(int genderNr) {
     if (genderNr == 1) {
                       string gender='boy';
                       
                             }
                             
     return gender;
                      }


Then I get errors like:

In function `void newGameFunc()':
no match for call to `(std::string) (int&)'
In function `std::string genderFunc(int)':
`std::string genderFunc(int)' redeclared as different kind of symbol
previous declaration of `std::string genderFunc'
previous non-function declaration `std::string genderFunc'
conflicts with function declaration `std::string genderFunc(int)'
[Warning] multi-character character constant
invalid conversion from `int' to `const char*'
initializing argument 1 of `std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'

What's wrong?? Please help me as soon as possible and thank you very much for your help.
I'm guessing its because the gender-variable only exists inside the if-statement. It's out of scope when you're trying to return it. Also you need to surround string-literals with double quotes, not single. Try this:

1
2
3
4
5
6
7
8
string genderFunc(int genderNr) {
     string gender;
     if (genderNr == 1) {
        gender="boy";
     }
                             
     return gender;
}
Last edited on
still appears most of the errors mate but solved a bit
Ah, I didnt pay attention. I think the problem is that you have a string variable with the same name as your function. Try to rename the genderFunc-variable and see if it compiles
still appears

In function `void newGameFunc()':
no match for call to `(std::string) (int&)'
In function `std::string gendFunc(int)':
`std::string gendFunc(int)' redeclared as different kind of symbol
previous declaration of `std::string gendFunc'
previous non-function declaration `std::string gendFunc'
conflicts with function declaration `std::string gendFunc(int)'

idk but i keep feeling it has somethine to do with

cout << "So you say you are a " << gendFunc(genderNr);
because it's here that it doesn't compile
Last edited on
Did you rename the variable or the function?

Here's what I would change in the code you posted:

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>

(...)
//the rest of the variables don't matter

int genderNr = 0;
int roleNr = 0;
string gender;

string _genderFunc;  //what do you need this for?

//Just so you can see i've declared everything

(...)

cout << "So you say you are a " << genderFunc(genderNr) ;

(...)

string genderFunc(int genderNr) {
     string gender;
     if (genderNr == 1) {
           gender="boy";
      }
                             
     return gender;
}
Last edited on
i did, the 3 times it appears
Last edited on
SOLVED:

The problem was when declaring the func I had it like this:

string gendFunc();

and it needed to be:

string gendFunc(int);

tyvm
Ah, I didnt realize you meant to declare the function, I thought you were trying to create a new variable :P
Topic archived. No new replies allowed.