• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

I can't display the right values from my structure

Salve !

Okay, I've got this structure, and once the user enters a few employees I'd like to show them all the employees that meet some criteria ( gender and payrate ).

But . . it's got two problems. The first is that i doesn't display the employees it's supposed to unless there's only one such person and they're entered last. The second is that the string that's supposed to hold the name isn't acting like a string, it's taking the first part of the name as the name and the last name as their gender ( the next input ). . . if you follow.

Here, I'll show you :

    struct employee
{
    char name[40];
    char gender;
    int age;
    long ssn;
    float payrate;
}; employee emp[10];

void company()
{
    employee employee[10];
    int e=0;
    char ans;
    while( ans !='n' )
    {
        cout << "\n\n Please enter your employe information.\n\n";
        cout << "Name: ";
        cin >> emp[e].name;
        cout << "Gender: ( F or M ) ";
        cin >> emp[e].gender;
        cout << "Age: ";
        cin >> emp[e].age;
        cout << "Social Security Number: ";
        cin >> emp[e].ssn;
        cout << "Payrate: ";
        cin >> emp[e].payrate;
        cout << "Would you like to add an employee ? Y/N ?";
        cin >> ans;
    }
    for( ans=='n'; e>=0; e++ )
    {
        if ( emp[e].gender='f', emp[e].payrate<10 )
            {
                        cout << emp[e].name;
            }    
        else
    {
        cout << "No females with payrate less than 10$ an hour. :) \n";
    }  
}
}


So, why does it only display the person if they're entered last, and why doesn't the string that holds the name accept the space between the first and second part of the employees name as part of the name ?
0
princess_of_reason
Asked:
princess_of_reason
  • 4
  • 4
  • 4
  • +3
5 Solutions
 
brettmjohnsonCommented:
You never increment e when inputting the records, so you
repeatedly fill in emp[0].

Also, ans is uninitialized on the first iteration of the while loop.
Either initialized it at local variable definition, or change the loop to a
do {} while(); loop

0
 
Sys_ProgCommented:
A couple of more problems once u fix the above mentioned fixes

1. The for loop would never end
for( ans=='n'; e>=0; e++ )
The for loop should start from e = 0 to whatever be the upper limit

2.
if ( emp[e].gender='f', emp[e].payrate<10 )
should probably be
if ( emp[e].gender== 'f' &&  emp[e].payrate<10 )


Amit



0
 
Sys_ProgCommented:
A better way to implement what u want shouldbe using vector instead of the array that u have used
Since u have declared an array of 10 emps but u may have less/more emps enetered which is goverened by the char value enetered in ans
i.e. while( ans !='n' )

Also, in exosting implementation,
employee employee[10];

seems to be extra

Amit
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
SteHCommented:
cin will stop parsing on any whitespace: space, tab, and newline. To have a string with both names you have to pass them to two variables:

string cname, name
cin >> cname >> name;
emp[e].name = cname + " " + name;

And add e++ in the while loop as brettmjohnson suggested. Without it you repeatedly fill emp[0] and thus can only access the last entered.

Does the line
    employee employee[10];
compile? It should not since type and name are identical.

The while loop should terminate when emp[9] was filled. Writing to emp[10] will cause problems so check

while ((ans != 'n') && (e < 10)) {
0
 
SteHCommented:
Hi SysProg,

another way to assure the last loop ends is to decrement e in the for loop:
    for(* ; e>=0; e-- )
since e is already at the max.

*)Depending on where e is incremented in the while loop before you need to decrement it here. Thus replace * with e--. Else simply remove *.
0
 
princess_of_reasonAuthor Commented:
Okay, I tried incrementing on each pass of the while loop . . so now it looks like this :

    do
    {
        cout << "\n\n Please enter your employees information.\n\n";
        cout << "Name: ";
        cin >> emp[e].name;
        cout << "Gender: ( F or M ) ";
        cin >> emp[e].gender;
        cout << "Age: ";
        cin >> emp[e].age;
        cout << "Social Security Number: ";
        cin >> emp[e].ssn;
        cout << "Payrate: ";
        cin >> emp[e].payrate;
        e = e++ ;
        cout << "\n Would you like to add an employee ? Y/N ?";
        cin >> ans;
        ans = toupper(ans);
    } while ((ans != 'N') && (e < 10));

        if ( emp[e].gender== 'f' &&  emp[e].payrate<10 )
            {
                        cout << emp[e].name;
            }    
        else
    {
        cout << "\n No females with payrate less than 10$ an hour. :) \n";
    }

But now it doesn't return anyone at all.   :(  
0
 
SteHCommented:
At least to me the second (for loop) is missing. You only test the tenth element which in addition does not exist:

Change this to

 for (e--; e>=0;e--) { // start with one less than e is after the while loop since it was incremented after the last add.
      if ( emp[e].gender== 'f' &&  emp[e].payrate<10 )
            {
                        cout << emp[e].name;
            }    
        else
    {
        cout << "\n No females with payrate less than 10$ an hour. :) \n";
    }
}
0
 
Sys_ProgCommented:
>>  e = e++ ;
should be

e++ ;

Also,
your do while loop would now accept 10 employees (which would be stored in 0th to 9th location)
Once e becomes 10, it exits out of the do loop

Now, u are displaying cout << emp [e].name ;
However, the value of e is 10 at this point
So, nothing is diaplyed snce u have exceedded the array size and nothing is present there

After the do while loop, u need to run a for loop

for ( e= 0 ; e < 10 ; e ++ ) {
       if (  ....) {
              cout << emp[e].name ;
      }
     else {
     }
}


0
 
SteHCommented:
Two smaller comments
e = e++;
is equal to
e++;
Latter will increment the content of e and assigning it back to e is not needed since its already the content of e.

It could be worth to compare the gender of the employee after making it lower or uppercase. Same as you did it for ans.
0
 
SkonenCommented:
   e = 0;

    do
    {
        cout << "\n\n Please enter your employees information.\n\n";
        cout << "Name: ";
        cin >> emp[e].name;
        cout << "Gender: ( F or M ) ";
        cin >> emp[e].gender;
        cout << "Age: ";
        cin >> emp[e].age;
        cout << "Social Security Number: ";
        cin >> emp[e].ssn;
        cout << "Payrate: ";
        cin >> emp[e].payrate;      
        cout << "\n Would you like to add an employee ? Y/N ?";
        cin >> ans;

    } while (toupper(ans) != 'N' && e++ < 10);

    for (e--; e >= 0; e--) {

             if ( toupper(emp[e].gender )== 'F'  &&  emp[e].payrate < 10 ) {
                        cout << emp[e].name;
             }   else  {
                        cout << "\n No females with payrate less than 10$ an hour. :) \n";
             }
    }

That may be what you are looking for, it could definately be more efficient and compact but if appears you like it in this form. Also, you're not displaying the male employee names, perhaps you should use:


if (  ((emp[e].gender = toupper(emp[e].gender))== 'F'  &&  emp[e].payrate < 10) ||  emp[e].gender == 'M') {
0
 
Sys_ProgCommented:
Obviously, the "C++" way to implement this could be to use write an Employee class and then have a vector declared of Employee type

Amit
0
 
pankajtiwaryCommented:
Hi princess_of_reason,

The place where you are checking whether the gender is male or female, that place you are just checking the (last +1)th employee information because the last employee is stored in (e+1)th index as there is no loop. So, that chek should be there in the loop and inside the loop you can check if there are any female employees, so that the final message can be printed. The function may be changed as follows:

void company()
{
    employee emp[10];
    int e=0;
    bool first_entry = true, no_entry = true; // first_entry is for the header message, no_entry is for the message when no one satisfies the criteria
    char ans;
    do
    {
        cout << "\n\n Please enter your employees information.\n\n";
        cout << "Name: ";
        cin >> emp[e].name;
        cout << "Gender: ( F or M ) ";
        cin >> emp[e].gender;
        cout << "Age: ";
        cin >> emp[e].age;
        cout << "Social Security Number: ";
        cin >> emp[e].ssn;
        cout << "Payrate: ";
        cin >> emp[e].payrate;
        e++ ;
        cout << "\n Would you like to add an employee ? Y/N ?";
        cin >> ans;
        ans = toupper(ans);
    } while ((ans != 'N') && (e < 10));

    for ( int i = 0 ; i < e ; i ++ ) {
        if ( emp[i].gender== 'f' &&  emp[i].payrate<10 )
        {
            no_entry = false;
            if ( first_entry) {
                cout << "The following employees satisfy the criteria" << endl ;
                first_entry = false ;
            }
            cout << emp[i].name << endl;
        }
    }
    if ( no_entry )
    {
        cout << "\n No females with payrate less than 10$ an hour. :) \n";
    }
    return ;
}


Cheers!
0
 
princess_of_reasonAuthor Commented:
Okay, thanks everyone ! :)

I get why the loop wasn't incrementing, and that's fixed, and now I almost understand why the employee[].name wouldn't keep the space ( though I don't completely understand how to fix that ) . .

Only one last thing I don't get, and that has to do with the final output. It prints the "no females with payrate" thing right alongside the names of the females who do indeed meet the criteria.

Why does it do that  ? I have that set as an "else", isn't it supposed to do one or the other ?
0
 
princess_of_reasonAuthor Commented:
. . . and why won't it display someone who meets the criteria if only one employee is entered ?

:?
0
 
princess_of_reasonAuthor Commented:
Okay, nevermind that, I've figured it out pretty much.

Thanks y'all ! Splitting points now.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 4
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now