c-strings

Dec 6, 2011 at 11:33pm
I'm supposed to modify an existing payroll program from using normal c++ strings to using c-strings...

and I don't get them.

Can someone explain them to me or direct me to a tutorial on them?

I understand the concept that they're basically char arrays but I don't understand how to populate them or use them in a program when I'm trying to read/write from a file.

thanks
Dec 7, 2011 at 12:05am
These combined should help a lot:
http://www.cplusplus.com/doc/tutorial/ntcs/ - Character arrays with a fixed size
http://www.cplusplus.com/doc/tutorial/dynamic/ - How to use new and delete to dynamically create an array of any size you want
http://www.cplusplus.com/reference/clibrary/cstring/ #include <cstring> to use functions with character arrays

If you understand that they are character arrays, and you understand how to allocate/deallocate them, then you should be able to use the functions provided in <cstring> with ease. Remember they were writeen in C originally, so they aren't very C++ish
Dec 7, 2011 at 1:48am
Thanks LB, but perhaps I should have been a little bit more specific with my question:

I need to read a name from a file ('master") into a c-string, and the following code reads the input from a cin
1
2
char name[21];
cin.get( name, 21, '\n' );


This is how I tried to alter it to function with reading from a file:
getline(master, empName[], '#');

now, I'm pretty sure that this is wrong, but can you tell me how to fix it because nothing else is working...
Dec 7, 2011 at 1:50am
And to make things easier for any other questions, this is the class that I'm working with.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Employee
{
  private:
    int id;             // employee ID
    char name[21];        // employee name in c-string
    double hourlyPay;   // pay per hour
    int numDeps;        // number of dependents
    int type;           // employee type

  public:
    Employee( int initId=0, char initName[], double initHourlyPay=0.0, int initNumDeps=0, int initType=0 );  // Constructor
    bool set(int newId, char newName[], double newHourlyPay, int newNumDeps, int newType);
    int getID() {return id;}
    char getName() {return name[21];}
    double getHourlyPay() {return hourlyPay;}
    int getNumDeps() {return numDeps;}
    int getType() {return type;}
};


I know that there's something wrong with the set function and constructor as well
Dec 7, 2011 at 3:32am
actually, if someone could just help me fix the constructor I'd be good, I've got the rest of it fixed:

Employee( int initId=0, char initName[], double initHourlyPay=0.0, int initNumDeps=0, int initType=0 );

The error I get is:
error: default argument missing for parameter 2 of 'Employee::Employee(int, char*, double, int, int)'

but when I initialize the c-string ; char initName[]={0} the compiler still has a problem with it.
Dec 7, 2011 at 6:22am
Just put arguments with default values 'last'. In your case, switch first and second argument order and it will work.
Topic archived. No new replies allowed.