# include <iostream>
# include <fstream>
usingnamespace std ;
int main ()
int BiggestNumber = 0 ; // The number that should be displayed
int number ; // number entered by a user
cout << "The current Biggest number is : " << BiggestNumber ; // showing the current big number
for (int i=0 ; i<5 ; i++) // the prgram runs 5 times only
ofstream BigNumber ("BigNumber.txt", ios::app); // creating a file
cout << "Enter Your Number : " ; // entering a number
cin >> number ;
if (number > BiggestNumber ) // comparing the number with the BiggestNumber
// if SO , store BiggestNumber in the file
BiggestNumber = number ;
BigNumber << BiggestNumber << endl;
BigNumber.close () ;
cout << "The Biggest number is : " << BiggestNumber << endl ;
OK , here is the problem
First run : Sample input
According to me this is correct because I want to display the biggest number entered by a user
but if you run again.....
the output will be :
The current Biggest number is : 0........and that's my problem it should be 20 instead of 0.........
So , The problem arises because BiggestNumber is always initialized to ZERO but that I don't wanna do......it must be the biggest number entered
by the user
a Program that displays the biggest number in a file
I am puzzled as to what this means. I assumed it meant the numbers are already in the file and the program has to read in the numbers from the file then display the biggest number on the screen.
On the other hand your program is using the file as output, and taking input from the keyboard. In this case, the main problem is the file is created inside the for-loop. Instead, I would create the file at the start before the loop begins and output the biggest number at the end, after the loop has finished.
If the numbers may include negative values, then choosing an initial value of zero may not be correct. In that case you could either set BiggestNumber to the first number entered by the user. Or set it to the smallest possible integer value, std::numeric_limits<int>::min() http://www.cplusplus.com/reference/limits/numeric_limits/