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?
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';
}
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.
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;
}
#include <fstream>
#include <iostream>
int main ( )
{
std::ifstream infile ( "Stock 1 Price To Book.txt" );
float num, sum = 0, max = 0, min = 0;
unsignedint 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 ( );
}
}