lundi 22 mai 2017

cannot get a value outside a loop in c++

here is the code it runs smoothly except the value of i is different from the value of i inside the loop

#include <iostream>

using namespace std;

int main ()
{
int x, f, c, i, y, z;
int d, o, g, e;
int b;




int a;
cout << "input data " << endl;
cin >> a;

int sight[100] = { 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, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 54, 65, 66, 67, 68, 68 , 70,
71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
81, 82, 83, 84, 85, 86, 87, 88, 89, 80,
91, 92, 93, 94, 95, 96, 97, 98, 99, 100};


y=0;
for(i=sight[0]; i<101; ++i )

{   

if(a==i)
{
cout << "scanned found " << i << endl;//the value of i here is correct
d=i-11;
o=i-9;
g=i+9;
e=i+11;
cout << "coordinates location " << d<<" "<< o<<" "<< g<<" " <<e << endl;    
x=1;
cout <<x << endl;
}
else
{
x=0;
}  



z=max(x,y);
if(z==0)
{   cout << "nothing found " << endl;
}

else
{ cout <<"value of z " << z << endl;
}
cout <<"value of i " << i << endl;// shows a garbage value

cout <<"value of x " << x << endl; // x is also different 


return 0;


}

my program is running except the value of i and x is different from its value inside the loop how do I get the value of i and x from the loop? thank you for your help

Aucun commentaire:

Enregistrer un commentaire