uninitialized local variable simple but aggrivating

Hey, im pretty sure that uninitialized local variable means that something isnt declared properly..im not seeing it though. if someone could just point it out to me id be very grateful.

#include <iostream>
using namespace std;


int main ()
{
int numbers_entered = 0;
char letter;

double number;

double number_1;
double number_2;
double max = -1000;
double min = 1000;

do

{



cout<<"Please enter the number."<<endl;
cin>>number;
numbers_entered++;

if (number > max)
{ max = number_1;
}
else
{ min = number_2;
}




cout<<"Would you like to enter another number? (y/n)?";
cin>> letter;

}

while (letter == 'y' || letter == 'Y');

cout<<"You entered "<< numbers_entered <<" numbers." <<endl;
cout<<"The maximum number entered is "<< number_1 <<"and the minimum number entered is "<< number_2 <<"."<<endl;






return 0;
}


Problem: Create a program that uses a loop to ask the user to enter a series of numbers. The loop will count how many numbers are entered (complete) and determine the maximum and minimum values of the numbers enters(error).
What line is this occurring on? Also, use [code] tags instead of whatever you are using.
1
2
3
4
5
6
if (number > max)
{ max = number_1;
}
else 
{ min = number_2;
}


That code is not doing what you want it to do.
If a variable was neither initialized nor assigned you shouldn't use it on a right-hand side of an expression like this:

max = number_1;

because its content is undefined.

Another question is why to use so many variables in your program. It's quite useless.

Consider this (much simpler) code:

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
#include <iostream>

using namespace std;

int main()
{
int numbers_entered = 0;
char letter;
double number;
double max;
double min;

do
{
	cout << "Please enter the number." << endl;
	cin >> number;
	numbers_entered++;
	if (numbers_entered==1) { max=number; min=number; } // first number entered
	if (number > max) max = number;
	if (number < min) min = number;
	cout << "Would you like to enter another number? (y/n)? ";
	cin >> letter;
} while (letter == 'y' || letter == 'Y');
cout << "You entered " << numbers_entered << " numbers." << endl;
cout << "The maximum number entered is " << max << " and the minimum number entered is " << min << "." << endl;
}
Topic archived. No new replies allowed.