# Logical Error

Page 1 of 1

## 4 Replies - 6104 Views - Last Post: 02 April 2007 - 04:36 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=25899&amp;s=ca94d07de1b86db6fc7bcbf4979b7021&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Lydia

• New D.I.C Head

Reputation: 0
• 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;
}

```

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

Reputation: 307
• Posts: 3,105
• 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++;
}
```

### #3 mattman059

• Epic Awesomeness

Reputation: 15
• 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;
...
...

```

### #4 Lydia

• New D.I.C Head

Reputation: 0
• 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

• g+ + -o drink whiskey.cpp

Reputation: 250
• 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.