Strcpy error

Am having a problem debugging this code
Please I need some help

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
  // Debug 5-1
#include<iostream.h>
#include<conio.h>
class Worker
 {
  private:
   int idNum;
   char lastName[20];
   char firstName[20];
   double salary;
   void setIdNum(int id);
   void setLastName(char last);
   void setFirstName(char first);
   void setSalary(double payRate);
   void displayWorker();
  };

void Worker::setIdNum(int id)
 {
   if(id< 0 || id > 999)
     idNum = 0;
   else
     idNum = id;
 }
void setLastName(char last)
 {
   strcpy(lastName,last);
  }
void Worker::setFirstName(char first)
 {
   strcpy(firstName,first);
  }
void setSalary(double payRate)
{
   if (payRate <= 5.75 || payRate > 99.99)
     salary = 5.75;
   else
     salary = payRate;
 }

void Worker::displayWorker()
 {
   cout<<"ID #"<<idNum<<" Name: "<<firstName<<" "<<lastName
   	<<" Salary: $"<<salary<<endl;
  }


void main()
  {
     Worker aWorker;
     aWorker.setIdNum(333);
     aWorker.setLastName("Vasquez");
     aWorker.setFirstName("Juan");
     aWorker.setSalary(15.65);
     aWorker.displayWorker();
     getch();
  }
Hello icezy,

First off using a "std::string" would be much easier, but if you have to use C style character arrays you will need the header file "<cstring>" for functions like "strcpy()".

Next I would have to ask what problems?

Where do you think it is?

Is there any compile or linker errors that come up? If so post the complete error message.

Andy
Here's what have been able to do

// Debug 5-1
#include<iostream>
#include<conio.h>
#include<string.h>

using namespace std;

class Worker
{
private:
int idNum;
char lastName[20];
char firstName[20];
double salary;
void setIdNum(int id);
void setLastName(char last);
void setFirstName(char first);
void setSalary(double payRate);
void displayWorker();
};

void Worker::setIdNum(int id)
{
if(id< 0 || id > 999)
idNum = 0;
else
idNum = id;
}
void Worker::setLastName(char last)
{
strcpy(lastName,last); //Am having errors here
}
void Worker::setFirstName(char first)
{
strcpy(firstName,first);
}
void setSalary(double payRate)
{
if (payRate <= 5.75 || payRate > 99.99)
salary = 5.75;
else
salary = payRate;
}

void Worker::displayWorker()
{
cout<<"ID #"<<idNum<<" Name: "<<firstName<<" "<<lastName
<<" Salary: $"<<salary<<endl;
}


int main()
{
Worker aWorker;
aWorker.setIdNum(333);
aWorker.setLastName("Vasquez");
aWorker.setFirstName("Juan");
aWorker.setSalary(15.65);
aWorker.displayWorker();
_getch();
}

But am still having errors in it
Last edited on
For setLastName(...) and setFirstName(...) you pass a single character. strcpy(...) does not accept a single character hence the compiler error. Change the function e.g. to:
1
2
3
4
5
6
7
8
void Worker::setLastName(const char last[])
 {
   strcpy(lastName,last);
  }
void Worker::setFirstName(const char first[])
 {
   strcpy(firstName,first);
  }


Actually you have more errors than that:
All members are 'private:'. Make at least the functions public:
setLastName(...) and setSalary(...) need the scope Worker::
and some more...
Thanks coder
Have done that and it works but still having an error in the:
1
2
3
4
5
6
7
void setSalary(double payRate)
{
if (payRate <= 5.75 || payRate > 99.99)
salary = 5.75;
else
salary = payRate;
}


and here too
1
2
3
4
5
6
7
8
9
10
int main()
{
Worker aWorker;
aWorker.setIdNum(333);
aWorker.setLastName("Vasquez");
aWorker.setFirstName("Juan");
aWorker.setSalary(15.65);
aWorker.displayWorker();
_getch();
}
Last edited on
Hello icezy,

Is this a C program or C++ program? Indications are you are trying to do both.

When you define:
1
2
3
4
5
class Worker
{


};

What is between the {}s is private by default for a class. The next thing is that every thing you have defined in the class is "private" and there is no way to access the functions. A "private" variable or function of a class can only be accessed through a "public" member function.

Most of your functions are stand alone functions and not part of the class. Therefor these standalone functions can not access the private section of the class.

It may appear to be nice, but you should avoid using "<conio.h>" as not everyone has this header file available or may not be using a Windows operating system.

Also
salem c once wrote:

#include<conio.h>
Obsolete since 1990, when the world stopped using DOS as a primary operating system.



In the place of "_getch();" I tend to use this code:
1
2
3
4
5
6
7
// A fair C++ replacement for "system("pause")". Or a way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();

return 0;  // <--- Not required, but makes a good break point. 

You can delete anything that you do not need.

If you adjust things as coder777 has showed you it should work better. You will also have to change your class.

Andy

void setSalary(double payRate)

->

void Worker::setSalary(double payRate)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Worker
 {
  private:
   int idNum;
   char lastName[20];
   char firstName[20];
   double salary;
  public:
   void setIdNum(int id);
   void setLastName(char last);
   void setFirstName(char first);
   void setSalary(double payRate);
   void displayWorker();
  };
Thanks Andy
I understand it now
There are various issues:

1) the return type of main() is int not void.
2) When passing a char string, use char*, not char
3) Some class methods aren't defined properly (no class name)
4) Public functions aren't specified as public - for class, these are private by default.

Consider:

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
#include <iostream>
#include <cstring>
//#include <conio.h>

class Worker
{
	int idNum {};
	char lastName[20] {};
	char firstName[20] {};
	double salary {};

public:
	void setIdNum(int id);
	void setLastName(const char* last);
	void setFirstName(const char* first);
	void setSalary(double payRate);
	void displayWorker() const;
};

void Worker::setIdNum(int id)
{
	idNum = (id < 0 || id > 999) ? 0 : id;
}

void Worker::setLastName(const char* last)
{
	strcpy(lastName, last);
}

void Worker::setFirstName(const char* first)
{
	strcpy(firstName, first);
}

void Worker::setSalary(double payRate)
{
	salary = (payRate <= 5.75 || payRate > 99.99) ? 5.75 : payRate;
}

void Worker::displayWorker() const
{
	std::cout << "ID #" << idNum << " Name: " << firstName << " " << lastName
		<< " Salary: $" << salary << '\n';
}

int main()
{
	Worker aWorker;

	aWorker.setIdNum(333);
	aWorker.setLastName("Vasquez");
	aWorker.setFirstName("Juan");
	aWorker.setSalary(15.65);
	aWorker.displayWorker();
	//getch();
}



ID #333 Name: Juan Vasquez Salary: $15.65

Last edited on
Hello icezy,

I came up with this. See what you think.

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
// Debug 5-1
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <limits>
#include <cstring>
//#include<conio.h>

const int MAXSIZE = 21;  //<--- + 1  for the "\0" to mark the end.
class Worker
{
    int idNum{};
    char lastName[MAXSIZE]{};
    char firstName[MAXSIZE]{};
    double salary{};

    public:
        Worker() {}  // <--- Default ctor.
        Worker(int id, const char last[], const char first[], double payRate)  // <--- Overloaded ctor.
        {                          // <--- Done this way because of the "strcpy"s.
            idNum = id;
            strcpy(lastName, last);
            strcpy(firstName, first);
            salary = payRate;
        }
        void setIdNum(int id);
        void setLastName(const char* last);
        void setFirstName(const char* first);
        void setSalary(double payRate);
        void displayWorker();
};

void Worker::setIdNum(int id)
{
    if (id < 0 || id > 999)
        idNum = 0;
    else
        idNum = id;
}

void Worker::setLastName(const char* last)
{
    strcpy(lastName, last);
}

void Worker::setFirstName(const char* first)
{
    strcpy(firstName, first);
}

void Worker::setSalary(double payRate)
{
    if (payRate <= 5.75 || payRate > 99.99)
        salary = 5.75;
    else
        salary = payRate;
}

void Worker::displayWorker()
{
    std::cout << "\n ID #" << idNum << "\n Name: " << firstName << " " << lastName
        << "\n Salary: $" << salary << '\n';
}


int main()
{
    //Worker aWorker;  // <--- Either one works. Just change here and remove the comments on lines 69 - 75.
    Worker aWorker(333, "Vasquez", "Juan", 15.65);

    //aWorker.setIdNum(333);

    //aWorker.setLastName("Vasquez");

    //aWorker.setFirstName("Juan");

    //aWorker.setSalary(15.65);

    aWorker.displayWorker();

    // A fair C++ replacement for "system("pause")" or "_getch()". Or a way to pause the program.
    // The next line may not be needed. If you have to press enter to see the prompt it is not needed.
    //std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
    std::cout << "\n\n Press Enter to continue: ";
    std::cin.get();

    return 0;  // <--- Not required, but makes a good break point.
}


Andy

Edit:
Last edited on
or using a class constructor:

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
#include <iostream>
#include <cstring>
//#include <conio.h>

class Worker
{
	int idNum {};
	char lastName[20] {};
	char firstName[20] {};
	double salary {};

public:
	Worker() {}
	Worker(int id, const char* last, const char* first, double payRate);
	void setIdNum(int id);
	void setLastName(const char* last);
	void setFirstName(const char* first);
	void setSalary(double payRate);
	void displayWorker() const;
};

Worker::Worker(int id, const char* last, const char* first, double sal) : idNum(id), salary(sal) {
	strcpy(lastName, last);
	strcpy(firstName, first);
}

void Worker::setIdNum(int id)
{
	idNum = (id < 0 || id > 999) ? 0 : id;
}

void Worker::setLastName(const char* last)
{
	strcpy(lastName, last);
}

void Worker::setFirstName(const char* first)
{
	strcpy(firstName, first);
}

void Worker::setSalary(double payRate)
{
	salary = (payRate <= 5.75 || payRate > 99.99) ? 5.75 : payRate;
}

void Worker::displayWorker() const
{
	std::cout << "ID #" << idNum << ", Name: " << firstName << " " << lastName
		<< ", Salary: $" << salary << '\n';
}

int main()
{
	const Worker aWorker(333, "Vasquez", "Juan", 15.65);

	aWorker.displayWorker();
	//getch();
}

@seeplus,

Thanks. Line 22 I was wondering how to do it that way. The "strcpy"s are something new in this context.

Andy
You can also do this, as strcpy() returns a pointer to destination:

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
#include <iostream>
#include <cstring>
//#include <conio.h>

class Worker
{
	int idNum {};
	char lastName[20] {};
	char firstName[20] {};
	char* const lastptr {};
	char* const firstptr {};
	double salary {};

public:
	Worker() {}
	Worker(int id, const char* last, const char* first, double payRate);

	void setIdNum(int id);
	void setLastName(const char* last);
	void setFirstName(const char* first);
	void setSalary(double payRate);
	void displayWorker() const;
};

Worker::Worker(int id, const char* last, const char* first, double sal) : idNum(id), salary(sal), lastptr(strcpy(lastName, last)), firstptr(strcpy(firstName, first)) {}

void Worker::setIdNum(int id)
{
	idNum = (id < 0 || id > 999) ? 0 : id;
}

void Worker::setLastName(const char* last)
{
	strcpy(lastName, last);
}

void Worker::setFirstName(const char* first)
{
	strcpy(firstName, first);
}

void Worker::setSalary(double payRate)
{
	salary = (payRate <= 5.75 || payRate > 99.99) ? 5.75 : payRate;
}

void Worker::displayWorker() const
{
	std::cout << "ID #" << idNum << ", Name: " << firstName << " " << lastName
		<< ", Salary: $" << salary << '\n';
}

int main()
{
	const Worker aWorker(333, "Vasquez", "Juan", 15.65);

	aWorker.displayWorker();
	//getch();
}

Topic archived. No new replies allowed.