C++ program

Fandango21

Junior Member
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;

}
 

YoungGun21

Platinum Member
Aug 17, 2006
2,546
1
81
I'm not 100&#37; sure, but I don't think you want to use a while loop.

Maybe you could do like if(number < 1){blah} else {......}
 

Fandango21

Junior Member
Feb 8, 2011
23
0
0
Lol. Wow. Thanks for noticing Crusty. To YoungGun21, I tried the if statement instead of while loop to see it in action and like I thought it does run a second time when the number < 1, however I need to to repeat over and over until the number is anything > 1 so I'm using the while loop. The If statement will only execute once basically.
 

Fallen Kell

Diamond Member
Oct 9, 1999
6,088
450
126
Also for sanity, you might want to change your "int number" to a "string myinput", and then actually do a test that it is indeed a number, since "cin" doesn't care at all if you typed a number or not and your setting of an asci character into an int can and will cause some havoc. Doing that, you can then use "sstream" to validate if your string is a number safely. Personally, I would use "getline(cin, myinput)" instead of just cin.
 
Last edited:

Fandango21

Junior Member
Feb 8, 2011
23
0
0
Yea, Kell I ran into that very problem when I accidentally hit a letter. It made the program go ape sh*t lol. I get what you're saying by using a string and the getline approach kind of. We've only really barely talked about it to this point and I've never used sstream before. Also, the function has to return an int so that was my logic there (obviously flawed somewhat) Guess I could just static_cast the return? I'd like to use this and will probably look it up myself and mess around with it later. Thanks for the info
 

Fallen Kell

Diamond Member
Oct 9, 1999
6,088
450
126
Well, reading in and returning are really two different things. It is perfectly fine to read in as a string, and after a verification that what you read in was indeed an integer, convert the string to an integer and then return the integer.

That said, I will send you to a little tutorial on sstream (or in long form StringStream):

http://www.cplusplus.com/doc/tutorial/basic_io/
 
Last edited:
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |