- Feb 8, 2011
- 23
- 0
- 0
Hey everyone. Its that time of year again and I'm back in school. Been a few months since I last wrote any code and I'm trying to refresh a bit. Currently this is what I have but for some reason the program wants to ask the question twice before just displaying the result. I would like to have it to continue a loop if the user puts a number in less than one but as soon as they put in 1 or higher I want it to just return the value, not ask again for the number. This was written using Visual Studio C++.
Code:
#include <iostream>
using namespace std;
int numEmployees() {
int number;
cout << "Please enter the number of employees in the company. " << endl;
cin >> number;
while (number < 1) {
cout << "The number of employees cannot be less than 1." << endl;
cout << endl;
cout << "Please enter the number of employees in the company. " << endl;
cin >> number;
}
return number;
}
int main () {
numEmployees();
cout << numEmployees() << endl;
system("pause");
return 0;
}
