4 Replies - 4907 Views - Last Post: 02 April 2007 - 04:36 PM Rate Topic: -----

#1 Lydia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 03-May 06

Logical Error

Posted 02 April 2007 - 02:01 PM

The shoe sizes of an unknown number of poeple have to be input and added together. Then the number of people have to be displayed and their average shoe size has to be calculated and displayed. The av shoe size is the total of all the sizes divided by the number of people and then rounded off to the nearest integer.

This is what I have done so far:

#include <iostream>
using namespace std;

int main( )
{
int numberPeople;
float size, totalSize;
float averageSize;

//initialize
totalSize = 0;
numberPeople = 0;

//loop
while (size > 0)
{
cout << "Please enter next persons shoe size: ";
cin >> size;

//Update totals
totalSize += size;
numberPeople++;
}//End of loop

//Display the number of people
cout << "The number of people is " << numberPeople << endl;
{

//Average 0.5 is added in order to round off
if (numberPeople > 0)
	averageSize = int (totalSize / numberPeople + 0.5);
else
	averageSize = 0;

cout << "The average shoe size is " << averageSize << endl;
}

return 0;
}


Out put reads

Please enter next persons shoe size: 2
Please enter next persons shoe size: 2
Please enter next persons shoe size: 0
The number of people is 3
The average shoe size is 1
Press any key to continue . . .

I think the main problem is with my count of people? Please help

Is This A Good Question/Topic? 0
  • +

Replies To: Logical Error

#2 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,079
  • Joined: 01-December 05

Re: Logical Error

Posted 02 April 2007 - 02:08 PM

while (size > 0)
{
   cout << "Please enter next persons shoe size: ";
   cin >> size;

   //Update totals
   totalSize += size;
   numberPeople++;
}


The while loop still increments numberPeople after you put in a zero.

try this:
while (true)
{
	cout << "Please enter next persons shoe size: ";
	cin >> size;
	if (! size > 0 )
		break;
	totalSize += size;
	numberPeople++;
}

Was This Post Helpful? 0
  • +
  • -

#3 mattman059  Icon User is offline

  • Epic Awesomeness
  • member icon

Reputation: 15
  • View blog
  • Posts: 538
  • Joined: 23-October 06

Re: Logical Error

Posted 02 April 2007 - 02:11 PM

and if youre uncomfortable doing it that way, I got it to work by just prompting the user for a size before the while loop like this

cout << "Please enter a size";
cin >> size;

while (size > 0)
cout << "Please enter a size";
cin >> size;
...
...



Was This Post Helpful? 0
  • +
  • -

#4 Lydia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 03-May 06

Re: Logical Error

Posted 02 April 2007 - 02:32 PM

I understand better what mattman059 is suggesting but If I change it to the following my loop never ends.... How did you lay it out for it to work?

#include <iostream>
using namespace std;

int main( )
{
int numberPeople;
float size, totalSize;
float averageSize;

//initialize
totalSize = 0;
numberPeople = 0;

cout << "Please enter a size" << endl;
cin >> size;

while (size > 0)
cout << "Please enter a size";
cin >> size;

//Update totals
totalSize += size;
numberPeople++;
}//End of loop

//Display the number of people
cout << "The number of people is " << numberPeople << endl;
{
//Average 0.5 is added in order to round off
if (numberPeople > 0)
averageSize = int (totalSize / numberPeople + 0.5);
else
averageSize = 0;
cout << "The average shoe size is " << averageSize << endl;
}


return 0;
}

This post has been edited by Dark_Nexus: 02 April 2007 - 04:43 PM

Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Logical Error

Posted 02 April 2007 - 04:36 PM

You are missing a bracket...
while (size > 0){
cout << "Please enter a size";
cin >> size;

//Update totals
totalSize += size;
numberPeople++;
}


This loop should be broken when a size of less than 0 is entered...a negative number.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1