4 Replies - 171 Views - Last Post: 13 October 2019 - 09:39 AM Rate Topic: -----

#1 Jrosal11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-October 19

Using while loops to find a sum and min/max.

Posted 13 October 2019 - 09:08 AM

#include <iostream>;
using namespace std;
int main()
{
	int min = INT_MIN;
	int max = INT_MAX;
	int sum = 0;
	int num = 0;
	while (num!= -1) {
		cout << "Enter a number" << endl;
		cin >> num;
		sum += num;
		

	}
	cout << "The sum of the numbers is: " << sum << endl;
	cout << "The largest number input was " << max << endl;
	cout << "The smallest number input was " << min << endl;

	
	
	return 1;
}


This post has been edited by Skydiver: 13 October 2019 - 09:17 AM
Reason for edit:: Put code in code tags. Learn to do this yourself.


Is This A Good Question/Topic? 0
  • +

Replies To: Using while loops to find a sum and min/max.

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7132
  • View blog
  • Posts: 24,230
  • Joined: 05-May 12

Re: Using while loops to find a sum and min/max.

Posted 13 October 2019 - 09:17 AM

In the future, please learn to put put your code in code tags.

So that's your code. What is your question?
Was This Post Helpful? 0
  • +
  • -

#3 Jrosal11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-October 19

Re: Using while loops to find a sum and min/max.

Posted 13 October 2019 - 09:24 AM

For my assignment, the sum of the numbers not including -1 needs to show, as does the max and min. When I run the code it gives me a max and min of +2147483647 or -2147483647 and the sum includes the addition of the -1. I know I'm supposed to use an if statement to give the max and min of the input numbers but I'm not sure how to do that.

This post has been edited by Skydiver: 13 October 2019 - 09:34 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7132
  • View blog
  • Posts: 24,230
  • Joined: 05-May 12

Re: Using while loops to find a sum and min/max.

Posted 13 October 2019 - 09:39 AM

There is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area.

Well the min/max that your program are the actual values of INT_MIN and INT_MAX because you assigned those values on lines 5 and 6. You are not doing anything to change those values by the time you display the variable values on lines 17 and 18.

The reason why your sum includes the -1 is because you are just blindly adding it in on line 12.

You'll need to recall how to use the if statements so that you can keep track of min/max values, as well as, not add in the -1.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,666
  • Joined: 25-December 09

Re: Using while loops to find a sum and min/max.

Posted 13 October 2019 - 09:39 AM

What have you tried?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1