Join 149,537 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,521 people online right now. Registration is fast and FREE... Join Now!
Compiler: Default compiler Executing g++.exe... g++.exe "G:\Documents\Student Class Work\Class Project Diaz.cpp" -o "G:\Documents\Student Class Work\Class Project Diaz.exe" -g3 -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3 G:\Documents\Student Class Work\Class Project Diaz.cpp: In function `int main()': G:\Documents\Student Class Work\Class Project Diaz.cpp:144: error: expected primary-expression before ']' token
G:\Documents\Student Class Work\Class Project Diaz.cpp:144: error: expected primary-expression before ']' token G:\Documents\Student Class Work\Class Project Diaz.cpp:144: error: expected primary-expression before ']' token
G:\Documents\Student Class Work\Class Project Diaz.cpp:155: error: expected primary-expression before ']' token G:\Documents\Student Class Work\Class Project Diaz.cpp:157: error: expected primary-expression before ']' token
G:\Documents\Student Class Work\Class Project Diaz.cpp:159: error: expected primary-expression before ']' token G:\Documents\Student Class Work\Class Project Diaz.cpp:162: error: expected primary-expression before ']' token G:\Documents\Student Class Work\Class Project Diaz.cpp:162: error: expected primary-expression before ']' token G:\Documents\Student Class Work\Class Project Diaz.cpp:162: error: expected primary-expression before ']' token
Execution terminated
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 quere by dept, salary status, or list all employees.
int admin_Options(char empNames[], double salaries[], char depts[], int count)//options for adding or modifying current employees { int admin_Choice = 0; char addRecord; char empName; double salary; char dept; char searchFor = ' '; int i;
//set num to maximum number of elements in array int num = 25;
switch (admin_Choice){ case 1: //enter in new emp. data cout << "Please enter employee's name: "; cin >> empName; cout << endl; cout << "Please enter employee's salary: "; cin >> salary; cout << endl; cout << "Please enter employee's department: "; cin >> dept; cout << endl; count = count + 1; cout << "Would you like to add another record? (YES or NO)\n"; cin >> addRecord; addRecord = toupper(addRecord);
while (addRecord = "YES" && i <num) { empNames[i] = empName; salaries[i] = salary; depts[i] = dept; i+=1; if (i<num) { cout << "Please enter employee's name: "; cin >> empName; cout << endl; cout << "Please enter employee's salary: "; cin >> salary; cout << endl; cout << "Please enter employee's department: "; cin >> dept; cout << endl; count = count + 1; cout << "Would you like to add another record? (YES or NO)\n"; cin >> addRecord; addRecord = toupper(addRecord); }//end if else cout <<"Max number of records is 25!"<<'\n'; }//end while
case 2: //modify current employee data //get user to search for cout << "Enter user you wish to modify: "; cin >> searchFor;
//search for user in array for (i=0; i<25; i+=1) if (empNames[i] = searchFor){; cout << "Enter the updated name: "; cin >> empNames[i]; cout << "Enter the updated salary: "; cin >> salaries[i]; cout << "Enter the updated department: "; cin >> depts[i]; }//end if //end for
case 9: //return to previous menu break;
default: cout << "Your input was invalid & could not be processed.\n"; }
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; int count = 0;//employee counter
//display menu 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"; cout << empList(empNames[], salaries[], depts[]); case 2: //list employees for specific dept. cout << "Please type the name of the department you wish to query, from the following list:\n"; for (i=0; i<25; i+=1) { cout << depts[i]; } cout << endl; cin >> deptChoice; //displays all employees from user selected dept. cout << "List of all employees from " << deptChoice << ":\n"; cout << empByDept(deptChoice, empNames[]); case 3: //calculate the avg. salary cout << "Average employee salary: $" << calcAvgSal(salaries[]) << endl; case 4: //calculate the lowest salary cout << "Lowest paid employee's salary: $" << calcLowSal(salaries[]) << endl; case 9: //administrative options system("cls"); //clears screen for administrative options cout << admin_Options(&empNames[], &salaries[], &depts[], &count); case 0: //exit cout << "Goodbye!\n"; exit (3); }
system("pause"); }//end main function
void empList(char empNames[], double salaries[], char depts[], int count)//lists all employees {for (int i=0; i<25; i+=1)
if (count < 1) { cout << "No Records to display. Try entering employee data first!"; } else { cout << "Employee: " << empNames[i] << "Salary: " << salaries[i] << "Department: " << depts[i] << endl; } }//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<25; i+=1)
if (count < 1) { cout << "No Records to display. Try entering employee data first!"; } else { if (depts[i] = deptChoice) cout << "Name: " << empNames[i] << endl; } }//end of EmpByDept function
double calcAvgSal(double salaries[], int count)//calculates the average salary of all employees {for (int i=0; i<25; i+=1)
if (count < 1) { cout << "No Records to display. Try entering employee data first!"; } else { double avg_Sal = 0.00; avg_Sal = avg_Sal + salaries[i]; return avg_Sal; } }//end calcAvgSal function
double calcLowSal(double salaries[], int count)//calculates the lowest salary of all paid employees {for (int i=0; i<25; i+=1)
if (count < 1) { cout << "No Records to display. Try entering employee data first!"; } else { double lowest = 0.00; if (salaries[i] > lowest) lowest = salaries[i]; return lowest; } }//end calcLowSal function
Can someone help me figure out why I can't enter in Emp details. Do I need to cin.ignore ('\n') somewhere? Is the previous return still in memory?
Thanks to all for your help.
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 quere by dept, salary status, or list all employees.
int admin_Options(char empNames[], double salaries[], char depts[], int count)//options for adding or modifying current employees { int admin_Choice = 0; char addRecord; char empName; double salary; char dept; char searchFor = ' '; int i;
//set num to maximum number of elements in array int num = 25;
switch (admin_Choice){ case 1: //enter in new emp. data cout << endl; cout << "Please enter employee's name: "; cin >> empName; cout << endl; cout << "Please enter employee's salary: "; cin >> salary; cout << endl; cout << "Please enter employee's department: "; cin >> dept; cout << endl; count = count + 1; cout << "Would you like to add another record? (YES or NO)\n"; cin >> addRecord; addRecord = toupper(addRecord);
while (addRecord = "YES" && i <num) { empNames[i] = empName; salaries[i] = salary; depts[i] = dept; i+=1; if (i<num) { cout << "Please enter employee's name: "; cin >> empName; cout << endl; cout << "Please enter employee's salary: "; cin >> salary; cout << endl; cout << "Please enter employee's department: "; cin >> dept; cout << endl; count = count + 1; cout << "Would you like to add another record? (YES or NO)\n"; cin >> addRecord; addRecord = toupper(addRecord); }//end if else cout <<"Max number of records is 25!"<<'\n'; }//end while
case 2: //modify current employee data //get user to search for cout << endl; cout << "Enter user you wish to modify: "; cin >> searchFor;
//search for user in array for (i=0; i<25; i+=1) if (empNames[i] = searchFor){; cout << "Enter the updated name: "; cin >> empNames[i]; cout << "Enter the updated salary: "; cin >> salaries[i]; cout << "Enter the updated department: "; cin >> depts[i]; }//end if //end for
case 0: //return to previous menu break;
default: cout << endl; cout << "Your input was invalid & could not be processed.\n"; }
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; 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); 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<25; i+=1) { cout << depts[i]; } cout << endl; cin >> deptChoice; //displays all employees from user selected dept. cout << "List of all employees from " << deptChoice << ":\n"; empByDept(deptChoice, empNames, depts, count); break; }//end else case 3: //calculate the avg. salary cout << "Average employee salary: $" << calcAvgSal(salaries, count) << endl; break; case 4: //calculate the lowest salary cout << "Lowest paid employee's salary: $" << calcLowSal(salaries, count) << endl; 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<25; 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<25; 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<25; 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<25; 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
Can someone help me figure out why I can't enter in Emp details. Do I need to cin.ignore ('\n') somewhere? Is the previous return still in memory?
Yes, that's always a possibility. every time you retrieve input from with the >> operator, it stops reading at the next whitespace character (including newlines).
You can either use cin.ignore(numeric_limits<streamsize>::max(),'\n'); after each call to cin >>
or change all your inputs so that you're retrieving them as strings with getline, and create a simple converter function when you need to turn them into an int.
CODE
string str; getline( cin, str );
Here's an example "convert to int" function.
CODE
bool to_int( const string& str, const int& i ) { stringstream ss( str ); return ( ss >> i ); }
With the added bonus that it can help you determine whether the user entered a valid number, without worrying about error flags on cin
CODE
int i; if( ! to_int( str, i ) ) { cout << "invalid input\n"; }
This post has been edited by Bench: 3 Dec, 2007 - 09:03 AM
Ok I hate to keep bugging the community, you all have been VERY helpful.
2 last things I need help with. I'm getting these errors:
CODE
Compiler: Default compiler Executing g++.exe... g++.exe "G:\Documents\Student Class Work\Class Project Diaz.cpp" -o "G:\Documents\Student Class Work\Class Project Diaz.exe" -g3 -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3 G:\Documents\Student Class Work\Class Project Diaz.cpp:72:22: warning: multi-character character constant G:\Documents\Student Class Work\Class Project Diaz.cpp:96:27: warning: multi-character character constant
Execution terminated Compilation successful
I'm trying to get it to exit the loop when user enters "no" in the admin option panel. but the program gets stuck in an endless loop. The input loop should only repeat if the user enters "yes" or the value of count is <25 (25 being the max users allowed).
Also I'm not sure as to how to update an entry in an array. I might have missed that day of class. I know how to globally update all elements in the array (ex. corporate wide raises).
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 quere by dept, salary status, or list all employees.
int admin_Options(char empNames[], double salaries[], char depts[], int count)//options for adding or modifying current employees { int admin_Choice = 0; char addRecord = ' '; char searchFor = ' '; int i;
do { system ("cls"); //set num to maximum number of elements in array int num = 25;
switch (admin_Choice){ case 1: //enter in new emp. data 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? (YES or NO)\n"; cin >> addRecord; cin.ignore (3,'\n'); addRecord = toupper(addRecord);
if (addRecord = 'YES'){ do { i+=1; 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); }while (count < 25);//end while cout << "Max number of records is 25!"<<'\n'; break; }//end if else if (addRecord = 'NO') { break; }//end if
case 2: //modify current employee data //get user to search for cout << endl; cout << "Enter user you wish to modify: "; cin >> searchFor; cin.ignore(100,'\n');
//search for user in array for (i=0; i<25; i+=1) if (empNames[i] = searchFor){; cout << "Enter the updated name: "; cin >> empNames[i]; cin.ignore(100,'\n'); cout << endl; cout << "Enter the updated salary: "; cin >> salaries[i]; cout << endl; cout << "Enter the updated department: "; cin >> depts[i]; cin.ignore(100,'\n'); cout << endl; }//end if //end for break;
case 0: //return to previous menu break;
default: cout << endl; cout << "Your input was invalid & could not be processed.\n"; } }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; 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); 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<25; 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); break; }//end else case 3: //calculate the avg. salary cout << "Average employee salary: $" << calcAvgSal(salaries, count) << endl; break; case 4: //calculate the lowest salary cout << "Lowest paid employee's salary: $" << calcLowSal(salaries, count) << endl; 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<25; 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<25; 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<25; 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<25; 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
This post has been edited by djkaotix: 3 Dec, 2007 - 07:59 PM
Ok, this line has two big problems: if (addRecord = 'YES'). One is that 'YES' shouldn't compile at all, single quote is a character, double is a string, so single around more than one char is not real good.
Your infinite loop comes from the other big problem. The single "=" means you're trying to assign value rather than test value. Try if (addRecord == 'Y') instead and see how far you get.
Although, asking "Would you like to add another record..." is probably bad form. They're preforming a single action and you're slipping another in at the end. It makes more sense just to let them fall back to the menu.
Also, maintaining three separate lists doesn't make as much sense as using a single list of type struct, if you've gotten that far.