What am I doing wrong?

Hello,

I appreciate any help anyone is willing to provide. I am trying to solve the following problem.
write a program with a loop that lets the user enter a series of integers, followed by -99 to signal the end of the series. After all the numbers have been entered, the program should display the largest and smallest numbers entered.

Below is my code. The wants to make the minimum number negative and the max a positive. If min/max are anything other than a -/+ then it gives me so off the wall number.

once again thank you for your help.



#include <iostream>

using namespace std;

int main()
{
//Declares a double variables
double number,max, min;
int sentinal = -99;




//for loop to calculate Calories Burned

while (number != sentinal)
{

//Ask user to input number
cout<< "Enter your number" << endl;
cout<< "Enter -99 if you have finished" << endl;
cin>> number;
if (number == sentinal)
break;
else if (number >= max)
{
max >= 0 || max <= 0;
max = number;
}
else if (number <= min)
{
min >= 0 || min <= 0;
min = number;

}
else
{
max = max;
min = min;
}




}
cout<< max << "\t\t" << min <<endl;

system("PAUSE");
return EXIT_SUCCESS;
}
Use code tags please.

What is the problem you're having?
Ok first of all 3 variables are un-initialized so it wont even compile properly cause you use number before it is initialized. Fix that first
The variable 'number' is being used without being initialized. Just put number = 0 below int sentinal = -99.

This is a problem because you're using the variable 'number' in your while parameters without it having a value.

The variables min and max share the same problem seeing you're using them in if statements but they don't have a value yet.

:)
Last edited on
Here fixed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <iostream>
using namespace std;
int main()
{
//Declares a double variables 
double number=0,max=0, min=0;
int sentinal = -99;
int count = 0;
//for loop to calculate Calories Burned
while (number != sentinal)
{

//Ask user to input number
cout<< "Enter your number" << endl;
cout<< "Enter -99 if you have finished" << endl;
cin>> number;
if (count == 0) min = number;
count++;
if (number == sentinal)
break;
else if (number >= max) 
{ 
max >= 0 || max <= 0;
max = number;
}
else if (number <= min) 
{ 
min >= 0 || min <= 0;
min = number;
}
else
{
max = max;
min = min;
}
}
cout<< max << "\t\t" << min <<endl; 

system("PAUSE");
return 0;
}
Lines 24 and 29 don't make sense, the else on line 32-36 is useless, and since you use a break inside the while the condition is redundant.

Comparison for equality with floating point numbers should never be done.

I'd also suggest to check if (number > max) and (number < min). FIrst because of floats equality again, second because you don't really need to change the value of a variable if that value is identical to the previous.
Last edited on
Another thing. You should put max and min at a specificated value which is not 0, because the user can also insert only negative numbers, or only positive. My advice is
float max=-100; (just because if you put in -99 the cycle finishes)
float min=100000 (a big number, you should initialize to the maximum value, which is 3.4 x 10 ^34, if I remember right, but it doesn't matter)

then... if it's not compulsory, avoid using float
Happy easter everyone (sorry for my English)
Thank you everyone. As you can tell I am beginner.
Also dont use system pause.

Use cin.get.
Topic archived. No new replies allowed.