Hey, I need help getting this program to run.

This is what I'm trying to do.

a) set and store the first name only
b) set and store the last name only
c) store and set the middle name
d) check to see if a given first name is the same as the first person already entered
e) check to see if a given last name is te same as the last person already entered

My main goal right now, is to get my program up and running, so I can enter in the first, middle, and last name. Thank you. My code is below.

.cpp

#include <iostream>
using namespace std;
#include "Unknown.h"


int main()
{
    
    
    personType first("Unknown");
    first.print();
    personType middle;
    middle.print();
    personType last;
    last.print();
    firstperson.checkfirst("James");
    lastperson.checkfirst("Berry");
    
    
    
    cout<<"What is the first name of person"<<endl;
    first;
    
    if (Student1.checkfirst(first))
    {
    cout<<"The first is not the same"<<endl;
    }
    
    if (Student1.checkfirst(last))
    {
    cout<<"The last is not the same"<<endl;
    }




system ("PAUSE");
return 0;
}

.h file

class personType
{
      
public:
       void print()const;
       void setName(string first, string middle, string last);
       string getFirstName()const;
       string getLastName()const;
       string getMiddleName()const;
       personType::personType();
       bool personType::checkfirst(string first);
       
       
private:
        
        string firstname;
        string lastname;
        string middlename;
};


void personType::print()const
{
     
     cout<<firstname<<" "<<middlename<<" "<<lastname;
     
}

void personType::setName(string first, string last, string middle)
{
     
     firstname = first;
     lastname = last;
     middlename = middle;
     
     
}

  personType::personType()
{
  
  
  firstname = "Unknown";
  middlename = "Unknown";
  lastname = "Unknown";
                   
}

string personType::getFirstName()const
{
       string firstname;
       
       cout<<"What is your first name"<<endl;
       cin>>firstname;
       
       return firstname;
       
}


string getmiddleName()const
{
       string middlename;
       
       
       cout<<"What is your middle name"<<endl;
       cin>>middlename;
       
       return middlename;
       
}

string personType::getLastName()const
{
       string lastname;
       
       cout<<"What is your last name"<<endl;
       cin>>lastname;
       
      return lastname;
       
       
       
}

bool personType::checkfirst(string first)
{
     
     
     
     
}

Recommended Answers

All 21 Replies

Well, what is wrong with the code you posted? Compiler errors/warnings? If yes, what are they?

Hey, I need help getting this program to run.

This is what I'm trying to do.

a) set and store the first name only
b) set and store the last name only
c) store and set the middle name
d) check to see if a given first name is the same as the first person already entered
e) check to see if a given last name is te same as the last person already entered

My main goal right now, is to get my program up and running, so I can enter in the first, middle, and last name. Thank you. My code is below.

.cpp

#include <iostream>
using namespace std;
#include "Unknown.h"


int main()
{
    
    
    personType first("Unknown");
    first.print();
    personType middle;
    middle.print();
    personType last;
    last.print();
    firstperson.checkfirst("James");
    lastperson.checkfirst("Berry");
    
    
    
    cout<<"What is the first name of person"<<endl;
    first;
    
    if (Student1.checkfirst(first))
    {
    cout<<"The first is not the same"<<endl;
    }
    
    if (Student1.checkfirst(last))
    {
    cout<<"The last is not the same"<<endl;
    }




system ("PAUSE");
return 0;
}

.h file

class personType
{
      
public:
       void print()const;
       void setName(string first, string middle, string last);
       string getFirstName()const;
       string getLastName()const;
       string getMiddleName()const;
       personType::personType();
       bool personType::checkfirst(string first);
       
       
private:
        
        string firstname;
        string lastname;
        string middlename;
};


void personType::print()const
{
     
     cout<<firstname<<" "<<middlename<<" "<<lastname;
     
}

void personType::setName(string first, string last, string middle)
{
     
     firstname = first;
     lastname = last;
     middlename = middle;
     
     
}

  personType::personType()
{
  
  
  firstname = "Unknown";
  middlename = "Unknown";
  lastname = "Unknown";
                   
}

string personType::getFirstName()const
{
       string firstname;
       
       cout<<"What is your first name"<<endl;
       cin>>firstname;
       
       return firstname;
       
}


string getmiddleName()const
{
       string middlename;
       
       
       cout<<"What is your middle name"<<endl;
       cin>>middlename;
       
       return middlename;
       
}

string personType::getLastName()const
{
       string lastname;
       
       cout<<"What is your last name"<<endl;
       cin>>lastname;
       
      return lastname;
       
       
       
}

bool personType::checkfirst(string first)
{
     
     
     
     
}

function bodies (definitions) should reside in a .cpp file, not a header file; the functions are being defined multiple times the way they are written (every file you include the header file in).

Thanx...
Sean

I'd recommend you either #ifdef #define something at the start of your .h file, or use a #pragma once line to ensure it isn't being included multiple times.

function bodies (definitions) should reside in a .cpp file, not a header file; the functions are being defined multiple times the way they are written (every file you include the header file in).

Thanx...
Sean

Can you give me an example of what you mean?

Well, what is wrong with the code you posted? Compiler errors/warnings? If yes, what are they?

Here is soe things that my compiler says. I don't know how to copy and paste, so i will type some of it manually:

In function int main()
10 - no match functoin for call to 'personType::personType(const char(8))'
candidates are: personType::personType(const personType&)
16 - 'firstperson' undeclared
17 - 'lastperson' is undeclared
24 - 'Student1' is undeclared

How do i solve line 10? and when do i suppose to declare the identifiers? I do declare them somewhere in .h file?

I'd recommend you either #ifdef #define something at the start of your .h file, or use a #pragma once line to ensure it isn't being included multiple times.

I was not taught how to do that. I'm just now learning Classes.

My main concern is to just getting the program to run. i want to be able to set and store a first, middle, and last name. Does anyone know how to do this from the code that I have thus far?

Here is soe things that my compiler says. I don't know how to copy and paste, so i will type some of it manually:

In function int main()
10 - no match functoin for call to 'personType::personType(const char(8))'
candidates are: personType::personType(const personType&)
16 - 'firstperson' undeclared
17 - 'lastperson' is undeclared
24 - 'Student1' is undeclared

How do i solve line 10? and when do i suppose to declare the identifiers? I do declare them somewhere in .h file?

The error messages should tell you where to go:

10 - no match functoin for call to 'personType::personType(const char(8))'
candidates are: personType::personType(const personType&)

Look at your constructors. Do you have a personType constructor that takes a char* for a parameter? If so, the compiler can't find it. If not, you need to add one or you can't use this call:

personType first("Unknown");

Line 16 - What does firstperson represent? What type is it? You can't use a variable without declaring it first somewhere:

#include <iostream>
using namespace std;
#include "Unknown.h"


int main()
{
    
    
    personType first("Unknown");
    first.print();
    personType middle;
    middle.print();
    personType last;
    last.print();
    firstperson.checkfirst("James");

If you haven't mentioned the variable firstperson anywhere before, the compiler has no idea what firstperson represents when it hits the red part above.

Thanks. I'm going to try to fix these problems. i will come reply back with my results or if i need more help.. thanks again

Hey... I tried to get my program to run, but i'm having problems getting it up...I need some help figuring out how to get it to run. My goal is to store and set the first name, middle name, and last name.

For example:

What is your first name?

Ray


etc.


I'm trying to get to that point.... Does anyone know what is wrong with the code? I comment out, so I can get the names to work.


Here is my code:


.cpp

#include <iostream>
using namespace std;
#include "Unknown.h"


int main()
{
    
    
    personType first("Unknown");
    firstname.getFirstName();
    middlename.getMiddleName();
    lastname.getLastName();
   // firstperson.checkfirst("James");
  //  lastperson.checkfirst("Berry");
    
    
    
  //  cout<<"What is the first name of person"<<endl;
  //  first;
    
  //  if (Student1.checkfirst(first))
  //  {
  //  cout<<"The first is not the same"<<endl;
  //  }
    
  //  if (Student1.checkfirst(last))
  //  {
  //  cout<<"The last is not the same"<<endl;
  //  }




system ("PAUSE");
return 0;
}

.h file

class personType
{
      
public:
       void print()const;
       personType(string first, string middle, string last);
       void getFirstName();
       void getLastName();
       void getMiddleName();
       personType::personType();
      // bool personType::checkfirst(string first);
       
       
private:
        
        string firstname;
        string lastname;
        string middlename;
};


void personType::print()const
{
     
     cout<<firstname<<" "<<middlename<<" "<<lastname;
     
}

personType::personType(string f, string l, string m)
{
     
     firstname = f;
     lastname = l;
     middlename = m;
     
     
}

  personType::personType()
{
  
  
  firstname = "Unknown";
  middlename = "Unknown";
  lastname = "Unknown";
                   
}

void personType::getFirstName()
{
       
       cout<<"What is your first name"<<endl;
       cin>>firstname;

       
}


void getmiddleName()
{
       
       cout<<"What is your middle name"<<endl;
       cin>>middlename;

       
}

void personType::getLastName()
{

       cout<<"What is your last name"<<endl;
       cin>>lastname;

       
       
       
}

//bool personType::checkfirst(string first)
//{
     
     
     
     
//}

I did some major changes to my code. I 'm still having problems getting it to run. I still need help


compiler errors:

getFirstName is undeclared
getMiddleNmae is undeclared
getLastName is undeclared


I don't know why they are undeclared when I declared them in my hfile as functions.

I'm trying to:

a) set and store the first name
b) set and store the middle name
c) set and store the last name
d) Check whether a given first name is the same as the first name of the person
e) Check whether a given first name is the same as the last name of the person


My main goal right now is to just get the program to run.

.cpp file

#include <iostream>
using namespace std;
#include "Unknown.h"


int main()
{
    
    
    personType name("Ray");
    personType James;
    getFirstName();
    getMiddleName();
    getLastName();
    




system ("PAUSE");
return 0;
}

.h file

class personType
{
      
public:
       void print()const;
       void setName(string first, string middle, string last);
       personType(string first = "", string middle = "", string last = "");
       string getFirstName();
       string getLastName();
       string getMiddleName();
       personType::personType();
      // bool personType::checkfirst(string first);
       
       
private:
        
        string firstname;
        string lastname;
        string middlename;
};


void personType::print()const
{
     
     cout<<firstname<<" "<<middlename<<" "<<lastname;
     
}

personType::personType(string first, string middle, string last)
{
     
     firstname = first;
     lastname = last;
     middlename = middle;
     
     
}

  void personType::setName(string first, string middle, string last)
{
  
  
  firstname = first;
  middlename = middle;
  lastname = last;
                   
}

string personType::getFirstName()
{
      cout<<"What is your first name"<<endl; 
      cin>>firstname;
      
      return firstname;

       
}


string personType::getMiddleName()
{
       cout<<"What is your middle name"<<endl;
       cin>>middlename;
       
       return middlename;

       
}

string personType::getLastName()
{
       cout<<"What is your last name"<<endl;
       cin>>lastname;
       
       return lastname;

       
       
       
}

Hey... I tried to get my program to run, but i'm having problems getting it up...I need some help figuring out how to get it to run. My goal is to store and set the first name, middle name, and last name.

For example:

What is your first name?

Ray


etc.


I'm trying to get to that point.... Does anyone know what is wrong with the code? I comment out, so I can get the names to work.


Here is my code:


.cpp

#include <iostream>
using namespace std;
#include "Unknown.h"


int main()
{
    
    
    personType first("Unknown");
    firstname.getFirstName();
    middlename.getMiddleName();
    lastname.getLastName();

You're "fixing" the code by commenting lines out, but that's not the problem. The lines that are giving the errors are not the lines where the errors actually are. The error has nothing to do with whether the compiler can find firstname, getFirstName, middlename, getMiddleName, lastname, or getLastName in the personType class. The problem is that the compiler has no idea that it is supposed to even LOOK at the personType class in these lines:

firstname.getFirstName();
    middlename.getMiddleName();
    lastname.getLastName();

You have declared exactly one instance of the personType class in main before these lines:

personType first("Unknown");

first does not show up anywhere in those lines. The compiler has no idea at all that these lines have anything to do with the personType class because you haven't told it that they do:

firstname.getFirstName();
    middlename.getMiddleName();
    lastname.getLastName();

so changing the personType class will have no effect. You need to step back and think about exactly what you are trying to do. Anything before the dot in the lines above has to be an instance of type personType for these lines to make sense. firstmname, middlename, and lastname are not instances of the personType class. They are data members of the personType class. If anything, they should be AFTER the dot, not before, but they shouldn't be there either since they are private. You need an instance of personType BEFORE the dot. In this case, presumablly that is the first variable, as in:

first.getFirstName ();

I don't know whether that's what you want or not, but it is at least syntactically correct.

I applied the first.getFirstName(); but now it says that first is undeclared. It is like whenever I apply something, it will say that it is undeclared. I tried it for the middle.getMiddleName(); and it says the same thing.

It is like this program sounds simple, but it seems difficult. =/

Where do I declare first,middle,last? Once again, I thought I declared it in the .h file. My code is still the same with the adjustment to first.getFirstName();

I am on the right track though?

I applied the first.getFirstName(); but now it says that first is undeclared. It is like whenever I apply something, it will say that it is undeclared. I tried it for the middle.getMiddleName(); and it says the same thing.

It is like this program sounds simple, but it seems difficult. =/

Where do I declare first,middle,last? Once again, I thought I declared it in the .h file. My code is still the same with the adjustment to first.getFirstName();

No, you DO NOT declare anything BEFORE the dot in the .h file. Everything AFTER the dot is in .h file. You didn't post your updated code, but I assume you didn't do anything like this:

int main()
{
    personType name("Ray");
    personType James;
    first.getFirstName ();


system ("PAUSE");
return 0;
}

This isn't going to work, if you did. When you changed your code from the prior post where you declared first in main to your last post where you didn't, obviously when you change the variable name of the variable declaration from first to name, you need to change the variable name of the function call too.

You need to write a null constructor. You have one declared, but none implemented. You also are either not reading the error messages carefully or you don't know what to do with them. You always declare the variable initially with a call to a constructor, even if it's the null constructor, THEN and ONLY THEN you can start making calls to the class functions themselves. So write a default constructor (it can be an empty function but it needs to be an implemented function, not just a declaration):

personType::personType ()
{
}

then you can do something like this in main:

personType person ();
person.getFirstName ();

On another note, you should use better variable names (first, middle, and last shouldn't be used since they are too similar to your data member names and it's easy to get confused. Finally, the compiler doesn't care, but keep your set and get functions consistent. You shouldn't be asking for a name in a get function. That's a set function's job. You either set or you get, but not both in the same function. Do something like this:

void personType:setFirstName ()
{
     cout << "Enter first name: ";
     cin >> firstname;
}

string personType::getFirstName ()
{
     return firstname;
}

I think you should probably do best to set this assignment aside and take a tutorial on the basics of a class. Or your textbook probably has a simple example. Work through an example that works, get used to the syntax, then come back to your assignment. Right now you don't understand the basics.

Actually, scratch the part about the null constructor. I didn't notice this line:

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

So you can do this:

personType person;
person.getFirstName ();  // but change it to setFirstName as explained before.

Thanks for the reply.


I made the changes you told me to make.


I'm getting 3 errors in my compiler:


11 request for member `getFirstName' in `person', which is of non-class type `personType ()()'
12 request for member `getMiddleName' in `person', which is of non-class type `personType ()()'
13 request for member `getLastName' in `person', which is of non-class type `personType ()()'


How do I resolve these errors, so I can get my program to run?

Here is my updated code:

.cpp

#include <iostream>
using namespace std;
#include "Unknown.h"


int main()
{
    
    
    personType person();
    person.getFirstName();
    person.getMiddleName();
    person.getLastName();
    




system ("PAUSE");
return 0;
}

.h file

class personType
{
      
public:
       void print()const;
       void setName(string first, string middle, string last);
       personType(string first = "", string middle = "", string last = "");
       void setFirstName();
       string getFirstName();
       void setLastName();
       string getLastName();
       void setMiddleName();
       string getMiddleName();
       personType::personType();
      // bool personType::checkfirst(string first);
       
       
private:
        
        string firstname;
        string lastname;
        string middlename;
};


void personType::print()const
{
     
     cout<<firstname<<" "<<middlename<<" "<<lastname;
     
}

personType::personType(string first, string middle, string last)
{
     
     firstname = first;
     lastname = last;
     middlename = middle;
     
     
}

  void personType::setName(string first, string middle, string last)
{
  
  
  firstname = first;
  middlename = middle;
  lastname = last;
                   
}

void personType::setFirstName ()
{
     cout << "Enter first name: ";
     cin >> firstname;
}


string personType::getFirstName()
{

      return firstname;

       
}


void personType::setMiddleName()
{
     cout << "Enter middle name: ";
     cin >> middlename;
}

string personType::getMiddleName()
{

      return middlename;

       
}

void personType::setLastName()
{
     cout << "Enter middle name: ";
     cin >> lastname;

     
}
string personType::getLastName()
{

      return lastname;

     
}

Sorry I just received your last post.

This is what the compiler says:

10 call of overloaded `personType()' is ambiguous
11 no matching function for call to `personType::setFirstName()'

I posted my .cpp file to show the corrections. My .h file is the same as my last post.

#include <iostream>
using namespace std;
#include "Unknown.h"


int main()
{
    
    
    personType person;
    person.setFirstName();
    person.setMiddleName();
    person.setLastName();
    




system ("PAUSE");
return 0;
}

Sorry I just received your last post.

This is what the compiler says:

10 call of overloaded `personType()' is ambiguous
11 no matching function for call to `personType::setFirstName()'

I posted my .cpp file to show the corrections. My .h file is the same as my last post.

#include <iostream>
using namespace std;
#include "Unknown.h"


int main()
{
    
    
    personType person;
    person.setFirstName();
    person.setMiddleName();
    person.setLastName();
    




system ("PAUSE");
return 0;
}

Try deleting your null constructor line from the .h file.

personType::personType(); //delete this line

Thanks. I have one more question.

I want to input another first name and see if it matches the name that I stored.

For example.

cout<<"What is your first name?";

cin>>Jack


cout<<"What is the first name?";

cin>>Jack


cout<<"The name is the same as the person."


I wrote some code, but I want to make sure if I'm on the right track or to see what I am missing...This is the code I wrote...and I will also post my full code below:

bool personType::checkfirst(string firstn)
{

     
     cout<<"What is the first name"<<endl;
     cin>>firstn;
     
    if (checkfirst(firstname))
    cout<<"The name is the same as the person!"<<endl;
     
     
     
}

.cpp file

#include <iostream>
using namespace std;
#include "Unknown.h"


int main()
{
    
    
    personType person;
    person.setFirstName();
    person.setMiddleName();
    person.setLastName();
    person.checkfirst();



system ("PAUSE");
return 0;
}

.h file

class personType
{
      
public:
       void print()const;
       void setName(string first, string middle, string last);
       personType(string first = "", string middle = "", string last = "");
       void setFirstName();
       string getFirstName();
       void setLastName();
       string getLastName();
       void setMiddleName();
       string getMiddleName();
       bool checkfirst(string firstn);
       
       
private:
        
        string firstname;
        string lastname;
        string middlename;
};


void personType::print()const
{
     
     cout<<firstname<<" "<<middlename<<" "<<lastname;
     
}


  void personType::setName(string first, string middle, string last)
{
  
  
  firstname = first;
  middlename = middle;
  lastname = last;
                   
}

personType::personType(string first, string middle, string last)
{
     
     firstname = first;
     lastname = last;
     middlename = middle;
     
     
}

void personType::setFirstName()
{
     cout << "Enter first name: "<<endl;
     cin >> firstname;
     cout<<endl;
}


string personType::getFirstName()
{

      return firstname;

       
}


void personType::setMiddleName()
{
     cout << "Enter middle name: "<<endl;
     cin >> middlename;
     cout<<endl;
}

string personType::getMiddleName()
{

      return middlename;

       
}

void personType::setLastName()
{
     cout << "Enter last name: "<<endl;
     cin >> lastname;
     cout<<endl;

     
}
string personType::getLastName()
{

      return lastname;

       
       
       
}

bool personType::checkfirst(string firstn)
{

     
     cout<<"What is the first name"<<endl;
     cin>>firstn;
     
    if (checkfirst(firstname))
    cout<<"The name is the same as the person!"<<endl;
     
     
     
}

Nevermind...I figured it all out...Thanks guys! I appreciate it =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.