Building a Simple Template Function

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!

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
  #include <iostream>
#include <fstream>
using namespace 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;
}
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
/**
 * 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;
    else
        throw count; // or signify an error some other way
}
Last edited on
Thanks very much! Very helpful.

Still having issues, however.

I kept the

T total(T num, string filename)

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!

Here's my current code:

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
#include <iostream>
#include <fstream>
using namespace 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;
}

Last edited on
Topic archived. No new replies allowed.