hi every one im in my prog the compiler wont complet the loop or output the massege if the condition is true i tride many different stiles and testing techneqes put no use.
first the dutey of the loop to cheack if the id exist in the file
and if not print an error massage this is my code.
can any one help me what is the problem.
iv also looked all oover the net but nothing
thanks so much.
void FindStud()
{ int id;
bool condition=true;
cout<<"enter the ID :";
cin>>id;
//while(condition)
//{
cout <<i<<endl;
for ( int x=0;x<=i;x++)
{ cout<<SID[x]<<endl<<id<<endl;
if (SID[x]==id)
{
cout << "print the info of the studentn "<<endl<<endl<<endl;
condition=false;}break;
}//end of for
//}//end of while
if (condition=true)
cout<<"the student is not aviliabel!!"<<endl;
}//end of void function
when i enter an id which exist it print that it is not avaliabel
after testing the program many times a reilized that whan the compiler enters the loop it cheaks for only the first index and then it goes out the loop.
i dont know why it dosent want to see the if condition or compelet the loop.
okay.... part of this is harder to see because of your poor indenting.
here's your code with better indenting... maybe this part of the problem will be more clear now:
1 2 3 4 5 6 7 8 9 10
for ( int x=0;x<=i;x++)
{
cout<<SID[x]<<endl<<id<<endl;
if (SID[x]==id)
{
cout << "print the info of the studentn "<<endl<<endl<<endl;
condition=false;
}
break; // do you see a problem with this line?
}
Your break is outside the 'if' block, so you're breaking always, no matter what. So your loop effectively runs once and then stops.
That + what I said in my earlier post about line 19.
sorry because of my poor english.
i see
even if i put the break outside it will not read the the out put statment
1 2 3 4 5 6 7 8
for ( int x=0;x<=i;x++)
{ cout<<SID[x]<<endl<<id<<endl;
if (SID[x]==id)
{
cout << "print the info of the studentn "<<endl<<endl<<endl;
condition=false;break;}
}//end of for
is the same my aime is when the compiler find the id do smth and gose out the loop .
i have already corrected my mistake ,the problem here is that the loop iterates only once and then gose out. I put the break inside gust to tell the compiler that no need to complet the
loop if you find the id that im cheacking!.
im realy lost is my whay good to do it ?
what is the value of i? if i is a global variable and you don't assign a value to it, it should be zero and the loop would iterate only once (for ( int x=0;x<=i;x++)). but I suppose i has the right value, since you print it and check on run-time that it's not zero... (is it really not a zero? what's its value?)