Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,923 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,937 people online right now. Registration is fast and FREE... Join Now!




Values not being displayed in function

2 Pages V  1 2 >  
Reply to this topicStart new topic

Values not being displayed in function, arrays only visable in main and admin_Options

djkaotix
4 Dec, 2007 - 10:28 AM
Post #1

New D.I.C Head
*

Joined: 16 Nov, 2007
Posts: 19


My Contributions
I've been working on this semester project for over two weeks, everyone on here has been very helpful. I've tested it out and it reports no errors. But however, i can not seem to get the values stored in the arrays when calling the separate functions. I tested to see if they were being stored in main after being entered in admin_Options and they show up if I add the code, but when I attempt to pass the arrays to the other functions, I either get garbage or nothing on my screen.

Can someone please overlook this... I've checked my code and can't seem to find where the problem is coming from, but maybe a second pair of eyes can help me find it quicker.

CODE

/*
  Name: Employee Records (list emps/lists emps by dept/calc avg/calc lowest)
        [supports up to 25 users]
  Author: Gus Diaz
  Description: This program is designed to store employee records, including salary and dept information.
               The user interface allows you to query by dept, salary status, or list all employees.              
              
  Input: admin_Choice, empNames[], salaries[], depts[], addRecord, searchFor, choice, deptChoice
  Processing: function empList -- lists all employee's
              function empByDept -- displays employee's from a dept query
              function calcAvgSal -- calculates the avg. employee salary
              function calcLowSal -- calculates the lowest salary
              function admin_Options -- user menu to add and modify employee's
  Output: list of all employees,
          list of employees from a specific dept,
          display of avg salary,
          display of lowest salary.
*/

#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <cmath>

using namespace std;

//function prototype
void empList(char [], double [], char [], int);
void empByDept(char, char [], char [], int);
double calcAvgSal(double [], int);
double calcLowSal(double [], int);
int admin_Options(char [], double [], char [], int);

int admin_Options(char empNames[], double salaries[], char depts[], int count)//options for adding or modifying current employees
{    
    int admin_Choice = 0;
    int addRecord;
    char searchFor = ' ';
    int i = 0;  
    
    do
    {
    system ("cls");    
    //set num to maximum number of elements in array
    int num = 25;
    
    //display admin menu
    cout << "Administrative Options\n";
    cout << endl;
    cout << "1. Add new employee record\n\n";
    cout << "0. Return to previous menu\n";
    cout <<endl;
    cout << "Choice:";
    cin >> admin_Choice;
        
    switch (admin_Choice){
     case 1: //enter in new emp. data
    
     //check to see if array is full
     if (count >=25){
     cout << "Max number of records is 25!\n";
     system("pause");
     break;
     }//end if
    
     else
     {
     cout << endl;
     cout << "Please enter employee's last name: ";
     cin >> empNames[i];
     cin.ignore(100,'\n');
     cout << endl;
     cout << "Please enter employee's salary: ";
     cin >> salaries[i];
     cin.ignore(100,'\n');
     cout << endl;
     cout << "Please enter employee's department: ";
     cin >> depts[i];
     cin.ignore(100,'\n');
     cout << endl;
     count = count + 1;
     cout << "Would you like to add another record? (1-YES or 2-NO)\n";
     cin >> addRecord;
        
     do {        
           if((addRecord == 1) && (count <25)){
           i+=1;
           cout << endl;
           cout << "Please enter employee's last name: ";
           cin >> empNames[i];
           cin.ignore(100,'\n');
           cout << endl;
           cout << "Please enter employee's salary: ";
           cin >> salaries[i];
           cout << endl;
           cout << "Please enter employee's department: ";
           cin >> depts[i];
           cin.ignore(100,'\n');
           cout << endl;
           count = count + 1;
           cout << "Would you like to add another record? (YES or NO)\n";
           cin >> addRecord;
           cin.ignore(100,'\n');
           addRecord = toupper(addRecord);
           }//end if
          
           else if(addRecord = 2){
                break;
           }//end else
           if (count <25){
           cout << "Max number of records is 25!\n";
           system("pause");
           break;
           }//end if
          
           else
           break;                      
     }while (count <25);//end do
        
     case 0: //return to previous menu
     break;
              
     default:
     cout << endl;
     cout << "Your input was invalid & could not be processed.\n";  
     }//end of switch
     }
}while (admin_Choice !=0);//end of do while
return empNames[i], salaries[i], depts[i], count;
}//end admin_Options function


int main()
{
      //declare constants and variables
      int choice = 0;
      char deptChoice = ' ';
      char empNames[25] = {' '};
      double salaries[25] = {0.0};
      char depts[25] = {' '};
      int i = 0;
      int count;//employee counter
      
      do{
      //display menu
      system ("cls");
      cout << "Welcome to _______ \n";
      cout << "Please chose from the following menu options:\n\n";
      cout << "1. List all employees with their data (name, department, salary)\n";
      cout << "2. Find all employees in a certain department.\n";
      cout << "3. Calculate the average salary for all employees.\n";
      cout << "4. Calculate the lowest salary.\n\n";
      cout << "9. Administrative options. (Add Employee(s))\n";
      cout << "0. Exit\n\n";
      cout << "Choice:";
      cin >> choice;
      
      switch (choice){
       case 1: //lists all employees along with their data
            empList(empNames, salaries, depts, count);
            system ("pause");
            break;
       case 2: //list employees for specific dept.
            if (count < 1)
            {
                 cout << "No Records to display. Try entering employee data first!\n";
                 system ("pause");
                 break;
            }//end if
            else
            {
                cout << "Please type the name of the department you wish to query, from the following list:\n";
                for (i=0; i<count; i+=1)
                {
                cout << depts[i];
                }
            cout << endl;
            cin >> deptChoice;
            cin.ignore(100,'\n');
            //displays all employees from user selected dept.
            cout << "List of all employees from " << deptChoice << ":\n";
            empByDept(deptChoice, empNames, depts, count);
            system ("pause");
            break;
            }//end else
       case 3: //calculate the avg. salary
            cout << "Average employee salary: $" << calcAvgSal(salaries, count) << endl;
            system ("pause");
            break;
       case 4: //calculate the lowest salary
            cout << "Lowest paid employee's salary: $" << calcLowSal(salaries, count) << endl;
            system ("pause");
            break;
       case 9: //administrative options
            system("cls"); //clears screen for administrative options
            cout << admin_Options(empNames, salaries, depts, count);
            break;
       case 0: //exit
            cout << "Goodbye!\n";
            exit (3);
       }
       }while (choice !=0);//end do while      
}//end main function

void empList(char empNames[], double salaries[], char depts[], int count)//lists all employees
{for (int i=0; i<count; i+=1)
  
    if (count < 1)
    {
        cout << "No Records to display. Try entering employee data first!\n";
        system ("pause");
        break;            
    }//end if
    else
    {
    cout << "List of all employees:\n";
    cout << "Employee: " << empNames[i] << "  Salary: " << salaries[i] << "  Department: " << depts[i] << endl;
    }//end else
}//end EmpList function

void empByDept(char deptChoice, char empNames[], char depts[], int count)//list all employees assigned to a specific department
{for (int i=0; i<count; i+=1)
  
    if (count < 1)
    {
        cout << "No Records to display. Try entering employee data first!\n";
        system ("pause");
        break;
    }//end if
    else
    {
    if (depts[i] = deptChoice)
    cout << "Name: " << empNames[i] << endl;
    }//end else
}//end of EmpByDept function

double calcAvgSal(double salaries[], int count)//calculates the average salary of all employees
{for (int i=0; i<count; i+=1)
  
    if (count < 1)
    {
        cout << "No Records to display. Try entering employee data first!\n";
        system ("pause");
        break;
    }//end if
    else
    {
    double avg_Sal = 0.00;
    avg_Sal = avg_Sal + salaries[i];
    return avg_Sal;
    }//end else
}//end calcAvgSal function

double calcLowSal(double salaries[], int count)//calculates the lowest salary of all paid employees
{for (int i=0; i<count; i+=1)
  
    if (count < 1)
    {
        cout << "No Records to display. Try entering employee data first!\n";
        system ("pause");
        break;
    }//end if
    else
    {  
       double lowest = 0.00;
       if (salaries[i] > lowest) lowest = salaries[i];
       return lowest;
    }//end else
}//end calcLowSal function

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Values Not Being Displayed In Function
4 Dec, 2007 - 11:38 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Lots and lots of things going wrong here. Hopefully I can give you a good start on understanding what is going on here. First I am going to assume you are not coding this in a compiler like VC++ because upon running this I kept getting errors referring to your count variable not being initialized before being used. So just for completeness sake, make sure you set your int count = 0;.

So to test your theory about passing arrays, I decided to take the least complex of your functions and used number 4, calculating the lowest salary. Upon looking at the function I noticed it was going to give you bad results just because you were comparing each salary to the lowest and taking the "highest" salary. Remember, to find the lowest it has to be lower than the previous lowest value. So I recoded your lowest salary function to look like this...

CODE

double calcLowSal(double salaries[], int count)//calculates the lowest salary of all paid employees
{
    if (count < 1) {
        cout << "No Records to display. Try entering employee data first!\n";
        system("pause");
        return 0.0;
    }
    
    double lowest = salaries[0];

    for (int i=1; i < count; i+=1) {
        if (salaries[i] < lowest) { lowest = salaries[i]; }
    }

    return lowest;
}//end calcLowSal function


As you can see, I immediately test the count value before continuing. If it is less than one, no use going into our loop. I then make sure to return a value of 0.0 if that is the case (make sure all paths of execution result in a return of a result).

I then take the first salary of the salaries array and go into a loop starting from the next salary on comparing it to see if it is LOWER than the previous value. If it is, then I set the lowest to that salary value. At the end, lowest has the lowest value from the salary array.

Ok, with that out of the way, I wanted to see if your salaries array was being passed correctly. Guess what, it is. You salaries array wasn't the problem here. It was your count variable! You added to count in admin_options when you were adding employees, but you forgot one subtle thing... because you have created count in main, you should have passed count by reference so that every time you incremented count in admin_options, it would be reflected back in main. You were adding to a COPY of count in admin_options. So after the function was done, main's version of count was still 0. Then guess what? You passed on count = 0 to your salaries function and your function was screwing up.

So how do we fix this? Simple, your count parameter to admin_options needed an ampersand sign.

CODE

int admin_Options(char empNames[], double salaries[], char depts[], int &count)//options for adding or modifying current employees
{  
     ....
}


Notice how count has an ampersand in front of the name. This tells the function not to make a copy of count, but use the address of the previous count defined back in main. So when we increment it, we are incrementing the count back in main.

Make sure this change is also reflected in the declaration of the function....

CODE

// Declaration for admin_Options
int admin_Options(char [], double [], char [], int &);


Another thing is how you setup your employee names and departments. You created arrays of chars, not an array of character strings. So when you type in the last name of "Doe" it would only take the "D" and throw away the rest.

You need to setup your array of names like so...

CODE

// Create a pointer array of 25 character string pointers
char **empNames = new char*[25];

// Set each array pointer to point to a new character string of length 25.
for (int i = 0; i < 25; i++) {
     empNames[i] = new char[25];
}


So what does this mean for your function signatures and declarations? For empName instead of empName[] you will need to have char **empName. Make your declarations match.

Once you do that, you will start seeing your names come out correctly. Btw... after all these fixes mentioned, it looks like it will also fix your listing of employees and some of the other functions are coming online as well. So make the changes and fully test everything. Then you will be ready to continue debugging the rest (like checking empByDept... you use a single = instead of == when checking deptChoice).

The fully modified code is below...

CODE


/*
  Name: Employee Records (list emps/lists emps by dept/calc avg/calc lowest)
        [supports up to 25 users]
  Author: Gus Diaz
  Description: This program is designed to store employee records, including salary and dept information.
               The user interface allows you to query by dept, salary status, or list all employees.              
              
  Input: admin_Choice, empNames[], salaries[], depts[], addRecord, searchFor, choice, deptChoice
  Processing: function empList -- lists all employee's
              function empByDept -- displays employee's from a dept query
              function calcAvgSal -- calculates the avg. employee salary
              function calcLowSal -- calculates the lowest salary
              function admin_Options -- user menu to add and modify employee's
  Output: list of all employees,
          list of employees from a specific dept,
          display of avg salary,
          display of lowest salary.
*/

#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <cmath>

using namespace std;

//function prototype
void empList(char **, double [], char [], int);
void empByDept(char, char **, char [], int);
double calcAvgSal(double [], int);
double calcLowSal(double [], int);
int admin_Options(char **, double [], char [], int &);

int admin_Options(char **empNames, double salaries[], char depts[], int &count)//options for adding or modifying current employees
{    
    int admin_Choice = 0;
    int addRecord;
    char searchFor = ' ';
    int i = 0;  
    
    do
    {
    //system ("cls");    
    //set num to maximum number of elements in array
    int num = 25;
    
    //display admin menu
    cout << "Administrative Options\n";
    cout << endl;
    cout << "1. Add new employee record\n\n";
    cout << "0. Return to previous menu\n";
    cout <<endl;
    cout << "Choice:";
    cin >> admin_Choice;
        
    switch (admin_Choice){
     case 1: //enter in new emp. data
    
     //check to see if array is full
     if (count >=25){
     cout << "Max number of records is 25!\n";
     system("pause");
     break;
     }//end if
    
     else
     {
     cout << endl;
     cout << "Please enter employee's last name: ";
     cin >> empNames[i];
     cin.ignore(100,'\n');
     cout << endl;
     cout << "Please enter employee's salary: ";
     cin >> salaries[i];
     cin.ignore(100,'\n');
     cout << endl;
     cout << "Please enter employee's department: ";
     cin >> depts[i];
     cin.ignore(100,'\n');
     cout << endl;
     count = count + 1;
     cout << "Would you like to add another record? (1-YES or 2-NO)\n";
     cin >> addRecord;
        
     do {        
           if((addRecord == 1) && (count <25)){
           i+=1;
           cout << endl;
           cout << "Please enter employee's last name: ";
           cin >> empNames[i];
           cin.ignore(100,'\n');
           cout << endl;
           cout << "Please enter employee's salary: ";
           cin >> salaries[i];
           cout << endl;
           cout << "Please enter employee's department: ";
           cin >> depts[i];
           cin.ignore(100,'\n');
           cout << endl;
           count = count + 1;
           cout << "Would you like to add another record? (YES or NO)\n";
           cin >> addRecord;
           cin.ignore(100,'\n');
           addRecord = toupper(addRecord);
           }//end if
          
           else if(addRecord = 2){
                break;
           }//end else
           if (count <25){
           cout << "Max number of records is 25!\n";
           system("pause");
           break;
           }//end if
          
           else
           break;                      
     }while (count <25);//end do
        
     case 0: //return to previous menu
     break;
              
     default:
     cout << endl;
     cout << "Your input was invalid & could not be processed.\n";  
     }//end of switch
     }
}while (admin_Choice !=0);//end of do while
return empNames[i], salaries[i], depts[i], count;
}//end admin_Options function


int main()
{
      //declare constants and variables
      int choice = 0;
      char deptChoice = ' ';
      char **empNames = new char*[25];

      for (int i = 0; i < 25; i++) {
          empNames[i] = new char[25];
      }
      double salaries[25] = {0.0};
      char depts[25] = {' '};
      int i = 0;
      int count = 0;//employee counter
      
      do{
      //display menu
      system ("cls");
      cout << "Welcome to _______ \n";
      cout << "Please chose from the following menu options:\n\n";
      cout << "1. List all employees with their data (name, department, salary)\n";
      cout << "2. Find all employees in a certain department.\n";
      cout << "3. Calculate the average salary for all employees.\n";
      cout << "4. Calculate the lowest salary.\n\n";
      cout << "9. Administrative options. (Add Employee(s))\n";
      cout << "0. Exit\n\n";
      cout << "Choice:";
      cin >> choice;
      
      switch (choice){
       case 1: //lists all employees along with their data
            empList(empNames, salaries, depts, count);
            system ("pause");
            break;
       case 2: //list employees for specific dept.
            if (count < 1)
            {
                 cout << "No Records to display. Try entering employee data first!\n";
                 system ("pause");
                 break;
            }//end if
            else
            {
                cout << "Please type the name of the department you wish to query, from the following list:\n";
                for (i=0; i<count; i+=1)
                {
                cout << depts[i];
                }
            cout << endl;
            cin >> deptChoice;
            cin.ignore(100,'\n');
            //displays all employees from user selected dept.
            cout << "List of all employees from " << deptChoice << ":\n";
            empByDept(deptChoice, empNames, depts, count);
            system ("pause");
            break;
            }//end else
       case 3: //calculate the avg. salary
            cout << "Average employee salary: $" << calcAvgSal(salaries, count) << endl;
            system ("pause");
            break;
       case 4: //calculate the lowest salary
            cout << "Lowest paid employee's salary: $" << calcLowSal(salaries, count) << endl;
            system ("pause");
            break;
       case 9: //administrative options
            //system("cls"); //clears screen for administrative options
            cout << admin_Options(empNames, salaries, depts, count);
            break;
       case 0: //exit
            cout << "Goodbye!\n";
            exit (3);
       }
       }while (choice !=0);//end do while      
}//end main function

void empList(char **empNames, double salaries[], char depts[], int count)//lists all employees
{for (int i=0; i<count; i+=1)
  
    if (count < 1)
    {
        cout << "No Records to display. Try entering employee data first!\n";
        system ("pause");
        break;            
    }//end if
    else
    {
    cout << "List of all employees:\n";
    cout << "Employee: " << empNames[i] << "  Salary: " << salaries[i] << "  Department: " << depts[i] << endl;
    }//end else
}//end EmpList function

void empByDept(char deptChoice, char **empNames, char depts[], int count)//list all employees assigned to a specific department
{for (int i=0; i<count; i+=1)
  
    if (count < 1)
    {
        cout << "No Records to display. Try entering employee data first!\n";
        system ("pause");
        break;
    }//end if
    else
    {
    if (depts[i] == deptChoice)
    cout << "Name: " << empNames[i] << endl;
    }//end else
}//end of EmpByDept function

double calcAvgSal(double salaries[], int count)//calculates the average salary of all employees
{for (int i=0; i<count; i+=1)
  
    if (count < 1)
    {
        cout << "No Records to display. Try entering employee data first!\n";
        system ("pause");
        break;
    }//end if
    else
    {
    double avg_Sal = 0.00;
    avg_Sal = avg_Sal + salaries[i];
    return avg_Sal;
    }//end else
}//end calcAvgSal function

double calcLowSal(double salaries[], int count)//calculates the lowest salary of all paid employees
{
    if (count < 1) {
        cout << "No Records to display. Try entering employee data first!\n";
        system("pause");
        return 0.0;
    }
    
    double lowest = salaries[0];

    for (int i=1; i < count; i+=1) {
        if (salaries[i] < lowest) { lowest = salaries[i]; }
    }

    return lowest;
}//end calcLowSal function


Keep in mind that there is more work to do on this, but this should get you printing output in the correct format and such. Keep up the good work!

"At DIC we be code correcting, employee adding code ninjas!" decap.gif




User is offlineProfile CardPM
+Quote Post

djkaotix
RE: Values Not Being Displayed In Function
4 Dec, 2007 - 11:53 AM
Post #3

New D.I.C Head
*

Joined: 16 Nov, 2007
Posts: 19


My Contributions
Thanks alot for you help Marty. I'm eager to learn, but as I said... sometimes you need a second pair of trained eyes to spot the errors.

If there's anyway I can help you out on here, up your rep(don't know how), or paypal you a few $$ for helping me out, let me know.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Values Not Being Displayed In Function
4 Dec, 2007 - 12:13 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
We are volunteer and always willing to help. All we ever ask for is that you tell a few programmer friends about the site and a link to us on any site you may have. That way we can continue helping more and more people and share the knowledge!

Thanks again for helping us out. smile.gif
User is offlineProfile CardPM
+Quote Post

djkaotix
RE: Values Not Being Displayed In Function
4 Dec, 2007 - 06:54 PM
Post #5

New D.I.C Head
*

Joined: 16 Nov, 2007
Posts: 19


My Contributions
Thanks again to Marty for your help. I figured out the problem with entering in the names, now it's working almost flawless, except for the display empByDept. I can seem to parse those records correctly from the array.

Not only that, but only the first character is read. Why's it doing that?

CODE


/*
  Name: Employee Records (list emps/lists emps by dept/calc avg/calc lowest)
        [supports up to 25 users]
  Author: Gus Diaz
  Description: This program is designed to store employee records, including salary and dept information.
               The user interface allows you to query by dept, salary status, or list all employees.              
              
  Input: admin_Choice, empNames[], salaries[], depts[], addRecord, searchFor, choice, deptChoice
  Processing: function empList -- lists all employee's
              function empByDept -- displays employee's from a dept query
              function calcAvgSal -- calculates the avg. employee salary
              function calcLowSal -- calculates the lowest salary
              function admin_Options -- user menu to add and modify employee's
  Output: list of all employees,
          list of employees from a specific dept,
          display of avg salary,
          display of lowest salary.
*/

#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <cmath>

using namespace std;

//function prototype
void empList(char **, double [], char **, int);
void empByDept(char, char **, char **, int);
double calcAvgSal(double [], int);
double calcLowSal(double [], int);
int admin_Options(char **, double [], char **, int &);

int admin_Options(char **empNames, double salaries[], char **depts, int &count)//options for adding or modifying current employees
{    
    int admin_Choice = 0;
    int addRecord;
    char searchFor = ' ';
    int i = 0;  
    
    do
    {
    system ("cls");    
    //set num to maximum number of elements in array
    int num = 25;
    
    //display admin menu
    cout << "Administrative Options\n";
    cout << endl;
    cout << "1. Add new employee record\n\n";
    cout << "0. Return to previous menu\n";
    cout <<endl;
    cout << "Choice:";
    cin >> admin_Choice;
        
    switch (admin_Choice){
     case 1: //enter in new emp. data
    
     //check to see if array is full
     if (count >=25){
     cout << "Max number of records is 25!\n";
     system("pause");
     break;
     }//end if
    
     else
     {
     cout << endl;
     cout << "Please enter employee's last name: ";
     cin >> empNames[i];
     cin.ignore(100,'\n');
     cout << endl;
     cout << "Please enter employee's salary: ";
     cin >> salaries[i];
     cin.ignore(100,'\n');
     cout << endl;
     cout << "Please enter employee's department: ";
     cin >> depts[i];
     cin.ignore(100,'\n');
     cout << endl;
     count = count + 1;
     cout << "Would you like to add another record? (1-YES or 2-NO)\n";
     cin >> addRecord;
    
     do{
           i+=1;
           cout << endl;
           cout << "Please enter employee's last name: ";
           cin >> empNames[i];
           cin.ignore(100,'\n');
           cout << endl;
           cout << "Please enter employee's salary: ";
           cin >> salaries[i];
           cout << endl;
           cout << "Please enter employee's department: ";
           cin >> depts[i];
           cin.ignore(100,'\n');
           cout << endl;
           count = count + 1;
           cout << "Would you like to add another record? (1-YES or 2-NO)\n";
           cin >> addRecord;
           cin.ignore(100,'\n');
           addRecord = toupper(addRecord);
                              
          if (count >=25) {
             cout << "Max number of records is 25!\n";
             system("pause");
             break;
             }
     }while (addRecord !=2);//end do while
        
     case 0: //return to previous menu
     break;
              
     default:
     cout << endl;
     cout << "Your input was invalid & could not be processed.\n";  
     }//end of switch
     }
}while (admin_Choice !=0);//end of do while
return empNames[i], salaries[i], depts[i], count;
}//end admin_Options function


int main()
{
      //declare constants and variables
      int choice = 0;
      char deptChoice;
      char **empNames = new char*[25];

      for (int i = 0; i < 25; i++) {
          empNames[i] = new char[25];
      }
      double salaries[25] = {0.0};
      char **depts = new char*[25];
      
      for (int i = 0; i < 25; i++) {
          depts[i] = new char[25];
      }
      int i = 0;
      int count = 0;//employee counter
      
      do{
      //display menu
      system ("cls");
      cout << "Welcome to _______ \n";
      cout << "Please chose from the following menu options:\n\n";
      cout << "1. List all employees with their data (name, department, salary)\n";
      cout << "2. Find all employees in a certain department.\n";
      cout << "3. Calculate the average salary for all employees.\n";
      cout << "4. Calculate the lowest salary.\n\n";
      cout << "9. Administrative options. (Add Employee(s))\n";
      cout << "0. Exit\n\n";
      cout << "Choice:";
      cin >> choice;
      
      switch (choice){
       case 1: //lists all employees along with their data
            cout << "List of all employees:\n";
            empList(empNames, salaries, depts, count);
            system ("pause");
            break;
       case 2: //list employees for specific dept.
            if (count < 1)
            {
                 cout << "No Records to display. Try entering employee data first!\n";
                 system ("pause");
                 break;
            }//end if
            else
            {
                cout << "Please type the name of the department you wish to query,\nfrom the following list:\n";
                for (int i=0; i<count; i+=1)
                {
                cout << depts[i] << endl;
                }
            cout << endl;
            cout << "Choice:";
            cin >> deptChoice;
            cin.ignore(100,'\n');
            //displays all employees from user selected dept.
            cout << "List of all employees from " << deptChoice << ":\n";
            empByDept(deptChoice, empNames, depts, count);
            system ("pause");
            break;
            }//end else
       case 3: //calculate the avg. salary
            cout << "Average employee salary: $" << calcAvgSal(salaries, count) << endl;
            system ("pause");
            break;
       case 4: //calculate the lowest salary
            cout << "Lowest paid employee's salary: $" << calcLowSal(salaries, count) << endl;
            system ("pause");
            break;
       case 9: //administrative options
            //system("cls"); //clears screen for administrative options
            cout << admin_Options(empNames, salaries, depts, count);
            break;
       case 0: //exit
            cout << "Goodbye!\n";
            exit (3);
       }
       }while (choice !=0);//end do while      
}//end main function

void empList(char **empNames, double salaries[], char **depts, int count)//lists all employees
{for (int i=0; i<count; i+=1)
  
    if (count < 1)
    {
        cout << "No Records to display. Try entering employee data first!\n";
        system ("pause");
        break;            
    }//end if
    else
    {
    cout << "Employee: " << empNames[i] << "  Salary: " << salaries[i] << "  Department: " << depts[i] << endl;
    }//end else
}//end EmpList function

void empByDept(char deptChoice, char **empNames, char **depts, int count)//list all employees assigned to a specific department
{for (int i=0; i<count; i+=1)
  
    if (count < 1)
    {
        cout << "No Records to display. Try entering employee data first!\n";
        system ("pause");
        break;
    }//end if
    else
    {
    if (deptChoice == **depts)
    cout << "Name: " << empNames[i] << endl;
    }//end else
}//end of EmpByDept function

double calcAvgSal(double salaries[], int count)//calculates the average salary of all employees
{
  double total_Sal = 0.00;
  double avg_Sal = 0.00;
  
  if (count < 1)
    {
        cout << "No Records to display. Try entering employee data first!\n";
        system ("pause");
    }//end if
    else
    {
    for (int i=0; i<count; i+=1)
    total_Sal = total_Sal + salaries[i];
    }//end else
    avg_Sal = total_Sal/count;
    return avg_Sal;
}//end calcAvgSal function

double calcLowSal(double salaries[], int count)//calculates the lowest salary of all paid employees
{
    if (count < 1) {
        cout << "No Records to display. Try entering employee data first!\n";
        system("pause");
        return 0.0;
    }
    
    double lowest = salaries[0];

    for (int i=0; i < count; i+=1) {
        if (salaries[i] < lowest) { lowest = salaries[i]; }
    }

    return lowest;
}//end calcLowSal function



This post has been edited by djkaotix: 4 Dec, 2007 - 06:56 PM
User is offlineProfile CardPM
+Quote Post

djkaotix
RE: Values Not Being Displayed In Function
4 Dec, 2007 - 07:37 PM
Post #6

New D.I.C Head
*

Joined: 16 Nov, 2007
Posts: 19


My Contributions
Thanks again to Marty for your help. I figured out the problem with entering in the names, now it's working almost flawless, except for the display empByDept. I can seem to parse those records correctly from the array.

Not only that, but only the first character is read. Why's it doing that?

CODE

/*
  Name: Employee Records (list emps/lists emps by dept/calc avg/calc lowest)
        [supports up to 25 users]
  Author: Gus Diaz
  Description: This program is designed to store employee records, including salary and dept information.
               The user interface allows you to query by dept, salary status, or list all employees.              
              
  Input: admin_Choice, empNames[], salaries[], depts[], addRecord, searchFor, choice, deptChoice
  Processing: function empList -- lists all employee's
              function empByDept -- displays employee's from a dept query
              function calcAvgSal -- calculates the avg. employee salary
              function calcLowSal -- calculates the lowest salary
              function admin_Options -- user menu to add and modify employee's
  Output: list of all employees,
          list of employees from a specific dept,
          display of avg salary,
          display of lowest salary.
*/

#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <cmath>

using namespace std;

//function prototype
void empList(char **, double [], char **, int);
void empByDept(char, char **, char **, int);
double calcAvgSal(double [], int);
double calcLowSal(double [], int);
int admin_Options(char **, double [], char **, int &);

int admin_Options(char **empNames, double salaries[], char **depts, int &count)//options for adding or modifying current employees
{    
    int admin_Choice = 0;
    int addRecord;
    char searchFor = ' ';
    int i = 0;  
    
    do
    {
    system ("cls");    
    //set num to maximum number of elements in array
    int num = 25;
    
    //display admin menu
    cout << "Administrative Options\n";
    cout << endl;
    cout << "1. Add new employee record\n\n";
    cout << "0. Return to previous menu\n";
    cout <<endl;
    cout << "Choice:";
    cin >> admin_Choice;
        
    switch (admin_Choice){
     case 1: //enter in new emp. data
    
     //check to see if array is full
     if (count >=25){
     cout << "Max number of records is 25!\n";
     system("pause");
     break;
     }//end if
    
     else
     {
     cout << endl;
     cout << "Please enter employee's last name: ";
     cin >> empNames[i];
     cin.ignore(100,'\n');
     cout << endl;
     cout << "Please enter employee's salary: ";
     cin >> salaries[i];
     cin.ignore(100,'\n');
     cout << endl;
     cout << "Please enter employee's department: ";
     cin >> depts[i];
     cin.ignore(100,'\n');
     cout << endl;
     count = count + 1;
     cout << "Would you like to add another record? (1-YES or 2-NO)\n";
     cin >> addRecord;
    
     do{
           i+=1;
           cout << endl;
           cout << "Please enter employee's last name: ";
           cin >> empNames[i];
           cin.ignore(100,'\n');
           cout << endl;
           cout << "Please enter employee's salary: ";
           cin >> salaries[i];
           cout << endl;
           cout << "Please enter employee's department: ";
           cin >> depts[i];
           cin.ignore(100,'\n');
           cout << endl;
           count = count + 1;
           cout << "Would you like to add another record? (1-YES or 2-NO)\n";
           cin >> addRecord;
           cin.ignore(100,'\n');
           addRecord = toupper(addRecord);
                              
          if (count >=25) {
             cout << "Max number of records is 25!\n";
             system("pause");
             break;
             }
     }while (addRecord !=2);//end do while
        
     case 0: //return to previous menu
     break;
              
     default:
     cout << endl;
     cout << "Your input was invalid & could not be processed.\n";  
     }//end of switch
     }
}while (admin_Choice !=0);//end of do while
return empNames[i], salaries[i], depts[i], count;
}//end admin_Options function


int main()
{
      //declare constants and variables
      int choice = 0;
      char deptChoice;
      char **empNames = new char*[25];

      for (int i = 0; i < 25; i++) {
          empNames[i] = new char[25];
      }
      double salaries[25] = {0.0};
      char **depts = new char*[25];
      
      for (int i = 0; i < 25; i++) {
          depts[i] = new char[25];
      }
      int i = 0;
      int count = 0;//employee counter
      
      do{
      //display menu
      system ("cls");
      cout << "Welcome to _______ \n";
      cout << "Please chose from the following menu options:\n\n";
      cout << "1. List all employees with their data (name, department, salary)\n";
      cout << "2. Find all employees in a certain department.\n";
      cout << "3. Calculate the average salary for all employees.\n";
      cout << "4. Calculate the lowest salary.\n\n";
      cout << "9. Administrative options. (Add Employee(s))\n";
      cout << "0. Exit\n\n";
      cout << "Choice:";
      cin >> choice;
      
      switch (choice){
       case 1: //lists all employees along with their data
            cout << "List of all employees:\n";
            empList&#