changing the variable names in a class.

I changed all the names in my class but I've done something wrong now and I can't figure out what it is:

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
#include "car.h"
#include <string.h>

Customer::Customer()
{
}
Customer::Customer(char name[], char gender[], int phone, char pcode[], float dob);
{
    strcpy(cust_name, name);
    strcpy(cust_gender, gender);
    cust_phone = phone;
    strcpy(cust_pcode, pcode);
    cust_dob = dob;


}

void Customer::setName(char name[])
{
	strcpy(cust_name, name);
}
char* Customer::getName()
{
	return cust_name;
}



void Customer::setGender(char gender[])
{
    strcpy(cust_gender, gender);
}
char* Customer::getGender()
{
    return cust_gender;
}


void Customer::setPhone(int phone)
{
    cust_phone = phone;
}
int Customer::getPhone()
{
    return cust_phone;
}


void Customer::setPcode(char pcode)
{
    strcpy(cust_pcode, pcode);
}
char* Customer::getPcode()
{
    return cust_pcode;
}

void Customer::setDob(float dob)
{
    cust_dob = dob;
}
float Customer::getDob()
{
    return cust_dob;
}



my errors are:
1
2
3
4
5
6
||=== car project, Debug ===|
/home/rej3kt/Desktop/car_class/car project/car.cpp|7|error: declaration of ‘Customer::Customer(char*, char*, int, char*, float)’ outside of class is not definition|
/home/rej3kt/Desktop/car_class/car project/car.cpp|8|error: expected unqualified-id before ‘{’ token|
/home/rej3kt/Desktop/car_class/car project/car.cpp|49|error: prototype forvoid Customer::setPcode(char)’ does not match any in class ‘Customer’|
/home/rej3kt/Desktop/car_class/car project/car.h|25|error: candidate is: void Customer::setPcode(char*)|
||=== Build finished: 4 errors, 0 warnings ===|
1. Remove a semicolon from line 7
2 .You need char * as a function parameter in line 49
Thanks mate appreciate it! Usually is simple mistakes

Just got a couple more, sorted some others out after that but can't figure this first out out and invalid type:

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

#include <iostream>
#include <string.h>
#include "car.h"
#include <cstdlib>
#include <fstream>
using namespace std;

int main()
{
    char cust_name[50];
    char cust_gender[50];
    int cust_phone;
    char cust_pcode;
    float cust_dob;
    int number;
    char username[50];
   Customer c1(cust_name, cust_gender, cust_phone, cust_pcode, cust_dob);


    cout << " Please enter your name to start: "<<endl;
    cin.getline(username, 50);

    do{

    cout << "\n\n\nWelcome, please enter a number to go to that section on the menu:\n1) Enter your car name"<<endl;
    cout << "2) Enter your car colour" << endl;
    cout << "3) Enter the age of your age"<<endl;
    cout << "4) Enter the value of your car in GBP"<<endl;
    cout << "5) Enter how many miles per gallon your car does"<<endl;
    cout << "6) Save any informaion entered" <<endl;
    cout << "7) Retrieve any previously entered information" <<endl;
    cout << "8) Exit program"<<endl;
    cin >> number;

    ofstream myfile;

 switch (number){
  case 1:
    cout << "Enter your car name: " << endl;
    cin >> cust_name;
    c1.setName(cust_name);
    break;
  case 2:
    cout << "Enter your car colour: " << endl;
    cin >> cust_gender;
    c1.setGender(cust_gender); break;
  case 3:
    cout << "Enter the age of your car: " << endl;
    cin >> cust_phone;
    c1.setPhone(cust_phone); break;
  case 4:
    cout << "Enter the value of your car in GBP: " << endl;
    cin >> cust_pcode;
    c1.setPcode(cust_pcode);
                       break;
  case 5:
    cout << "Enter how many miles per gallon your car does: " << endl;
    cin >> cust_dob;
    c1.setDob(cust_dob); break;
  case 6:

   cout << "Saved!"<<endl;


   myfile.open ("class2.txt");
   myfile << c1.getName() << endl;
   myfile << c1.getGender() << endl;
   myfile << c1.getPhone() << endl;
   myfile << c1.getPcode() << endl;
   myfile << c1.getDob() <<endl;

   myfile.close();
   break;


    case 7:
    break;

 case 8:
    cout << "Press enter to close"<<endl;
    break;
    return 0;

}

    }while(number !=8    );


    cout << "\n\n\n\n\n\n\n\n\n" ;
    cout << "Your car name is: " << c1.getName() <<endl;
    cout << "Your car colour is: " << c1.getGender() <<endl;
    cout << "Your car is " << c1.getPhone() << " years old"<<endl;
    cout << "Your car is worth £" <<c1.getPcode() <<endl;
    cout << "Your car does " <<c1.getDob()<< "mpg"<<endl;
    return 0;
}


and the errors:
1
2
3
4
5
6
7
/home/rej3kt/Desktop/car_class/car project/main.cpp||In function ‘int main()’:|
/home/rej3kt/Desktop/car_class/car project/main.cpp|17|error: invalid conversion from ‘char’ to ‘char*’|
/home/rej3kt/Desktop/car_class/car project/main.cpp|17|error:   initializing argument 4 of ‘Customer::Customer(char*, char*, int, char*, float)’|
/home/rej3kt/Desktop/car_class/car project/main.cpp|54|error: invalid conversion from ‘char’ to ‘char*’|
/home/rej3kt/Desktop/car_class/car project/main.cpp|54|error:   initializing argument 1 of ‘void Customer::setPcode(char*)’|
||=== Build finished: 4 errors, 0 warnings ===|
So Customer::cust_pcode is supposed to be a number? If so, you can use an integer ( int ).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// car.h
class Customer
{
// ...

int cust_pcode;
// ...

void setPcode(int pcode);


};


// car.cpp

void Customer::setPcode(int pcode)
{
cust_pcode=pcode;
}



EDIT:

cout << "Enter your car colour: " << endl;
cin >> cust_gender;
c1.setGender(cust_gender); break;

Am I missing something or you are confusing a car colour with person's gender?
Last edited on
pcode is postcode so it's letters and numbers

edit: Sorry it's a bit confusing. It was a car class before I've changed it into a customer class. Just haven't changed the couts yet :)
Last edited on
then write char cust_pcode[50]; instead of char cust_pcode; in main

EDIT: fixed a typo
Last edited on
Got it working now. Thank's mate you're a life saver. In programming it's always the little mistakes that screw you over. A semi colon here, an extra bracket there etc...
And one more thing, you can use std::string instead of char arrays.

http://www.cplusplus.com/reference/string/string/
people keep telling me that lol, I'm not going to try confuse myself again. I'll leave it the way i've been taught.

Great, code::blocks has decided to randomly delete my entire project and It's not in my recycle bin either. Think most of it is on these forums anyways lol.
Okay well since code:blocks decided to delete any trace of my program I've managed to piece it back together, I seem to have a clusterfuck of errors though and can't quite see where they're all stemming from. There's something I've not defined I think.

This is my .cpp 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
54
55
56
57
58
59
60
61
62
63
64
65
#include "cust.h"
#include <string.h>

Customer::Customer()
{
}
Customer::Customer(char name[], char gender[], int phone, char pcode[], float dob)
{
    strcpy(cust_name, name);
    strcpy(cust_gender, gender);
    cust_phone = phone;
    strcpy(cust_pcode, pcode);
    cust_dob = dob;


}

void Customer::setName(char name[])
{
	strcpy(cust_name, name);
}
char* Customer::getName()
{
	return cust_name;
}



void Customer::setGender(char gender[])
{
    strcpy(cust_gender, gender);
}
char* Customer::getGender()
{
    return cust_gender;
}


void Customer::setPhone(int phone)
{
    cust_phone = phone;
}
int Customer::getPhone()
{
    return cust_phone;
}


void Customer::setPcode(char pcode)
{
    strcpy(cust_pcode, pcode);
}
char* Customer::getPcode()
{
    return cust_pcode;
}

void Customer::setDob(float dob)
{
    cust_dob = dob;
}
float Customer::getDob()
{
    return cust_dob;
}



my 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
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

#include <iostream>
#include <string.h>
#include "cust.h"
#include <cstdlib>
#include <fstream>
using namespace std;

int main()
{
    char cust_name[50];
    char cust_gender[50];
    int cust_phone;
    char cust_pcode[50];
    float cust_dob;
    int number;
    char username[50];
   Customer c1(cust_name, cust_gender, cust_phone, cust_pcode, cust_dob);


    cout << " Please enter your name to start: "<<endl;
    cin.getline(username, 50);

    do{

    cout << "\n\n\nWelcome, please enter a number to go to that section on the menu:\n1) Enter your car name"<<endl;
    cout << "2) Enter your car colour" << endl;
    cout << "3) Enter the age of your age"<<endl;
    cout << "4) Enter the value of your car in GBP"<<endl;
    cout << "5) Enter how many miles per gallon your car does"<<endl;
    cout << "6) Save any informaion entered" <<endl;
    cout << "7) Retrieve any previously entered information" <<endl;
    cout << "8) Exit program"<<endl;
    cin >> number;

    ofstream myfile;

 switch (number){
  case 1:
    cout << "Enter your car name: " << endl;
    cin >> cust_name;
    c1.setName(cust_name);
    break;
  case 2:
    cout << "Enter your car colour: " << endl;
    cin >> cust_gender;
    c1.setGender(cust_gender); break;
  case 3:
    cout << "Enter the age of your car: " << endl;
    cin >> cust_phone;
    c1.setPhone(cust_phone); break;
  case 4:
    cout << "Enter the value of your car in GBP: " << endl;
    cin >> cust_pcode;
    c1.setPcode(cust_pcode);
                       break;
  case 5:
    cout << "Enter how many miles per gallon your car does: " << endl;
    cin >> cust_dob;
    c1.setDob(cust_dob); break;
  case 6:

   cout << "Saved!"<<endl;


   myfile.open ("class2.txt");
   myfile << c1.getName() << endl;
   myfile << c1.getGender() << endl;
   myfile << c1.getPhone() << endl;
   myfile << c1.getPcode() << endl;
   myfile << c1.getDob() <<endl;

   myfile.close();
   break;


    case 7:
    break;

 case 8:
    cout << "Press enter to close"<<endl;
    break;
    return 0;

}

    }while(number !=8    );


    cout << "\n\n\n\n\n\n\n\n\n" ;
    cout << "Your car name is: " << c1.getName() <<endl;
    cout << "Your car colour is: " << c1.getGender() <<endl;
    cout << "Your car is " << c1.getPhone() << " years old"<<endl;
    cout << "Your car is worth £" <<c1.getPcode() <<endl;
    cout << "Your car does " <<c1.getDob()<< "mpg"<<endl;
    return 0;
}


and my .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
#ifndef RANDOMCLASSHEADER_H_INCLUDED
#define RANDOMCLASSHEADER_H_INCLUDED



#endif // RANDOMCLASSHEADER_H_INCLUDED





class Customer
{

    Customer();

 public:
    Customer(char name[], char gender[], int phone, char pcode[], float dob);

    void setName(char name[]);
    char* getName();

    void setGender(char get[]);
    char* getGender();

    void setPhone(int phone);
    int getPhone();

    void setPcode(char pcode[]);
    char* getPcode();

    void setDob(float dob);
    float getDob();


 private:
    char name[50];
    char gender[50];
    char phone[50];
    int pcode;
    float dob;
};


and my errors:
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
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp||In constructor ‘Customer::Customer(char*, char*, int, char*, float)’:|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp|9|error: ‘cust_name’ was not declared in this scope|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp|10|error: ‘cust_gender’ was not declared in this scope|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp|11|error: ‘cust_phone’ was not declared in this scope|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp|12|error: ‘cust_pcode’ was not declared in this scope|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp|13|error: ‘cust_dob’ was not declared in this scope|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp||In member function ‘void Customer::setName(char*)’:|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp|20|error: ‘cust_name’ was not declared in this scope|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp||In member function ‘char* Customer::getName()’:|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp|24|error: ‘cust_name’ was not declared in this scope|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp||In member function ‘void Customer::setGender(char*)’:|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp|31|error: ‘cust_gender’ was not declared in this scope|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp||In member function ‘char* Customer::getGender()’:|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp|35|error: ‘cust_gender’ was not declared in this scope|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp||In member function ‘void Customer::setPhone(int)’:|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp|41|error: ‘cust_phone’ was not declared in this scope|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp||In member function ‘int Customer::getPhone()’:|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp|45|error: ‘cust_phone’ was not declared in this scope|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp|49|error: prototype forvoid Customer::setPcode(char)’ does not match any in class ‘Customer’|
/home/rej3kt/Desktop/C++ Class/custclass/cust.h|29|error: candidate is: void Customer::setPcode(char*)|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp||In member function ‘char* Customer::getPcode()’:|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp|55|error: ‘cust_pcode’ was not declared in this scope|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp||In member function ‘void Customer::setDob(float)’:|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp|60|error: ‘cust_dob’ was not declared in this scope|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp||In member function ‘float Customer::getDob()’:|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp|64|error: ‘cust_dob’ was not declared in this scope|
||=== Build finished: 16 errors, 0 warnings ===|


I know it's alot of code and I'm really sorry. Thanks for any help.
Learn to understand compiler errors -- the compiler tells you that those variables aren't declared. Just rename Customer::name to Customer::cust_name and etc.

And you are using include guards in a wrong way. A correct code would be
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef RANDOMCLASSHEADER_H_INCLUDED
#define RANDOMCLASSHEADER_H_INCLUDED

class Customer
{

// ...

};



#endif // RANDOMCLASSHEADER_H_INCLUDED 
Last edited on
I've no idea what the "Guards" do, they're automatically included when I create a header file.

Sorry, in regards to changing Customer::name to Customer:cust_name I'm not quite sure where abouts your looking in my code for that.
Sorry, in regards to changing Customer::name to Customer:cust_name I'm not quite sure where abouts your looking in my code for that.


Change char name[50]; to char cust_name[50]; in your Customer class. Do the same for other variables.

I've no idea what the "Guards" do, they're automatically included when I create a header file.

They prevent you from including the same file twice. See http://www.cplusplus.com/forum/articles/10627/#msg49679
Thank you mate I appreciate all the help, everything is fixed now, you've been great thanks again =)
Topic archived. No new replies allowed.