Just a quick question

Okay I have fined like 99% of my code but I am getting some kind of return error.
error C4716: 'personType::isFirstEqual' : must return a value
I can't seem to figure it out if you all could help me that would be great!
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//PersonType.h
 #include <string>

using namespace std;

class personType
{
public:
	void print() const;
	//Function to output the first name and last name
	//in the form firstName lastName.

	//void setName(string first, string last);
	//Function to set firstName and lastName according
	//to the parameters.
	//Postcondition: firstName = first; lastName = last

	void setFirstName(string first);

	void setMiddleName(string middle);
	void setLastName(string last);


	string isFirstEqual(string& newFirst);

	string isLastEqual(string& newLast);

	string getFirstName() const;
	//Function to return the first name.
	//Postcondition: The value of firstName is returned.

	string getLastName() const;
	//Function to return the last name.
	//Postcondition: The value of lastName is returned.

	personType(string first = "", string middle = "", string last = "");

	// personType(string first = "");
	//Constructor
	//Sets firstName and lastName according to the parameters.
	//The default values of the parameters are null strings.
	//Postcondition: firstName = first; lastName = last


private:
	string firstName; //variable to store the first name
	string lastName;  //variable to store the last name
	string middleName;
	string newFirst;
	string newLast;
};
// I just called it personType.cpp
#include <iostream>
#include <string>
#include "personType.h"

using namespace std;

void personType::print() const
{
	cout << firstName << " " << " " << middleName << " " << lastName;
}

/*void personType::setName(string first, string last)
{
firstName = first;
lastName = last;
}
**/
void personType::setFirstName(string first)
{
	firstName = first;
	//lastName = last;
}
void personType::setLastName(string last)
{
	lastName = last;
}

void personType::setMiddleName(string middle)
{
	middleName = middle;

}

string personType::isFirstEqual(string &newFirst)
{
	cout << "Enter a first name to compare" << endl;
	cin >> newFirst;
	if (firstName.compare(newFirst) != 0)
		std::cout << firstName << " is not " << newFirst << '\n';
	else cout << firstName << " is the same as " << newFirst << endl;
}

string personType::getFirstName() const
{
	return firstName;
}

string personType::getLastName() const
{
	return lastName;
}

//constructor
personType::personType(string first, string middle, string last)

{
	firstName = first;
	middleName = middle;
	lastName = last;
}
// int main
#include <iostream>
#include <string>
#include "personType.h"

using namespace std;

int main()
{
	string newFirst;
	string firstName;

	//personType student("Lisa", "Regan");
	personType student("Lisa", "Marie", "Jones");






	student.print();
	cout << endl;
	cout << "Enter a first name to compare" << endl;
	cin >> newFirst;
	student.isFirstEqual(newFirst);


	cout << endl;

	system("pause");
	return 0;
}
The problem is here:

string personType::isFirstEqual(string &newFirst);

This function declaration says that isFirstEqual returns a string type. However,

1
2
3
4
5
6
7
8
string personType::isFirstEqual(string &newFirst)
{
	cout << "Enter a first name to compare" << endl;
	cin >> newFirst;
	if (firstName.compare(newFirst) != 0)
		std::cout << firstName << " is not " << newFirst << '\n';
	else cout << firstName << " is the same as " << newFirst << endl;
}


This only outputs and does not return anything. I would either make isFirstEqual return void or change the header to

bool personType::isFirstEqual(string &newFirst);

and return true or false depending on if it's equal. Hopefully that helped.
I hope you don't mind me asking, what is the difference between outputs and return. For some reason they are the same thing to me.

I changed it the code to the to what you recommended
bool personType::isFirstEqual(string &newFirst);

and I also changed it to the .cpp file
1
2
3
4
5
6
7
8
9
10
bool personType::isFirstEqual(string &newFirst)
{
	cout << "Enter a first name to compare" << endl;
	cin >> newFirst;
	if (firstName.compare(newFirst) != 0)
		std::cout << firstName << " is not " << newFirst << endl;
	else 
		cout << firstName << " is the same as " << newFirst << endl;
	
}


Am i going about this all wrong? Because I feel like I am. Forgive me if I am making no sense, I am tired and I am not entirely thinking straight.
Oh wait, so I just added something and it seems to work fine, but another problem has arrived.
1
2
3
4
5
6
7
8
9
10
11
bool personType::isFirstEqual(string &newFirst)
{
	cout << "Enter a first name to compare" << endl;
	cin >> newFirst;
	if (firstName.compare(newFirst) != 0)
		std::cout << firstName << " is not " << newFirst << endl;
	else 
		cout << firstName << " is the same as " << newFirst << endl;
       return 0;
	
}

So the program works fine, except now it says " Enter a firs name to compare" twice.
Is that because of return 0; ?
Of course, no problem.
A return statement sends data back to the calling function.
So in this case, if main() were to call your isFirstEqual() function, it expects to receive a boolean value back.
Output is completely different. Output simply sends data to the screen, file, or something else. A file can output one thing and return another. However, it can only and MUST return what it's function declariation specifies.

The problem with your function, both before and now, is that you are outputting but not returning anything. When I said return true and false I mean something along the lines of the following function:

1
2
3
4
bool isGreater(int a, int b) {
    if (a > b) return true
    else return false
}


if you don't want to return anything, then set the function type to void:

void personType::isFirstEqual(string &newFirst);
Last edited on
Okay, that makes sense regarding the output and the return.

Ahh, I think that is what I wanted. I don't think I wanted to return anything, I just wanted to output.
Thanks!.
If you don't mind me bothering you some more, it keeps on out putting "Enter a first Name" twice and I have to input twice, however when it comes to the last name one it out puts it twice but I only enter once.
That's because you're asking for input in main() AND inside the isFirstEqual function. Remove it from the latter since you pass a string anyway.
Thank you so much! One of my projects is finished!
Thanks for helping me keep on moving forwards!
Topic archived. No new replies allowed.