Help with program for class!!!

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.

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

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;
}

Thank you for your help.
Last edited on
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.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

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;
}
Ok, even I can read it now. Let's just talk about D, "Find the sum of every third number."

You are given a sequence of numbers, so you need to read them into a container, probably a vector as it can grow as you read each number.

You then index thru the vector in steps of 3 rather than 1 as you'd normally do and sum those.

Easy right?

Topic archived. No new replies allowed.