Need help with class

so i need a class that asks the user for their name and then returns "Hello<name>" so far this is what i have. the issue that im having is that it is only printing "Hello" and no asking the user for their input

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
  //.h file
#include<iostream>
using namespace std;
class returnName
{
public: 
	int name;

	int getName();
};

//returnName.cpp
#include".h file"
#include<iostream>
using namespace std;

int returnName::getName()
{
	cout << "Please enter your name" << endl;
	cin >> name;
	return name;
}

//main function in seperate cpp file
int main()
{
	returnName;
	cout<< "Hello " << name << endl;
	system("pause");
	return 0;
}
Well...
Ok so you want your class to ask the user for their name and print "Hello" followed by the user's name? Why is your getName function returning an int? How does that help you? Why is the name field an int, are you expecting the person to only enter a number for name?

In main you are using a variable (name) you have not declared. Declaring it in your class does not mean you have declared it in main. Think of your class as being something entirely seperate from your main code. What are you trying to do on line 27?

Ok so you don't really need to create a class just to ask for a person's name and display it; unless specifications require it to be that way (really dumb way to practice OOP). What you want is to declare a function that asks for and displays the person's name. Also, unless you are expecting the person to enter just a number for name, I would suggest storing the person's name in a string; see http://www.cplusplus.com/reference/string/string/string/ then have the function return this string
closed account (48T7M4Gy)
1
2
3
4
5
6
int main()
{
	returnName somebody( "Bill" ); // create a somebody, but need a constructor first?
	std::cout<< "Hello " << somebody.getName()<< std::endl; // now use somebody's name
	return 0;
}


Despite returnName being a very unpleasant name for a class the above might be useful. Think of a better name and take smacs advice.
Don't forget to include the class in main() too.
alright so i changed all the things to string but the issue im having is that my class isnt returning anything. also to answer smacs question the prof wants us to create this class for a future assignment which is stupid because this could have been done in a single function in main like you said.
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
//.h file
#include<iostream>
#include<string>
using namespace std;
class returnName
{
public: 
	string name;

	string getName();
};

//returnName.cpp
#include".h"
#include<iostream>
#include<string>
using namespace std;

string returnName::getName()
{
	cout << "Please enter your name" << endl;
	cin >> name;
	return name;
}

//main.cpp
#include ".h"
#include<iostream>
#include<string>
using namespace std;
int main()
{
	string n;
	cout << "Hello " << returnName.getName(n) << endl;
	system("pause");
	return 0;
}
returnName is a datatype just like int or bool or string
Would line 34 make sense if it were cout << "Hello " << std::string.length() << endl; ?
Topic archived. No new replies allowed.