Making Functions

if(choice=='E'){
cout<< "Enter new employee ID: ";
cin >> pos;
if(pos < lower || pos > upper){
cout<< "ID out of range." << endl<<endl;
}
else if(pos >=lower && pos <=upper){
if(temp.getValue(pos)=="") { //If temp is empty at pos
cout << "No Employee with this ID" << endl<<endl;
}
else{
cout<< "Employee name is " <<temp.getValue(pos)<< endl;
cout<< "Enter new name: ";
cin.get(A);
getline(cin, name);
temp.setValue(pos, name);
cout << "Employee name is set."<<endl<<endl;
}
}
}
else if(choice=='A'){
cout<< "Enter new employee ID: ";
cin >> pos;
if(pos < lower || pos > upper){
cout<<"ID out of range." << endl<<endl;
}
else if(pos>=lower && pos <=upper){
if(temp.getValue(pos).length()>0){
cout<< "ID is already in use." << endl<<endl;
}
else if(temp.getValue(pos).length()==0){
cout<< "Enter new name: ";
cin.get(A);
getline(cin, name);
temp.setValue(pos, name);
emps++;
cout<< "New employee is added." << endl << endl;

}
}
}
else if(choice == 'R'){
cout<< "Enter employee ID to remove: ";
cin >> pos;
if(pos < lower || pos > upper){
cout<<"ID out of range." << endl << endl;
}
else if(pos>=lower && pos <=upper){
if(temp.getValue(pos)==""){
cout<<"ID is not in use." << endl << endl;
}
else if(temp.getValue(pos).length()>0){ //If temp isn't empty
temp.setValue(pos, "");
emps--;
cout<< "Employee is removed." << endl << endl;
}
}
}

else if(choice == 'D'){
cout << "Employee List" << endl << "-------------------------" << endl;

for(int i=lower; i<=upper; i++){
if(temp.getValue(i).length()>0){ //If temp isn't empty
cout<< i ;
cout<< " *"<<temp.getValue(i)<<'*' << endl;
}
}
I need to make these add/ edit/ remove/ and display functions instead of if statements.
closed account (N36fSL3A)
You should use code tags so people can actually read your code.
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
if(choice=='E'){
cout<< "Enter new employee ID: ";
cin >> pos;
if(pos < lower || pos > upper){
cout<< "ID out of range." << endl<<endl;
}
else if(pos >=lower && pos <=upper){
if(temp.getValue(pos)=="") { //If temp is empty at pos
cout << "No Employee with this ID" << endl<<endl;
}
else{
cout<< "Employee name is " <<temp.getValue(pos)<< endl;
cout<< "Enter new name: ";
cin.get(A);
getline(cin, name);
temp.setValue(pos, name);
cout << "Employee name is set."<<endl<<endl;
}
}
}
else if(choice=='A'){
cout<< "Enter new employee ID: ";
cin >> pos;
if(pos < lower || pos > upper){
cout<<"ID out of range." << endl<<endl;
}
else if(pos>=lower && pos <=upper){
if(temp.getValue(pos).length()>0){
cout<< "ID is already in use." << endl<<endl;
}
else if(temp.getValue(pos).length()==0){
cout<< "Enter new name: ";
cin.get(A);
getline(cin, name);
temp.setValue(pos, name);
emps++;
cout<< "New employee is added." << endl << endl;

}
}
}
else if(choice == 'R'){
cout<< "Enter employee ID to remove: ";
cin >> pos;
if(pos < lower || pos > upper){
cout<<"ID out of range." << endl << endl;
}
else if(pos>=lower && pos <=upper){
if(temp.getValue(pos)==""){
cout<<"ID is not in use." << endl << endl;
}
else if(temp.getValue(pos).length()>0){ //If temp isn't empty
temp.setValue(pos, "");
emps--;
cout<< "Employee is removed." << endl << endl;
}
}
}

else if(choice == 'D'){
cout << "Employee List" << endl << "-------------------------" << endl;

for(int i=lower; i<=upper; i++){
if(temp.getValue(i).length()>0){ //If temp isn't empty
cout<< i ;
cout<< " *"<<temp.getValue(i)<<'*' << endl;
}
}


does this fix that issue?
does this fix that issue?

No.

You need to indent your code. Now it is readable and one can - for example - see at one glance that it is incomplete.

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
if(choice=='E'){
    cout<< "Enter new employee ID: ";
    cin >> pos;
    if(pos < lower || pos > upper){
        cout<< "ID out of range." << endl<<endl;
    }
    else if(pos >=lower && pos <=upper){
        if(temp.getValue(pos)=="") { //If temp is empty at pos
            cout << "No Employee with this ID" << endl<<endl;
        }
        else{
            cout<< "Employee name is " <<temp.getValue(pos)<< endl;
            cout<< "Enter new name: ";
            cin.get(A);
            getline(cin, name);
            temp.setValue(pos, name);
            cout << "Employee name is set."<<endl<<endl;
        }
    }
}
else if(choice=='A'){
    cout<< "Enter new employee ID: ";
    cin >> pos;
    if(pos < lower || pos > upper){
        cout<<"ID out of range." << endl<<endl;
    }
    else if(pos>=lower && pos <=upper){
        if(temp.getValue(pos).length()>0){
            cout<< "ID is already in use." << endl<<endl;
        }
        else if(temp.getValue(pos).length()==0){
            cout<< "Enter new name: ";
            cin.get(A);
            getline(cin, name);
            temp.setValue(pos, name);
            emps++;
            cout<< "New employee is added." << endl << endl;

        }
    }
}
else if(choice == 'R'){
    cout<< "Enter employee ID to remove: ";
    cin >> pos;
    if(pos < lower || pos > upper){
        cout<<"ID out of range." << endl << endl;
    }
    else if(pos>=lower && pos <=upper){
        if(temp.getValue(pos)==""){
            cout<<"ID is not in use." << endl << endl;
        }
        else if(temp.getValue(pos).length()>0){ //If temp isn't empty
            temp.setValue(pos, "");
            emps--;
            cout<< "Employee is removed." << endl << endl;
        }
    }
}

else if(choice == 'D'){
    cout << "Employee List" << endl << "-------------------------" << endl;

    for(int i=lower; i<=upper; i++){
        if(temp.getValue(i).length()>0){ //If temp isn't empty
            cout<< i ;
            cout<< " *"<<temp.getValue(i)<<'*' << endl;
        }
    }
closed account (N36fSL3A)
Still hurts my eyes. Add curly brackets after the line with if statements/functions/loops etc.

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
if(choice=='E')
   {
    cout<< "Enter new employee ID: ";
    cin >> pos;
    if(pos < lower || pos > upper){
        cout<< "ID out of range." << endl<<endl;
    }
    else if(pos >=lower && pos <=upper)
  {
        if(temp.getValue(pos)=="") 
        { //If temp is empty at pos
            cout << "No Employee with this ID" << endl<<endl;
        }
        else
        {
            cout<< "Employee name is " <<temp.getValue(pos)<< endl;
            cout<< "Enter new name: ";
            cin.get(A);
            getline(cin, name);
            temp.setValue(pos, name);
            cout << "Employee name is set."<<endl<<endl;
        }
    }
}
else if(choice=='A')
   {
    cout<< "Enter new employee ID: ";
    cin >> pos;
    if(pos < lower || pos > upper){
        cout<<"ID out of range." << endl<<endl;
    }
    else if(pos>=lower && pos <=upper)
    {
        if(temp.getValue(pos).length()>0)
        {
            cout<< "ID is already in use." << endl<<endl;
        }
        else if(temp.getValue(pos).length()==0)
       {
            cout<< "Enter new name: ";
            cin.get(A);
            getline(cin, name);
            temp.setValue(pos, name);
            emps++;
            cout<< "New employee is added." << endl << endl;

        }
    }
}
else if(choice == 'R')
    {
    cout<< "Enter employee ID to remove: ";
    cin >> pos;
    if(pos < lower || pos > upper){
        cout<<"ID out of range." << endl << endl;
    }
    else if(pos>=lower && pos <=upper)
        {
        if(temp.getValue(pos)=="")
        {
            cout<<"ID is not in use." << endl << endl;
        }
        else if(temp.getValue(pos).length()>0){ //If temp isn't empty
            temp.setValue(pos, "");
            emps--;
            cout<< "Employee is removed." << endl << endl;
        }
    }
}

else if(choice == 'D')
       {
    cout << "Employee List" << endl << "-------------------------" << endl;

    for(int i=lower; i<=upper; i++){
        if(temp.getValue(i).length()>0){ //If temp isn't empty
            cout<< i ;
            cout<< " *"<<temp.getValue(i)<<'*' << endl;
        }
    }


Not perfect, but better.
Last edited on by Fredbill30
Topic archived. No new replies allowed.