Hi guys, I created a menu() function which sets up the users name, etc. I have a string there called name, and I did put it in a public class (I know this is bad, I thought this would work in a new function though.) However it did not work. I want to take the users "name" that was entered in the menu function, and use it in subsequent function such as the mage1() or warrior1() etc. Is this possible? Mainly in line 73 is the problem. Also line 68 is just so I can use main() in the following function, had to call that one.
#include <iostream>
#include <unistd.h>
#include <cstdlib>
#include <windows.h>
#include <dos.h>
#include <string>
#include <time.h>
usingnamespace std;
// I will call all of my functions here so that they are available to quickly reference,
int mage1();
int rogue1();
int warrior1();
class names
{
public:
string name;
};
int menu(){
names enter;
cout << "Hello Adventurer, Please enter your name: " << endl;
// moving this to a public class string name; //Here is the string of user's name
cin >> enter.name; // string for users name
cout << "Hello, " << enter.name << ", Please pick a class: " << endl; // hello, etc.
Sleep(2); // wait for 2 seconds
cout << "1. Mage" << endl;
// Here are the class options
cout << "2. Rogue" << endl;
cout << "3. Warrior" << endl;
int classpick; // option that user picks for his class. //
cin >> classpick; // input of 1 2 or 3 to pick a class
switch(classpick){ // Switch for what class user picks
case 1:
mage1();
break;
case 2:
rogue1();
break;
case 3:
warrior1();
break;
default:
cout << "Not possible bro!";
Sleep(2);
menu();
}
}
int main();
int mage1() // begin the quest as a mage
{
names enter;
cout << "Hello, we know you as the most brave magician, " << enter.name << endl; // I cant get name from menu() function, what to do???
Sleep(2);
}
int rogue1() // these are being constructed
{
cout << "being built";
}
int warrior1() // being built
{
cout << "being built";
}
int main(){
names enter;
menu();
return 0;
}
Line 73 is the problem, enter.name continues to be blank!
The problem is that your names object is only valid inside the scope it's declared in, this means that the object you create in mage1(), even though it has the same name, is a brand new object whose name variable is currently empty.
Using your current program structure, you would either need to make your object in global scope, or create it by manually allocating memory, both are pretty bad ideas in my opinion. The best option IMO would be to start from scratch, and make a function that returns a string value with the player's name.
Great thinking, I had No idea about this. However, how can I make there be a function for inputting the name, then one for just outputting it? Would it use return at all?
I'm so lost, since I am new to c++. Could I do a while loop, so that once a player has entered SOMETHING into the string name, it then becomes true. And while it is true, it will only print the name, instead of ask for the name and input each time??
Here's what I got and i'm lost.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int nameprint()
{
cout << "Enter your name: " << endl;
string name;
cin >> name;
if (//something here to make the inside true, saying the user entered their name)
{
int truetest=1; // so if there is user input into name, this truetest is 1.)
}
while (truetest=1)
{
return name;
}
}
#include <iostream>
#include <string>
using std::cout; using std::cin;
using std::endl; using std::string;
int main()
{
cout << "Give me your name!: ";
string player_name;
getline(cin, player_name); // this way you can read names with spaces!
while(player_name.empty()){
cout << "That name is unacceptable! Try again!: ";
getline(cin, player_name);
}
// do other stuff
}
This would be enough for you right now, since you'll probably only need to ask for the name one time, a function just for getting the player's name seems unnecessary. This would also work if you later added a save function too. While it checks to make sure the user didn't give you an empty string, it doesn't do anything in case cin fails (you should google that).
Your other question was if a function that doesn't return a value would use a return statement at all, the answer is: Not if you don't want to. That function would look like this:
See that optional return statement? If it wasn't there the function would just return when it got to the last curly brace. In this case it makes no difference; it might even be better to skip the return statement since it's a one-statement function, but I prefer to have it there for clarity's sake when functions are larger (especially if they return in more than one location).