-
Parameters?
This ones giving me problems... I can't see why the parameters aren't matching up. Help? PATY.
Code:
Code:
#include<iostream>
#include<conio.h>
using namespace std;
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();
};
void Worker::setIdNum(int id)
{
if(id< 0 || id > 999)
idNum = 0;
else
idNum = id;
}
void Worker::setLastName(char last)
{
strcpy(lastName, last);
}
void Worker::setFirstName(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()
{
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();
std::cin.clear();
std::cin.ignore();
getchar();
}
Error Message:
1>c:\users\user\documents\visual studio 2010\projects\debug 5\debug 5\de.cpp(31): error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\user\documents\visual studio 2010\projects\debug 5\debug 5\de.cpp(35): error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\user\documents\visual studio 2010\projects\debug 5\debug 5\de.cpp(56): error C2664: 'Worker::setLastName' : cannot convert parameter 1 from 'const char [8]' to 'char'
1> There is no context in which this conversion is possible
1>c:\users\user\documents\visual studio 2010\projects\debug 5\debug 5\de.cpp(57): error C2664: 'Worker::setFirstName' : cannot convert parameter 1 from 'const char [5]' to 'char'
1> There is no context in which this conversion is possible
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.92
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
P.S. Like my post before this, it is homework but it's a program that I've had to fix the problems for and it was worse than this when I started. I've only used this forum as a last resort when I'm absolutely stuck. Thanks again.
-
'char' to 'const char *'
It wants a pointer pointers give you strings
a char is 'a';
a char * can be "Hello World!"
Fix your declarations and it will probably help always fix the first error.
-
OMFG! Thank you! My buddy's gonna ........ bricks, he knew it was something to do with pointers, so we tried what we thought was everything. Apparently it wasn't everything. Thanks again mate.
-
Errors like that are telling you: Hey this is <function>, you gave me <datatype> I was expecting you to give me <datatype2> I am confused on what to do, are you sure you gave me the correct variable or data? Generally in this case that is the simple form of what you got.
-
Change the chars to std::string and replace strcpy with a normal assignment.
-
And...
- Get rid of the conio.h header since nothing in your code requires it.
- Don't use void main
- The cin.clear/ignore statements don't do anything useful in this particular case and could be removed.
- Remove the getchar and replace with a call to cin.get instead.
- If you insist on using old style C-strings, you should explicitly include the <cstring> header on not rely on your implementation to divine its necessity. Likewise, if you switch to using C++ std::strings then you'll want to include the <string> instead.
- You should consider a constructor and pass all those initializations you're currently performing as separate function calls in as parameters in the constructor so you build/create the worker object in a single statement.