write a program to accept the salaries of 10 employees from the user and display them in descending order for all the employees,if the user enters zero, the program should display the message"the amount should be greater than zero" and accept the value again.

#include <iostream>

using namespace std;

class Employee
{
    float salary[10];
    public:
  void sortdata()
  {
      int ctr;
      for(ctr=0;ctr<10;ctr++)
      {
          cout<<"enter the salary:";
          cin>>salary[ctr];
          if(salary[ctr] == 0)
          {
          cout<<"salary should be greater than zero"<<endl;
          continue;
          }
      }
  int sal=0;
  while (sal<9)
  {
      float temp;
      if(salary[sal]<salary[sal+1])
      {
          temp=salary[sal];
          salary[sal]=salary[sal+1];
          salary[sal+1]=temp;
          sal=0;
          continue;
      }
   sal++;
  }
  }
  void display()
  {
      for(int sal=0;sal<10;++sal)
      {
      cout<<"element"<<sal<<":"<<salary[sal]<<endl;
      }
  }
};
int main()
{
    Employee E;
    E.sortdata();
    E.display();
    return 0;
}

so i wrote the program but the last part of the question isn't working, if the user enters zero,the program should display the message"the amount should be greater than zero" and accept the values again. whats wrong with the code.:(

Recommended Answers

All 13 Replies

continue; causes a while or for loop to begin again at the top of the loop.

Do you need to decrement the value of ctr to have another value?

does it display "the amount should be greater than zero"?

Do you need to decrement the value of ctr to have another value?

yes it displays "the amount should be greater than zero" if i enter 0 and i dont think i need to decrement ctr value cause when 0 is entered the continue should work and loop should be returning to for

for(ctr=0;ctr<10;ctr++)
{
cout<<"enter the salary:";
cin>>salary[ctr];
if(salary[ctr] == 0)
{
cout<<"salary should be greater than zero"<<endl;
continue;
}

So whats the problem?... If I understand what you just said that part of the code is doing what its suppose to do :?:

Maybe instead of using continue why not change the value of ctr back to 0 whenever the user inputs 0?

Maybe instead of using continue why not change the value of ctr back to 0 whenever the user inputs 0?

but if i do that wont the user have to input the whole values again ?

but if i do that wont the user have to input the whole values again ?

using continue does exactly the same thing

do you just want the salary to have a value not equal to 0?
then why not change the current value(0) to give way to a new value

for(ctr=0;ctr<10;ctr++)
{
cout<<"enter the salary:";
cin>>salary[ctr];
if(salary[ctr] == 0)
{
cout<<"salary should be greater than zero"<<endl;
cin>>salary[ctr];
}

using continue does exactly the same thing

do you just want the salary to have a value not equal to 0?
then why not change the current value(0) to give way to a new value

for(ctr=0;ctr<10;ctr++)
{
cout<<"enter the salary:";
cin>>salary[ctr];
if(salary[ctr] == 0)
{
cout<<"salary should be greater than zero"<<endl;
cin>>salary[ctr];
}

thx but it is still sorting the value 0 and it is not even showing "salary should be greater than zero" when 0 is entered . :confused:

try to add continue and see if it works you... though I think its suppose function normally without it

for(ctr=0;ctr<10;ctr++)
{
cout<<"enter the salary:";
cin>>salary[ctr];
if(salary[ctr] == 0)
{
cout<<"salary should be greater than zero"<<endl;
cin>>salary[ctr];
continue;
}

try to add continue and see if it works you... though I think its suppose function normally without it

for(ctr=0;ctr<10;ctr++)
{
cout<<"enter the salary:";
cin>>salary[ctr];
if(salary[ctr] == 0)
{
cout<<"salary should be greater than zero"<<endl;
cin>>salary[ctr];
continue;
}

not working can anyone else help me with this please.:sad:

Have you considered using a loop to request input into a given salary[ctr] as long as salary[ctr] equals zero?

Have you considered using a loop to request input into a given salary[ctr] as long as salary[ctr] equals zero?

hmm like this ? this is working when 0 is entered first time but if i enter zero again it is getting sorted.:-/

#include <iostream>

    using namespace std;

    class Employee
    {
    float salary[10];
    public:
    void sortdata()
    {
    int ctr;
    for(ctr=0;ctr<10;ctr++)
    {
    cout<<"enter the salary:";
    cin>>salary[ctr];
    if(salary[ctr] == 0)
    {
    cout<<"salary should be greater than zero"<<endl;
    for(ctr=0;ctr<10;ctr++)
    {
    cout<<"enter the salary:";
    cin>>salary[ctr];
    }
    }
    }
    int sal=0;
    while (sal<9)
    {
    float temp;
    if(salary[sal]<salary[sal+1])
    {
    temp=salary[sal];
    salary[sal]=salary[sal+1];
    salary[sal+1]=temp;
    sal=0;
    continue;
    }
    sal++;
    }
    }
    void display()
    {
    for(int sal=0;sal<10;++sal)
    {
    cout<<"element"<<sal<<":"<<salary[sal]<<endl;
    }
    }
    };
    int main()
    {
    Employee E;
    E.sortdata();
    E.display();
    return 0;
    }

finally i was able to figure out how to do it.

#include <iostream>

using namespace std;

class Employee
{
float salary[10];
public:
void sortdata()
{
int ctr;
for(ctr=0;ctr<10;ctr++)
{
cout<<"enter the salary:";
cin>>salary[ctr];
if(salary[ctr] == 0)
{
cout<<"salary should be greater than zero"<<endl;
cout<<"enter the salary:";
cin>>salary[ctr];
continue;
}
}
int sal=0;
while (sal<9)
{
float temp;
if(salary[sal]<salary[sal+1])
{
temp=salary[sal];
salary[sal]=salary[sal+1];
salary[sal+1]=temp;
sal=0;
continue;
}
sal++;
}
}
void display()
{
for(int sal=0;sal<10;++sal)
{
cout<<"element"<<sal<<":"<<salary[sal]<<endl;
}
}
};
int main()
{
Employee E;
E.sortdata();
E.display();
return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.