Thread: Parameters?

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    12

    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.

  2. #2
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    '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.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    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.

  4. #4
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    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.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Change the chars to std::string and replace strcpy with a normal assignment.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    And...

    1. Get rid of the conio.h header since nothing in your code requires it.
    2. Don't use void main
    3. The cin.clear/ignore statements don't do anything useful in this particular case and could be removed.
    4. Remove the getchar and replace with a call to cin.get instead.
    5. 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.
    6. 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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Replies: 6
    Last Post: 07-21-2008, 06:04 AM
  3. Parameters quick Question
    By lifeis2evil in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2007, 11:12 PM
  4. function with variable number of parameters
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 07-23-2006, 03:35 PM
  5. Additional parameters for operator delete
    By darksaidin in forum C++ Programming
    Replies: 0
    Last Post: 09-21-2003, 11:46 AM