Structures and reference...

As part of a decent size project I am doing for school I need to zero an entire array of structures although was having a lot of trouble, could anyone please point me in the right direction?

Here is the structure (very simple):
1
2
3
4
5
struct employee         //employee prototype
{
       char name[20];
       float sales[12];
};


The array I need to initialize is of size 10.

And here is my function to zero the entire array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void zerorecord(employee a[])             //given an array zero all digits of each entry
{
     for(int i=0;i<10;i++)    
     {
             for(int k=0;k<20;k++)
             {
                     
                     a[i].name[k]='\0';
             }
             
             for(int j=0;j<12;j++)
             {
                     a[i].sales[j]=0;
             }
     }
}


I am unsure of how to pass the array directly which is what I need to do. I am attempting to make all 10 e.name arrays equal '\0' initially then make all 12 e.sales for each of them 0 initially.

Here is a link to the full program(I am not posting it directly because I feel most of it wont be needed):
http://pastebin.com/m57349eee

Currently the program will compile although it gives a run time error immediatly after running (which I assume to be due to the comparison to a value that isnt assigned; something I think might be corrected once I fix my function.
All help will be greatly appreciated.
1
2
3
4
#include <cstring>
//this goes in an init() function in employee
memset(this->name,0,sizeof(char)*20);
memset(this->sales,0,sizeof(float)*12);

To zero all elements in the array, just traverse the array while calling init() in every iteration.
Sadly I think I would get a 0 for that method. Our course is really retarded and we aren't allowed to use (proper) methods that aren't found in the textbook. The method I have attempted above is following their rough guidlines, although I need to find a way to reference.
Oh, sorry. I misunderstood your question.

Does the error happen before or after the file is opened?

In zerorecord(), n appears to be undefined. How can that compile? (Yeah, confirmed. GCC complains.)

Why do you open "outfile.txt" and not do any output to it?
Sorry that was my fault. that was an old version of paste bin, the current one has 10 instead of n. It compiles fine in the current build. It opens fine, and runs fine until
1
2
3
4
5
6
7
8
9
10
11
12
13
14
          for(i=0;i<10;i++)
          {
               if (strcmp(e[i].name,name)==0)
               {
                    e[i].sales[month=1]+=revenue;
                    break;
               }
               
               else if(strcmp(e[i].name,'\0')==0)
               {
                    strcpy(e[i].name,name);
                    e[i].sales[month=1]+=revenue;
                    break;
               }


Where the error comes in is when it runs the else if (not unusual since none of the will initially be equal to a name since it is zero'd/blank). Do you think it is that my zerorecord function isnt passing the structures back to the main?

(Current build of program:
http://pastebin.com/m51d5091a)
Topic archived. No new replies allowed.