So I am writing a program for class and cannot seem to get anywhere else with this. It is supposed to be all in main and open a document and then do a series of different things. A-C all work fine. I am really stuck on D and if there is anything else that you see that looks off please let me know. D. reread file and compute sum of every third integer. E. Reread file and determine its range, smallest and largest integer. F. Reread file and find integer closest to 200.
G. Reread and compute the average of the first two lines. Output with three decimal points. H. Reread the file and compute the sum of the numbers as long as it does not go over 100.
int main()
{
ifstream FileIn;
ofstream FileOut;
int number;
FileOut.open("C:\\answers.txt");
//====Task A==================================
FileIn.open("C:\\datafile2.txt");
FileOut << "Numbers in datafile2.txt:" << endl;
while (FileIn >> number)
{
FileOut << number << " ";
}
FileOut << endl << endl;
FileIn.close();
FileIn.clear();
//====Task B==================================
FileIn.open("C:\\datafile2.txt");
double total, aver, x;
x = 0; total = 0;
while (FileIn >> number)
{
total = total + number;
x++;
}
aver = total / x;
FileOut << "The average of your numbers is: " << endl;
FileOut << aver << setprecision(3);
FileOut << endl << endl;
FileIn.close();
FileIn.clear();
//====Task C====================================
FileIn.open("C:\\datafile2.txt");
double aver2, y, total2;
y = 11; total2 = 0;
while (y >= 0)
{
total2 = total2 + number;
y--;
}
aver2 = total2 / 12;
FileOut << "The average of the first twelve numbers is: " << endl;
FileOut << aver2 << setprecision(3);
FileOut << endl << endl;
FileIn.close();
FileIn.clear();
//====Task D=======================================
FileIn.open("C:\\datafile2.txt");
int sum, z;
z = 0, sum = 0;
while (z >= 0)
{
sum = sum + number;
(z++);
}
FileOut << "The sum of every third number is: ";
FileOut << sum;
FileIn.close();
FileIn.clear();
//====Task E=======================================
FileIn.open("C:\\datafile2.txt");
bool isValid;
double Largest, Smallest;
Largest = number;
Smallest = number;
while (number != 0)
{
if (number > Largest)
{
Largest = number;
}
else
if (number < Smallest)
{
Smallest = number;
}
}
FileOut << "Your largest number is: " << Largest << endl;
FileOut << "Your smallest number is: " << Smallest << endl;
isValid = false;
FileIn.close();
FileIn.clear();
//====Task F=======================================
int besthit, Min;
Min = 0;
while (number)
{
FileIn >> number;
sum = abs(number - 200);
if (sum < Min)
{
besthit = number;
Min = sum;
}
}
FileIn.close();
FileIn.clear();
//====Task G=======================================
int Sum, count;
char Inchar;
FileIn.open("C:\\Datafile2.txt");
Sum = count = 0;
Inchar = ' ';
while (Inchar != '\n')
{
FileIn >> number;
FileIn.get(Inchar);
Sum = Sum + number;
count++;
}
FileIn.close();
FileIn.clear();
//====Task H=======================================
int Sums;
bool Over;
FileIn.open("C:\\Datafile2.txt");
Sums = 0;
Over = true;
while (Over)
{
if (Sums > 1000)
Over = false;
else
Sums = Sums + number;
}
FileIn.close();
FileIn.clear();
return 0;
}
I have reposted your code for two reasons
1. it's impossible to read with the missing formatting. In future if post any code please use the code format tags
2. I'm fed up of people creating an account, asking for help with homework on their first post and deleting it when they get help.
You have to say what the steps do. How is anyone supposed to know what step A, B and C are? As you've written the code so nicely, it should be easy fill in description of what each step ought to do.