hello, i need to find the least number in the file by a pointer. If the least number in the beginning of the file the program does not find it. Write your tips please.
Put your code between code tags so it will have indentation and will look nice: [code ] insert it here [/code]
Do not use global variables.
Check if the file opened successfully. like if ( !f ) { /* handle error */ }
Do not loop on eof(). use while( f >> var ) { } instead
Use more meaningful variable names
Do you really need to use new ?
1 2
s=newint;
s=&g;
Uhmm, By doing this you lose the address of the newly allocated integer and will have memory leak and
crash later because of delete s;. Do you meant s = newint( g ) instead ?
Can you post the instructions so we can evaluate it further ?
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
ifstream f( "ugiai.txt" );
if( !f ) { // if stream is bad
cout << "Error in opening file. exiting...\n";
return 1;
}
int* least = newint;
int* current = newint;
if( f >> *least ) { // if reading of first number succeeds
while( f >> *current ) { // read the rest (breaks if bad input is encountered)
if( /* *current is less than *least */ )
// then change *least to *current
}
}
cout << "Least number : " << *least << '\n';
delete least;
delete current;
}