While Loop Help

Sum of numbers wrong

Page 1 of 1

11 Replies - 6000 Views - Last Post: 22 January 2009 - 04:45 PM Rate Topic: -----

#1 shaba_levi  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 19
  • Joined: 14-December 08

While Loop Help

Posted 22 January 2009 - 02:44 PM

Hey guys, need a little help again!

My assignment is this: Create a C++ console application that uses a while loop to count, total, and average a series of positive integers entered by a user. The user enters a 1 to signal the end of data input and to display the count, total, and average of the numbers entered.

Im almost there! here is my code:
//Specification: Find total, sum, and average of user input using While Loop
#include <iostream> 

using namespace std;

int main(){
	  int userInput = 0;
	  int const endData = -1;
	  cout << "This program will count, total, and average your numbers\n";
	  cout << "Please enter a number, enter -1 to display results and exit\n";
	  cin >> userInput;
	  //while loop
	  int totalNums = 0;
	  int sum = 0;
	  double average = 0;
	  int sentinalValue = endData;
	  int loopControlVariable = userInput; //prime the loop
	  while (loopControlVariable != sentinalValue){
			cin >> loopControlVariable;
			totalNums = totalNums + 1;
			sum = sum + loopControlVariable;
			average = sum / totalNums;
	  }
	  cout << "Youve entered " << totalNums << " numbers\n";
	  cout << "The sum of these numbers is " << sum << endl;
	  cout << "The average of these numbers is " << average << endl;
return 0;
}


My problem is the sum of the numbers...It isnt adding up right and i dont know why..i think it might be skipping the number just above -1 (which ends the loop)
can anyone push me in the right direction here?

Is This A Good Question/Topic? 0
  • +

Replies To: While Loop Help

#2 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: While Loop Help

Posted 22 January 2009 - 02:55 PM

Take a look at how you declared the data types...

All integers right?

This means that if I were to enter 1.003, the program reads this as 1.
Similarly if I enter 2.9, the program sees a 2.

The decimal is truncated.
So how can we solve this?

I'll let you figure that out, it's pretty straight forward.
Was This Post Helpful? 0
  • +
  • -

#3 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: While Loop Help

Posted 22 January 2009 - 02:55 PM

You shouldn't calculate the average until after you have collected all the values.


int const endData = -1;
int sentinalValue = endData;

What's the point of this? Isn't one variable enough to store the information? A nicer way would be o use a define.

This post has been edited by Gloin: 22 January 2009 - 02:57 PM

Was This Post Helpful? 0
  • +
  • -

#4 shaba_levi  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 19
  • Joined: 14-December 08

Re: While Loop Help

Posted 22 January 2009 - 03:05 PM

View PostGloin, on 22 Jan, 2009 - 01:55 PM, said:

You shouldn't calculate the average until after you have collected all the values.


int const endData = -1;
int sentinalValue = endData;

What's the point of this? Isn't one variable enough to store the information? A nicer way would be o use a define.


The point of that was priming values, for some reason the instructor wanted me to do it...I agree with you that it was redundant, but w/e

Updated code, but i still dont understand..its probably some DOH! thing isnt it... I hate it when that happens!

//Specification:  Find total, sum, and average of user input using While Loop
#include <iostream> 

using namespace std;

int main(){
	  double userInput = 0;
	  int const endData = -1;
	  cout << "This program will count, total, and average your numbers\n";
	  cout << "Please enter a number, enter -1 to display results and exit\n";
	  cin >> userInput;
	  //while loop
	  double totalNums = 0;
	  double sum = 0;
	  double average = 0;
	  int sentinalValue = endData;
	  double loopControlVariable = userInput; //prime the loop
	  while (loopControlVariable != sentinalValue){
			cin >> loopControlVariable;
			totalNums = totalNums + 1;
			sum = sum + loopControlVariable;
	  }
	  average = sum / totalNums;
	  cout << "Youve entered " << totalNums << " numbers\n";
	  cout << "The sum of these numbers is " << sum << endl;
	  cout << "The average of these numbers is " << average << endl;
return 0;
}

Was This Post Helpful? 0
  • +
  • -

#5 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: While Loop Help

Posted 22 January 2009 - 03:26 PM

Your loopControlVariable value is always included in the sum. So instead of initializing sum to 0, make it 1.
Was This Post Helpful? 0
  • +
  • -

#6 shaba_levi  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 19
  • Joined: 14-December 08

Re: While Loop Help

Posted 22 January 2009 - 03:46 PM

View PostGloin, on 22 Jan, 2009 - 02:26 PM, said:

Your loopControlVariable value is always included in the sum. So instead of initializing sum to 0, make it 1.


Ah, that did it! but my program is now including the -1 into the answer, which i dont want...I tried making the endData value char = "q" and changing the sentinal value as well. I even got rid of the sentinal value and made the necessary changes in the loop, but it gave me this error: Error 1 error C2440: 'initializing' : cannot convert from 'const char [2]' to 'char'

What gives? If i cannot change the value, how do i make the code ignore the -1 value?

Edit: Im hungry and a bit tired from working late yesterday, so if some of this seems kind of "DUH!", please bare with me lol...

This post has been edited by shaba_levi: 22 January 2009 - 03:48 PM

Was This Post Helpful? 0
  • +
  • -

#7 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: While Loop Help

Posted 22 January 2009 - 04:00 PM

It's simpler if you show the code but naturally you can't fit a char array of size two into a char of size one. Try filling a 1 litre bottle with 2 litres of water.

This post has been edited by Gloin: 22 January 2009 - 04:00 PM

Was This Post Helpful? 0
  • +
  • -

#8 shaba_levi  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 19
  • Joined: 14-December 08

Re: While Loop Help

Posted 22 January 2009 - 04:06 PM

View PostGloin, on 22 Jan, 2009 - 03:00 PM, said:

It's simpler if you show the code but naturally you can't fit a char array of size two into a char of size one. Try filling a 1 litre bottle with 2 litres of water.


//Specification: Find total, sum, and average of user input using While Loop
#include <iostream> 

using namespace std;

int main(){
	  double userInput = 0;
	  char endData = "q";
	  cout << "This program will count, total, and average your numbers\n";
	  cout << "Please enter a number, enter q to display results and exit\n";
	  cin >> userInput;
	  //while loop
	  double totalNums = 0;
	  double sum = 1;
	  double average = 0;
	  char sentinalValue = endData;
	  double loopControlVariable = userInput; //prime the loop
	  while (loopControlVariable != sentinalValue){
			cin >> loopControlVariable;
			totalNums = totalNums + 1;
			sum = sum + loopControlVariable;
	  }
	  average = sum / totalNums;
	  cout << "Youve entered " << totalNums << " numbers\n";
	  cout << "The sum of these numbers is " << sum << endl;
	  cout << "The average of these numbers is " << average << endl;
return 0;
}


I dont understand why im getting the error..shouldnt that code be right?, what am i missing?

even if i were to:
char sentinalValue = endData; //delete this
	  double loopControlVariable = userInput; //prime the loop
	  while (loopControlVariable != endData){ //changed the value



I still get the error..

This post has been edited by shaba_levi: 22 January 2009 - 04:08 PM

Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: While Loop Help

Posted 22 January 2009 - 04:17 PM

Change char endData = "q"; to char endData = 'q';

"q" is an array of two characters, the 'q' and the terminating null ('\0'). 'q' is just the 'q' character only.
Was This Post Helpful? 0
  • +
  • -

#10 OrganizedChaos  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 153
  • Joined: 29-November 08

Re: While Loop Help

Posted 22 January 2009 - 04:25 PM

Since you already primed the loop, try this:

while (loopControlVariable != sentinalValue){
			totalNums = totalNums + 1;
			sum = sum + loopControlVariable;
			cin >> loopControlVariable;
}


Was This Post Helpful? 0
  • +
  • -

#11 OrganizedChaos  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 153
  • Joined: 29-November 08

Re: While Loop Help

Posted 22 January 2009 - 04:33 PM

Another problem you're going to have is when you go to enter 'q' the program is not going to work how you want it to because you're trying to store a char, 'q' into a double, userInput.

EDIT: I was just looking back in this thread and noticed you had -1 as the end value to begin with. Get rid of the chars and go back to that -1 since that's what the assignment's looking for anyway. Additionally, what's the point of loopControlVariable? You can get rid of that AND endValue, since that's unneeded also and end up with something like this:

#include <iostream>

using namespace std;

int main(){
	  double userInput = 0;
	  double totalNums = 0;
	  double sum = 0;
	  double average = 0;
	  cout << "This program will count, total, and average your numbers\n";
	  cout << "Please enter a number, enter -1 to display results and exit\n";
	  cin >> userInput;
	  //while loop
	  while (userInput != -1){
			totalNums++;
			sum = sum + userInput;
			cin >> userInput;
	  }
	  average = sum / totalNums;
	  cout << "Youve entered " << totalNums << " numbers\n";
	  cout << "The sum of these numbers is " << sum << endl;
	  cout << "The average of these numbers is " << average << endl;
return 0;
}


This post has been edited by OrganizedChaos: 22 January 2009 - 04:47 PM

Was This Post Helpful? 0
  • +
  • -

#12 shaba_levi  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 19
  • Joined: 14-December 08

Re: While Loop Help

Posted 22 January 2009 - 04:45 PM

Thanks for all your help guys! The program works, im too tired to figure out what did it, but here it is, working as intended
//Specification:  Find total, sum, and average of user input using While Loop
#include <iostream> 

using namespace std;

int main(){
	  double userInput = 0;
	  int const endData = 0;
	  cout << "This program will count, total, and average your numbers\n";
	  cout << "Please enter a number, enter 0 to display results and exit\n";
	  cin >> userInput;
	  //while loop
	  double totalNums = 0;
	  double sum = 0;
	  double average = 0;
	  int sentinalValue = endData;
	  double loopControlVariable = userInput; //prime the loop
	  while (loopControlVariable != sentinalValue){
			totalNums = totalNums + 1;
			sum = sum + loopControlVariable;
			cin >> loopControlVariable;
	  }
	  average = sum / totalNums;
	  cout << "Youve entered " << totalNums << " numbers\n";
	  cout << "The sum of these numbers is " << sum << endl;
	  cout << "The average of these numbers is " << average << endl;
return 0;
}


I switched some values...

This post has been edited by shaba_levi: 22 January 2009 - 04:47 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1