How to calculate certain cells of numbers instead of entire txt file in C++?

As you can see, the code below calculates the average, sum, number of items, etc, for a list of numbers in two separate files and compares them with one another.

e.g. we will have a text file of 10 numbers;

45
65
24
26
26
36
35
100
109
433
etc...

The problem is that the code will perform calculations on all the numbers in the txt. or csv. file. This is problematic because if there is any text in the file, e.g. headings, then the calculations will not be performed. For instance, suppose that I only wanted to include rows 5-10 in the calculations, how would I specify this in my C++ code?

#include <iostream>
#include <cmath>
#include <math.h>
#include <fstream>
#include <string>
#include <numeric>
using namespace std;

int main()
{
ifstream infile;
float num;
float total;
float x;
float aver;

x = 0; total = 0;
infile.open("Stock 1 Price To Book.txt");

while (!infile.eof())
{
infile >> num;
total = total + num;
x++;
}

aver = (total - num) / (x - 1);

cout << "Price/Book Value" << "\n";
cout << "Final Array Value: " << num << "\n";
cout << "Sum: " << (total - num) << '\n';
cout << "Number of items: " << x - 1 << '\n';
cout << "Average: " << aver << '\n';
cout << "" << '\n';

infile.close();

{
ifstream infile2;
float num2;
float total2;
float x2;
float aver2;
float ratio;

x2 = 0; total2 = 0;
infile.open("Stock 1 Return on Equity.txt");

while (!infile.eof())
{
infile >> num2;
total2 = total2 + num2;
x2++;
}

aver2 = (total2 - num2) / (x2 - 1);

cout << "Return on Equity" << "\n";
cout << "Final Array Value: " << num2 << "\n";
cout << "Sum: " << (total2 - num2) << '\n';
cout << "Number of Items: " << x2 - 1 << '\n';
cout << "Average: " << aver2 << '\n';
cout << "" << '\n';

ratio = num / aver2;

if (ratio < 0.3)
{
cout << "Terminal P/B is " << ratio * 100 << "% of average Return on Equity. Stock is a buy." << '\n';
}
else
{
cout << "Terminal P/B is " << ratio * 100 << "% of average Return on Equity. Stock is NOT a buy." << '\n';
}

}

getchar();
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
string dump;

int i;

for ( i = 0; i < START_ROW; ++i )
	getline ( infile, dump );

for ( i = START_ROW; i <= END_ROW; ++i )
{
	infile >> num2;
	total2 += num2;
	++x2;
}
Two things:

1) When I do this, I am getting an error that string is an undeclared identifier.

2) I rewrote the code as eof was causing inefficiencies. Can you please tell me how I should specify?

//Make a price-to-book system; see which ones are trading below their historical average.
//Have two files for each; one which calculates price-to-book; another to calculate earnings growth.


#include <fstream>
#include <iostream>
#include <cmath>
#include <algorithm> // std::max

int main() {
std::ifstream infile("Stock 1 Price To Book.txt");

float num;
float total = 0.0f;
unsigned int count = 0;

float sum = 0, max = 0, min = 0;


// While infile successfully extracted numbers from the stream
while (infile >> num) {
total += num;
++count;

sum += num;
if (num>max) max = num;
if (num<min) min = num;

}
// don't need the file anymore, close it
infile.close();

// test to see if anything was read (prevent divide by 0)
if (!count) {
std::cerr << "Couldn't read any numbers!" << std::endl;
return 1;
}

// give the average
std::cout << "Number of variables: " << count << std::endl;
std::cout << "The average is: " << total / count << std::endl;
std::cout << "The maximum is: " << max << std::endl;
std::cout << "The minimum is: " << min << std::endl;
std::cout << "The sum is: " << sum << std::endl;

// pause the console
std::cin.sync();
std::cin.get();
getchar();
return 0;
}
I forgot to mention it last time, but please use code blocks when posting code here. (click the icon to the right that looks like '<>')
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
#include <fstream>
#include <iostream>


int main ( )
{
	std::ifstream infile ( "Stock 1 Price To Book.txt" );

	float num, sum = 0, max = 0, min = 0;

	unsigned int count = 0, startLine, endLine, i;


	std::cout << "What line do you want to start reading numbers from?\n";

	std::cin >> startLine;

	std::cout << "\nWhat is the last line you want to raed numbers from?\n";

	std::cin >> endLine;


	// ignore lines between the start and the starting line
	for ( i = 0; i < startLine; ++i )
		infile.ignore ( 1000, '\n' ); // ignores 1000 chars or until it hits the end of the line


	// read numbers in from the starting line to the endng line
	for ( i = startLine; i <= endLine; ++i )
	{
		infile >> num;

		sum += num;
		++count;

		if ( num > max ) max = num;
		if ( num < min ) min = num;
	}

	// don't need the file anymore, close it
	infile.close ( );


	// test to see if anything was read ( prevent divide by 0 )
	if ( !count ) std::cout << "\nCouldn't read any numbers!\n\n";

	else
	{
		// give the average
		std::cout << "\nNumber of variables: " << count << '\n';
		std::cout << "The average is: " << sum / count << '\n';
		std::cout << "The maximum is: " << max << '\n';
		std::cout << "The minimum is: " << min << '\n';
		std::cout << "The sum is: " << sum << '\n';

		// pause the console
		std::cin.sync ( );
		std::cin.ignore ( );
	}
}
Last edited on
Topic archived. No new replies allowed.