# Need help with lots of stuff

Page 1 of 1

## 6 Replies - 736 Views - Last Post: 24 July 2009 - 07:46 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=116630&amp;s=46fc1bcb47b6b21479f6807a6f4d59e3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Spiffyness

Reputation: 0
• Posts: 11
• Joined: 21-July 09

# Need help with lots of stuff

Posted 24 July 2009 - 01:43 PM

Uhhh... I guess I'll just give the description of what this program is supposed to do:

"Write a program that reads up to 10 donation values into an array of double. The program should terminate input on non-numeric input. It should report the average of the numbers and also report how many numbers in the array are larger than the average."

Here's my code:

```//2.1
#include <iostream>
const int Max = 10;
int main()
{
using namespace std;
double donation[Max];
int i = 0;
cout << "Enter the values of up to 10 donations: <q to terminate>\n"
<< "Donation #" << i + 1 << ": ";
while (i < Max && cin >> donation[i])
{
if (++i < Max)
cout << "Donation #" << i + 1 << ": ";
}
if (i < Max - 1)
{
cout << "Input terminated.\n";
cin.clear();
cin.get();
}
double total;
int j;
for (j = 0; j < i; j++)
total += donation[j];
double avg = total / (i + 1);
cout << "\nThe average donation is \$" << avg << ".\n";
int n;
for (j = 0; donation[j] < avg; j++)
n++;
cout << n << " donations are above-average.\nBye!";
cin.get();
cin.get();
return 0;
}

```

This program works really erratically. It always returns incorrect values, but they vary wildly. One time it reported that 13 donations were above-average... when I only input 10.

I think the "cease input on non-numeric input" thing is working correctly, but it's pretty sloppy.

Sorry I can't give any more specific info... it's inconsistent and the numbers are all over the place. HELP! Thanks in advance.

This post has been edited by Spiffyness: 24 July 2009 - 01:45 PM

Is This A Good Question/Topic? 0

## Replies To: Need help with lots of stuff

### #2 eker676

• Software Engineer

Reputation: 378
• Posts: 1,833
• Joined: 18-April 09

## Re: Need help with lots of stuff

Posted 24 July 2009 - 01:48 PM

Look at these articles:

Checking user input - C++

Control Structures

BTW: Constants are usually defined with an all caps name like MAX.

Also beware of integer division side effects that may be throwing off your average.
EXAMPLE:
```int a = 1;

a = 1/3;

cout << a << endl;
```

That will produce a zero because .33333 is truncated during integer division.

This post has been edited by eker676: 24 July 2009 - 01:50 PM

Reputation: 9
• Posts: 120
• Joined: 11-July 09

## Re: Need help with lots of stuff

Posted 24 July 2009 - 02:11 PM

Forget that it's not working lol.

This post has been edited by YamNad: 24 July 2009 - 02:35 PM

### #4 Spiffyness

Reputation: 0
• Posts: 11
• Joined: 21-July 09

## Re: Need help with lots of stuff

Posted 24 July 2009 - 02:43 PM

YamNad, on 24 Jul, 2009 - 01:11 PM, said:

Forget that it's not working lol.

Anyway... here's what I have now. I changed a lot of it up.

```//2.1
#include <iostream>
const int MAX = 10;
int main()
{
using namespace std;
double donation[MAX];
int i = 0;
cout << "Enter the values of up to 10 donations: <q to terminate>\n"
<< "Donation #" << i + 1 << ": ";
while (i < MAX && cin >> donation[i])
{
if (++i < MAX)
cout << "Donation #" << i + 1 << ": ";
}
if (i < MAX - 1)
{
cout << "Input terminated.\n";
cin.clear();
cin.get();
}
double total = 0;
int j;
for (j = 0; j < i; j++)
total += donation[j];
double avg = total / (i);
cout << "\nThe average donation is \$" << avg << ".\n";
int n = 0;
j = 0;
while (j < i)
{
if (donation[j] > avg)
n++;
j++;
}
cout << n << " donations are above-average.\nBye!";
cin.get();
cin.get();
return 0;
}

```

It works, but I'm sure it's not a very efficient or clean program. Does anyone have any advice for improving it?

### #5 mono15591

• D.I.C Regular

Reputation: 12
• Posts: 406
• Joined: 05-November 08

## Re: Need help with lots of stuff

Posted 24 July 2009 - 03:48 PM

Well code looks better when you create all variables in the beginning instead of when you use them in your code
it doesn't make a difference but it looks more neat.

### #6 Spiffyness

Reputation: 0
• Posts: 11
• Joined: 21-July 09

## Re: Need help with lots of stuff

Posted 24 July 2009 - 07:12 PM

Yeah, I think so, too... but my book says to declare variables right before they're used... which is the most common method?

Is there any better way to construct the loops? The last 'while' one in particular? It's buggin' me and I don't know why

### #7 log3337

Reputation: 0
• Posts: 77
• Joined: 22-July 09

## Re: Need help with lots of stuff

Posted 24 July 2009 - 07:46 PM

you can declare anything at anytime, as long it is declared before it is used.

as soon as a program reads a declaration such as: "double a" it creates a space in the system memory called "a" and it know that it will hold a number with a decimal i.e. 45.9

so it really doesn't matter where you put your declaration