In need of help with my code

For some reason my while loop is not working I'd like some help
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct empDetail{
string EmpFName;
string EmpLName;
string occupation;
int EmpID;
float bonus;
float salary;
} employees[3];

string EmpSalary;
int n,j,i,key;
char options;
string EmpID;

void enterdisplay()
{
for (int n=0; n<3; n++)

{
cout<<"Please enter ID:";
cin>>employees[n].EmpID;

cout<<"Please enter first name:";
cin>>employees[n].EmpFName;

cout<<"Please enter last name:";
cin>>employees[n].EmpLName;

cout<<"Please enter occupation:";
cin>>employees[n].occupation;

cout<<"Please enter salary:";
cin>>employees[n].salary;

}
cout << "\nYou entered employee records, the bonus' are:\n";

//cout<<"organized employee IDS are:\n";
// for(j=0;j<3;j++)
//{
// cout<<employees[3].EmpID;
//}
}
void organize() //Function for sorting the data by employee name
{
empDetail temp;
for(int i=0; i<3; i++)
{
for(int j=i+1; j<3; j++)
{
if(employees[j].EmpID >employees[i].EmpID)
{
temp=employees[i];
employees[j]=employees[j+1];
employees[j+1] = temp;

}
}
}
cout<<"organized employee IDS are:\n";
for(int j=0; j< 3; j++)
{
cout<<employees[j].EmpID<<"\n";
}

}
void bonus()
{
for (int n = 0; n < 3; n++)
{
if (employees[n].occupation.compare("Programmer") == 0)
{
employees[n].bonus = employees[n].salary * (15.0 / 100.0);
}
if (employees[n].occupation.compare("Clerk") == 0)
{
employees[n].bonus = employees[n].salary * (20.0 / 100.0);
}
if (employees[n].occupation.compare("Analyst") == 0)
{
employees[n].bonus = employees[n].salary * (10.0 / 100.0);
}
if (employees[n].occupation.compare("Accountant") == 0)
{
employees[n].bonus = employees[n].salary * (10.0 / 100.0);
}
// cout << "Bonus \n\n";
//for (int j = 0; j < 3; j++)
//{

cout << employees[n].occupation<<"\n";

cout << employees[n].bonus<<"\n";
//}
}
}


void seekout()
{
cout<< "Enter the employee ID you want…";
cin>> key;
int flag = 0; // set flag to off
for(int i=0; i<3; i++) // start to loop through the array
{
if (employees[i].EmpID == key) // if match is found
{
flag = 1; // turn flag on
break ; // break out of for loop
}
}
if (flag) // if flag is TRUE (1)
{
cout<< "ID has been found" << i <<".\n";
cout<<employees[i].EmpFName;
cout<<employees[i].EmpLName;
cout<<employees[i].occupation;
cout<<employees[i].salary;

}
else

{
cout<< "Sorry,ID not found."<<endl<<endl;
}
}

int main()
{
do{

cout<<"**************************************************\n";
cout<<" MENU \n";
cout<<"**************************************************\n";


cout<<"OPTIONS\n";
cout<<"1. Enter Employee Details\n";
cout<<"2. Display Database Info\n";
cout<<"3. Search for Employee\n";
cout<<"4. Sort and Display Details\n";
cout<<"5. Calculate Job Bonus\n";
cout<<"6. Exit\n";
cout<<"Choose a number";
cin>>options;
if (options == 1)
{
enterdisplay();
}
if (options == 2)
{

}
if (options == 3)
{
seekout();
}
if (options== 4)
{
organize();
}
if (options == 5)
{
bonus();
}
} while(options!=6);
return 0;
}

Last edited on
Please edit your post to include code tags.
https://www.cplusplus.com/articles/jEywvCM9/
options is a char. You are comparing it to numbers (1,2,3. etc). You need to compare it to the ASCII characters '1', '2', '3' etc. instead. Here is a working version of main() with the calls to the rest of the program commented out:
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
#include <iostream>
using namespace std;

int
main()
{
    char options;
    do {

	cout << "**************************************************\n";
	cout << " MENU \n";
	cout << "**************************************************\n";

	cout << "OPTIONS\n";
	cout << "1. Enter Employee Details\n";
	cout << "2. Display Database Info\n";
	cout << "3. Search for Employee\n";
	cout << "4. Sort and Display Details\n";
	cout << "5. Calculate Job Bonus\n";
	cout << "6. Exit\n";
	cout << "Choose a number";
	cin >> options;
	if (options == '1') {
	    // enterdisplay();
	}
	if (options == '2') {

	}
	if (options == '3') {
	    // seekout();
	}
	if (options == '4') {
	    // organize();
	}
	if (options == '5') {
	    // bonus();
	}
    } while (options != '6');
    return 0;
}


Have you learned about the switch statement? It would make your intention clearer:
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
#include <iostream>
using namespace std;

int
main()
{
    char options;
    do {

	cout << "**************************************************\n";
	cout << " MENU \n";
	cout << "**************************************************\n";

	cout << "OPTIONS\n";
	cout << "1. Enter Employee Details\n";
	cout << "2. Display Database Info\n";
	cout << "3. Search for Employee\n";
	cout << "4. Sort and Display Details\n";
	cout << "5. Calculate Job Bonus\n";
	cout << "6. Exit\n";
	cout << "Choose a number";
	cin >> options;
	switch(options) {
	case '1':
	    // enterdisplay();
	    break;
	case '2':
	    break;
	case '3':
	    // seekout();
	    break;
	case '4':
	    // organize();
	    break;
	case '5':
	    // bonus();
	    break;
	case '6':
	    // This is handled in the while() test below.
	    break;
	default:
	    cout << "Illegal option. Please try again.\n";
	    break;
	}
    } while (options != '6');
    return 0;
}
Yeah I did what u said and it worked im grateful man
Topic archived. No new replies allowed.