is being used without being initialized

Hey ! I'm new to the forums and new to C++ too, so I have a homework in which I have to make a program that will determine which of the 5 numbers the user enters is the lowest and the highest. My code goes like this :

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
43
44
45
46
47
48
49
50
51
//But : Créer un programme qui lit 5 entiers et qui dit lequel de ces entiers est le plus gros et le plus petit
//Auteur : Anthony Gauthier
//Date : 25 octobre 2012

#include <iostream>
int main()

{
using namespace std;								    //Ne pas a avoir a ecrire std::
setlocale (LC_ALL, ""); 								//Caractères accentués

int iNb1;												//Variables utilisées
int iNb2;
int iNb3;
int iNb4;
int iNb5;
int iMax;
int iMin;

cout << "Veuillez entrer un premier nombre" << endl;	//Demander à l'utilisateur un premier nombre
cin	 >> iNb1;											//Prend en note le nombre entrer				
	
cout << "Veuillez entrer un deuxième nombre" << endl;   //Demander à l'utilisateur un deuxième nombre
cin	 >> iNb2;											//Prend en note le nombre entrer

cout << "Veuillez entrer un troisième nombre" << endl;  //Demander à l'utilisateur un troisième nombre
cin	 >> iNb3;											//Prend en note le nombre entrer
											
cout << "Veuillez entrer un quatrième nombre" << endl;  //Demander à l'utilisateur un quatrième nombre
cin	 >> iNb4;											//Prend en note le nombre entrer

cout << "Veuillez entrer un cinquième nombre" << endl;	//Demander à l'utilisateur un cinquième nombre
cin	 >> iNb5;											//Prend en note le nombre entrer

system ("cls");											//Faire disparaître le texte à l'écran

if (iNb1<iNb2)iMax=iNb2;								//Trouver le plus grand nombre
if (iNb1<iNb3)iMax=iNb3;
if (iNb1<iNb4)iMax=iNb4;
if (iNb1<iNb5)iMax=iNb5;
cout << "Le plus grand nombre est : " << iMax << endl;	//Afficher le plus petit nombre

if (iNb1>iNb2)iMin=iNb2;								//Trouver le plus petit nombre
if (iNb1>iNb3)iMin=iNb3;
if (iNb1>iNb4)iMin=iNb4;
if (iNb1>iNb5)iMin=iNb5;
cout << "Le plus petit nombre est : " << iMin << endl;	//Afficher le plus petit nombre	

system ("Pause");										//Permet au programme de rester à l'écran
return 0;												//Tout fonctionne, on retourne à 0
}


iMin is not being initialized apparently

Thanks for all your future answers !
Last edited on
So initialize it on line 18 like this :
int iMin = 0;

It is recommended to do the same with all your variables.
Yeah I tried that, it works, but then "iMin" which is the lowest number always is 0 for some reasons ... I don't get it
Do you mean

1
2
3
4
5
iMax = iNb1;
if (iMax<iNb2)iMax=iNb2;
if (iMax<iNb3)iMax=iNb3;
if (iMax<iNb4)iMax=iNb4;
if (iMax<iNb5)iMax=iNb5;


And similarly for iMin

Andy
Last edited on
Oh my god, thank you !

I did what you say, now it's like this and it works :

1
2
3
4
5
6
7
8
9
10
11
12
13
iMax = iNb1;
if (iMax<iNb2)iMax=iNb2;
if (iMax<iNb3)iMax=iNb3;
if (iMax<iNb4)iMax=iNb4;
if (iMax<iNb5)iMax=iNb5;
cout << "Le plus grand nombre est : " << iMax << endl;	//Afficher le plus petit nombre

iMin = iNb1,2,3,4,5;
if (iMin>iNb2)iMax=iNb2;
if (iMin>iNb3)iMax=iNb3;
if (iMin>iNb4)iMax=iNb4;
if (iMin>iNb5)iMax=iNb5;
cout << "Le plus petit nombre est : " << iMin << endl;	//Afficher le plus petit nombre	 
What's this about?

iMin = iNb1,2,3,4,5;
To be honest, my teacher asked me the same thing and .. I didn't remember why I wrote this xD But it's working as expected now, thanks a bunch ! :)
Last edited on
Topic archived. No new replies allowed.