Another question about c-strings

Pages: 12
I'm reading from this file ('master'):
5 Christine Kim (spaces) 30.00 2 1 F


And I while I can read the file correctly into these variables:
1
2
3
4
5
6
7
8
9
10
master >> masId;
	master.ignore();
	master.get(empName, 21);
	master.clear();
	master >> empHourlyPay;
	master >> empNumDeps;
	master >> empType;
	master.ignore();
	master >> check[0].sex;
	info[0].set(masId, empName, empHourlyPay, empNumDeps, empType);


and I'm trying to use this given class:

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
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[]=0, 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;}
};

Employee::Employee( int initId, char initName[], double initHourlyPay, int initNumDeps, int initType)
	{
		bool status = set( initId, initName, initHourlyPay, initNumDeps, initType );
		if ( !status )
		{
			id = 0;
			strcpy(name, " ");
   			hourlyPay = 0.0;
   			numDeps = 0;
   			type = 0;
		}
	}

bool Employee::set( int newId, char newName[], double newHourlyPay, int newNumDeps, int newType )
{
  bool status = false;

  if ( newId > 0 && newHourlyPay > 0 && newNumDeps >= 0 && newType >= 0 && newType <= 1 )
  {
    status = true;
    id = newId;
    strcpy(newName, name);
    hourlyPay = newHourlyPay;
    numDeps = newNumDeps;
    type = newType;
  }
  return status;
}


and it's setting the name to a single blank space while the other numbers are set correctly.
I guess I should be asking why the strcpy(empName, name); isn't working
strcpy_s()

unresolved function
strcpy_s() is a non-standard function!

Shouldn't it be strcpy(name, newName);?
shouldn't it be strcpy(name, newName);?


That's what I had initially, but it still didn't work.
uhm, I've always used strcpy_s(). And as far as I remember, I always get weird results with strcpy :/
That's what I had initially, but it still didn't work.

It should be strcpy(name, newName);.

I think the problem is that newName doesn't have a null terminator. Before you read empName from the file, I would ensure that it has been zeroed out first.

Try doing:

memset(empName, 0, 21);

before reading it.
Last edited on
it's been initialized to zero

char empName[21] = {0}; // employee name
in that case you should only read 20 characters so that the last char is not overwritten
master.get(empName, 20);
Last edited on
still nothing, there's got to be something else that I'm missing or not understanding.

new code:
1
2
3
4
5
6
7
8
9
10
11
master >> masId;
		master.ignore();
		memset(empName, 0, 21);
		master.get(empName, 20);
		master.clear();
		master >> empHourlyPay;
		master >> empNumDeps;
		master >> empType;
		master.ignore();
		master >> sex;
		info[i].set(masId, empName, empHourlyPay, empNumDeps, empType);
Last edited on
Reading the documentation it looks like master.get(empName, 21); should work after all.

How did you check that name only contained a single blank space?
it's been initialized to zero

char empName[21] = {0}; // employee name


Why don't you make the array = NULL?
char empName[21] = { '\0' };
The name, in total, contains 20 characters.

the file is this (minus the '-', the '-' are spaces):
-5-Christine Kim-------30.0-2-1-F

Even with the array=NULL, no output from the set function
Change the char to a string and use getline( master, empName, ' ' );? lol

I can't, I'm required to use a c-string.

It's my last homework assignment and this thing is just throwing me off
Have you tried adding a delimeter to the call?
master.get( empName, 20, ' ' );

Also, Have you tried to cout empName after getting it from the file, to see whether or not it's correct?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
master >> masId;
		master.ignore();
		memset(empName, 0, 21);
		master.get(empName, 20);

		std::cout << "empName:\t" << empName << '\n';

		master.clear();
		master >> empHourlyPay;
		master >> empNumDeps;
		master >> empType;
		master.ignore();
		master >> sex;
		info[i].set(masId, empName, empHourlyPay, empNumDeps, empType);
With the master.get(empName, 20, ' '); it gets "Christine" and not the full name because there is a space between the first and last name.

edit: when I tried it again with the code above, and printed it it game me "Christine" and a bunch of empty lines until it got to the next part of the program that I'm working on.

checking if the name was being read from the file correctly was the first thing that I checked before asking.
Last edited on
closed account (zwA4jE8b)
what about using fgets()?
Are you allowed to edit the file to be read in any way?

Baring that, I know it's a pain in the ass, but writing a function so you can use master.get(empName, 20, ' '); twice to, two seperate variables and then adding them together.

EDIT:
ahaha I said it was a pain in the ass and then wrote the function for you!! But as I said below, I wrote it in firefox and haven't tested it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
func( char empName1[], char empName2[] )
{
    int length_1 = strlen( empName1 );
    int startAtThisLocation = length_1 + 1;

    //add the space between names
    empName1[ startAtThisLocation ] = ' ';
    ++startAtThisLocation;

    int endOfName = ( length_1 + ( strlen( empName2 + 1 ) );

    int itorator = 0;

    for( int i = startAtThisLocation; i < endOfName; ++i )
    {
        empName1[ i ] = empName2[ itorator ];

        ++itorator;
    }
}


I wrote this in firefox, could be wrong, lol.
Last edited on
Pages: 12