Need help with variables

I'm trying to make a simple program to state minimum and maximum of 3 numbers using nested if else statements I've got everything solved i think except when i try to output the values it says i haven't stated the variables minimum and maximum. i just started learning to program yesterday so I'm still very new. Please help thank you! Please note the code isn't properly indented when i post it on the website


#include <iostream>
#include <string>
using namespace std;

int main() {
char a, b, c;
cout << "Enter 3 numbers:";
cin >> a >> b >> c;

if(a >= b) {
if(a >= c){
if(b >= c){
double minimum = c;
double maximum = a;
}

else{
double minimum = b;
double maximum = a;
}

}
else{
double minimum = b;
double maximum = c;

}


}
else {
if( a >= c){
if(b >= c) {
double maximum = b;
double minimum = a;
}

else {
double maximum = c;
double minimum = a;
}
}
else {
double minimum = c;
double maximum = b;
}

}
cout << "The maximum is:"<< maximum;
cout << "The minimum is:"<< minimum;


return 0;
}
Last edited on
The problem is that the variables minimum and maximum's scopes are inside of the if statements and will thus go away once they reach the end of their scope e.g. the end of the if statement. To fix this simply declare minimum and maximum outside of the if statements. And then instead of doing double minimum = "somevalue" inside of the if statement do minimum = "somevalue"
So then how do i make it so that minimum and maximum get set to the variable only when it satisfies the condition? Sorry thats kinda confusing
You need to declare the variables a,b,c with int. Not char.

You need to declare the variables minimum, maximum only once.
For example:
1
2
3
4
5
6
int main() {
    int a, b, c;
    double minimum{ 0.0 };
    double maximum{ 0.0 };
    cout << "Enter 3 numbers:";
    cin >> a >> b >> c;


Take the variable type double of the rest of the minimum and maximum variables on the rest of the code. For instance:

1
2
3
if (something)
   minimum = c;
   maximum = a;


Use code tags when your are posting your code.
@OP, take a look at this link :
http://www.cplusplus.com/doc/tutorial/variables/

This could answer some of your questions.
Topic archived. No new replies allowed.