Goto statements, any alternatives

Hello, is there any possibilities not to use the Goto statements in this program?

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
#include <cstdlib>
#include <iostream>

/* Program to accept the salaries of 10 employees from the user and display them
in descending order for all employees. If the user enters zero, the program 
should display the message "The amount should be greater than zero" and accept
the value again */

using namespace std;

class empsal
{
      float salary[10];
      int count;
      
      public:
      void input()                                       //method for getting values
      {
           for (count = 0; count < 10; count++)
           {
               szeros:                                           
                      cout << "Salary: ";
                      cin >> salary[count];
                      
                      if (salary[count] == 0)
                      {
                         cout << endl << "The amount should be greater than zero" << endl;
                         goto szeros;
                      }
           }
      }
      void sorting()                          //method for sorting values
      {      
           
           cout << endl;
           cout << "Salaries displayed in a descending order:: \a\a\a";
           cout << endl;
           
           int c, t; 
           int swapper;             
           for(c = 0; c < 10; c++)                                    //Bubble array sorting
           {
                 for (t=0; t < 9; t++)
                 {
                     if (salary[t+1] > salary[t])      
                     { 
                       swapper = salary[t];            
                       salary[t] = salary[t+1];
                       salary[t+1] = swapper;
                                     
                     }
                 }
           }
      }
      
      void display()                           //method for displaying values
      {
           for (count = 0; count < 10; count++)
           {
                 cout << endl << "           " << count + 1 << ". " << salary[count] << endl;
           }
           cout << endl;
      }

};

int main(int argc, char *argv[])
{
    cout << "******************************SALARIES***********************************" << "\n\n\a";
    
    empsal es;
    es.input();
    es.sorting();
    es.display();
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


Feel free to give your opinion... I am open to anything that works!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void input()      //method for getting values
{
  for (count = 0; count < 10; count++)
  {
    salary[count] = 0;
    while (salary[count] == 0)
    {
       cout << "Salary: ";
       cin >> salary[count];
                      
       if (salary[count] == 0)
       {
            cout << endl << "The amount should be greater than zero" << endl;
       }
    }
}


I see you're comparing a floating point number. You should read this:
http://floating-point-gui.de/
Last edited on
Thanks, I got it!
Topic archived. No new replies allowed.