Homework help (please) What's wrong with my code?

Can anyone tell me why this compiles but then crashes after it executes? As always, any help GREATLY appreciated!
Tricia
.h file
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
  //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;

};

function:
1
2
3
4
5
6
7
8
9
string personType::isFirstEqual(string& newFirst)
{

if (firstName.compare(newFirst) != 0)
    std::cout << firstName << " is not " << newFirst << '\n';
    else cout<<firstName<< " is the same as "<<newFirst<<endl;


}

Main
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
//Test Program personType

#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;

    return 0;
}
It would help if we could see your constructor and print function.
I thought about that but I was worried it was too much to post. Here is the rest:
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
//personTypeImp.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;
}

Works fine for me. The only thing I noticed is on line 36 in your implementation file you forgot the reference so it doesn't match the prototype. string personType::isFirstEqual(string &nweFirst).
I uninstalled and re-installed codeblocks...still crashed. I decided to just keep going before I was out of time to finish my assignment and now the crashing seems to have gone away. weird lol

Thank you for looking at it :)!
Topic archived. No new replies allowed.