Testing phase. Need help please.

My program accepts the second input, not the first.

Page 1 of 1

2 Replies - 357 Views - Last Post: 07 February 2010 - 08:14 PM Rate Topic: -----

#1 Riskinit  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 157
  • Joined: 14-January 10

Testing phase. Need help please.

Posted 07 February 2010 - 04:43 PM

I have completed a simple program for my software methods class. Basically it asks for how many employees the company has. It then asks how many days each employee missed. It then finds the average days missed. I just started using functions in this weeks lesson but I dont believe my problem is because of the functions. Basically when I run the program I am forced to enter an input 2 times. How do I get rid of this? (so I only have to enter the data one time.)

Thank you in advance; also, any pointers on making the program better will be well appreciated. All that is required though, is that the numbers entered are greater than zero and actually numbers.

#include <iostream>
#include <string>
using namespace std;

int numEmployees();
int daysMissed(int);
double averageMiss(int, int);

int main()
{
		int employees;
		int sum;
		double average;
		cout << "This program calculates the average number of days missed by employees.\n";
		employees = numEmployees();
		sum = daysMissed(employees);
		average = averageMiss(sum, employees);
		cout << "Average number of days missed equals " << average << "\n";
		return 0;
}

int numEmployees()
{
	string invalid;
	int num;
	cout << "How many employees do you have?\n";
	cin >> num;
	while(!(cin >> num) || num < 1)
	{
		cin >> invalid;
		cout << "You typed \"" << invalid << "\" instead of a positive integer." << "\n";
		cout << "Please try again." << "\n";
	}
	return num;
}

int daysMissed(int employees)
{
	int count = 0;
	int sum = 0;
	int num;
	while(count != employees)
	{
		cout << "How many days did employee " << count+1 << " miss?";
		cin >> num;
		if((cin >> num) && num > 0)
		{
			count++;
			sum = sum + num;
		}
		else
		{
			cout << "Invalid input!\n";
		}
	}
	return sum;
}

double averageMiss(int sum, int employees)
{
	double average = static_cast<double>(sum)/static_cast<double>(employees);
	return average;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Testing phase. Need help please.

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1369
  • View blog
  • Posts: 4,737
  • Joined: 19-February 09

Re: Testing phase. Need help please.

Posted 07 February 2010 - 06:49 PM

View PostRiskinit, on 08 February 2010 - 12:43 AM, said:

... Basically when I run the program I am forced to enter an input 2 times. How do I get rid of this? (so I only have to enter the data one time.) ...


Here you use cin >> num twice.
Which causes the program to try and get input for num twice.
You also use cin >> invalid which will wait for input.
	cout << "How many employees do you have?\n";
	cin >> num;
	while(!(cin >> num) || num < 1)
	{
		cin >> invalid;
		cout << "You typed \"" << invalid << "\" instead of a positive integer." << "\n";
		cout << "Please try again." << "\n";
	}
	return num;



You want to read a number from the console so use cin >> num - which is ok.
You also want to check if the input was valid, there are a few of ways of doing that:-
	cout << "How many employees do you have?\n";
	// remove first cin >> num
	while(!(cin >> num) || num < 1)
	{



OR

	cout << "How many employees do you have?\n";
	cin >> num;
	while(!cin.good() || num < 1)
	{



If you are checking the stream state you may need to reset it,
with cin.clear() and cin.sync() or cin.ignore(), in the while loop.

cin >> invalid, will also pause for input and is not required.
	cout << "How many employees do you have?\n";
	cin >> num;
	while( cin.bad() || num < 1)
	 {
		cin.clear();
		cin.sync();
		cout << "Invalid input. " << endl;
		cout << "Please enter a positive integer. : ";
		cin >> num;
	 }
	return num;



Something like that should work, I haven't tested this, so maybe errors.
Was This Post Helpful? 1
  • +
  • -

#3 Riskinit  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 157
  • Joined: 14-January 10

Re: Testing phase. Need help please.

Posted 07 February 2010 - 08:14 PM

Ok, I figured out the kinks thank to you. I can't believe it was so obvious. I learned that code from some other guy and I guess the program I used it in didn't have a problem. But thanks a lot. I'll have to be more careful in the future. The bigger these programs get the more complex they get to think about. I can't even imagine a code with 400-500 lines much less code with thousands and thousands of lines yet.

This post has been edited by Riskinit: 07 February 2010 - 08:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1