Need help with lots of stuff

terminating input on non-numeric input, loops, etc.

Page 1 of 1

6 Replies - 571 Views - Last Post: 24 July 2009 - 07:46 PM Rate Topic: -----

#1 Spiffyness  Icon User is offline

  • New D.I.C Head

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

Need help with lots of stuff

Post icon  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  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 YamNad  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#4 Spiffyness  Icon User is offline

  • New D.I.C Head

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

Re: Need help with lots of stuff

Posted 24 July 2009 - 02:43 PM

View PostYamNad, on 24 Jul, 2009 - 01:11 PM, said:

Forget that it's not working lol.


Yeah, but your comments about n and total were correct; you shouldn't have gotten rid of them! It was very helpful.

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?
Was This Post Helpful? 0
  • +
  • -

#5 mono15591  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 Spiffyness  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 :P
Was This Post Helpful? 0
  • +
  • -

#7 log3337  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 73
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1