My assignment is to write a template function called total() that keeps a running total of the values entered. It takes as an argument the number of values the function is to read.
In addition, I have to get my data from three separate text files. One with integers, one with doubles and one with non-numbers.
I can run each of these files on its own, but the issue I'm having is prompting the user to choose the file they want to take data from, and then running the program with that file accordingly. I tried passing the file name as a parameter but couldn't get it to work. Then I tried making separate if statements in the main() function and open the correct file depending their choice but I couldn't get that working either.
Any ideas for what I can try? Any hints or advice would be much appreciated. Thank you!
#include <iostream>
#include <fstream>
usingnamespace std;
template <typename T>
T total(T num)
{
T numbers = 0;
T total = 0;
T count = 0;
ifstream inputFile;
inputFile.open("integers.txt");
if(inputFile)
{
while (count < num)
{
inputFile >> numbers;
total += numbers;
numbers++;
count++;
}
cout << total << endl;
inputFile.close();
return total;
}
else
{
cout << "Error opening file.";
}
}
int main()
{
int choice = 0;
//Select the type of file
cout << "Choose the number of the file type you want to total:" << endl
<< "1. Integers\n2. Doubles\n3. Strings\n" << endl;
cin >> choice;
//Select the amount of data to read from the file (num/n)
if (choice == 1)
{
cout << "How many lines of data would you like to total?";
int n = 0;
cin >> n;
total(n);
}
if (choice == 2)
{
cout << "How many lines of data would you like to total?";
double n = 0.0;
cin >> n;
total(n);
}
if (choice == 3)
{
cout << "How many lines of data would you like to total?";
int n;
cin >> n;
total(n);
}
if (choice < 1 || choice > 3) {
cout << "Invalid choice. Enter integers, doubles or strings." << endl;
cout << "Choose the file type you want to total: integers, doubles, or strings.";
cin >> choice;
}
return 0;
}
/**
* Reads a given number of tokens of a given type from a file,
* accumulating them using the += operator, and returning the total.
*
* @param T - the type of data to be parsed
* @param num - the number of token to read from the given file
* @param filename - the name of the file to be opened and read from
*/
template <typename T> T total(int num, string filename) {
T token; // where we store each value as we read it in
T total; // the total accumulated result (use default constructor instead of 0, T may not be a numeric type)
int count = 0; // how many values we have read in so far
ifstream inputFile(filename); // open the given file
// loop until we have read "num" values or until a read fails
while( (count++ < num) && (inputFile >> token) )
total += token; // accumulate the values
// check, did we read num tokens in, or did the reading fail early
if( count == num )
return total;
elsethrow count; // or signify an error some other way
}
T num instead of T int because I know I would be passing a double to it at some point but I'm getting an error when its calling total(n, "integers.txt).
If I change it to T total (int num, string filename) I'm getting an error in the same line "no matching function for call to total(int&, const char[13])
Any idea what I'm doing wrong or what else I can try? I appreciate all the help and advice. Thanks again!
#include <iostream>
#include <fstream>
usingnamespace std;
template <class T>
T total(int num, string filename)
{
T numbers;
T total;
T count = 0;
ifstream inputFile(filename);
//inputFile.open(filename);
while ( (count++ < num) && (inputFile >> numbers) )
total += numbers;
if (count == num)
return total;
else
{
cout << "Error opening file.";
}
}
int main()
{
int choice = 0;
//Select the type of file
cout << "Choose the number of the file type you want to total:" << endl
<< "1. Integers\n2. Doubles\n3. Strings\n" << endl;
cin >> choice;
//Select the amount of data to read from the file (num/n)
if (choice == 1)
{
cout << "How many lines of data would you like to total?";
int n = 0;
cin >> n;
total(n, "integers.txt");
}
if (choice == 2)
{
cout << "How many lines of data would you like to total?";
double n = 0.0;
cin >> n;
total(n, "doubles.txt");
}
if (choice == 3)
{
cout << "How many lines of data would you like to total?";
int n;
cin >> n;
total(n, "not_numbers.txt");
}
if (choice < 1 || choice > 3) {
cout << "Invalid choice. Enter integers, doubles or strings." << endl;
cout << "Choose the file type you want to total: integers, doubles, or strings.";
cin >> choice;
}
return 0;
}