loop

anyone know how to make 7 numbers from a file get plugged into the equation with loop?
1
2
3
4
5
for (int counter = 1; counter >=7; counter ++)
if (number < low)
{low = number;}
if (number>high)
{high = number;
Last edited on
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

int main()
{
	int values[7];

	std::ifstream iFile;

	iFile.open("File.txt", std::ios_base::in);

	if(iFile.good())
	{
		std::string line;
		int i = 0;

		while(std::getline(iFile, line)) //this assumes the numbers are each on different lines and that there are only 7 numbers
		{
			std::stringstream ss;

			ss << line;

			ss >> values[i];

			i++;
		}

		iFile.close();
	}

	return 0;
}
Last edited on
so far it only gets me highest lowest total and average of all my numbers but i have 8 rows 7 collumns. ill try seeing what i can change thanks


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
 #include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
	
	string line;
	ifstream myfile;
	ofstream newfile; 
int low= 999999;
int high = -999999;
float sum=0;
float average=0;
int number;




myfile.open("C:\\Users\\ser\\Documents\\orig.txt");

if (!myfile)	//there was an error on open, file not found
{	
	cout << "Cannot open file, terminating program"<< endl;
	system("pause");
exit (1);
}

newfile.open("C:\\Users\\ser\\Documents\\new.txt");

do 
{
		
		for	(int counter = 1; counter <= 7; counter++)
		{
			cout << line << "";
			newfile << line  << "";
		}

	
if (myfile >> number)
{sum += number;
	//adds up row in line
}
//validates high and low number
	{
		high = number;
		low = number;
	}
		while (myfile >> number)
	{
		if(number > high)
{
high = number;
}
else if (number < low)
{
low = number;
}
	}	
// outputs number
	average = sum / 7; 
		
cout << "highest number= " << high << endl;
cout << "lowest number= " << low << endl;
cout << "total number= " << sum << endl;
cout << "average= " << average << endl;

 newfile << endl;
 newfile << "The highest number is: " << high << endl;
 newfile << "The lowest number is: " << low << endl;
 newfile << "The total of the numbers is: " << sum << endl;
 newfile << "The average of the numbers is: " << average << endl << endl;

	
high = -999999;
low = 999999;
sum = 0;
average = 0;
cout << endl << endl;

}while (!myfile.eof());
	cout << endl << endl << "\t complete" << endl;
	newfile << endl << endl << "\t complete" << endl;
	newfile.close();
	myfile.close();
	return 0;
}
Topic archived. No new replies allowed.