-1

I'm working on a program that can count the amount of numbers in a file, and addi them all together.

Ex. Let say there is a .txt file that contains the numbers "1 5 6 2 56 8". As you can see there is 6 numbers, and the sum is 78. I did that calculating these myself. In the end, I want the program to export another file telling me "The amount of number: , and the sum of the number is: "

How can I modify my program to do this with any file?

Here is my source code:

ifstream inFile;
int value1, value2, value3, value4, value5, value6, value7, value8, value9, value10,
    value11, value12, value13, value14, value15, value16, value17, value18, value19,
    value20;
double average, sum;

//OPEN FILE
inFile.open("data.txt");

//ASSIGN VALUES WITH VARIABLES
inputFile >> value1;
inputFile >> value2;
inputFile >> value3;
inputFile >> value4;
inputFile >> value5;
inputFile >> value6;
inputFile >> value7;
inputFile >> value8;
inputFile >> value9;
inputFile >> value10;
inputFile >> value11;
inputFile >> value12;
inputFile >> value13;
inputFile >> value14;
inputFile >> value15;
inputFile >> value16;
inputFile >> value17;
inputFile >> value18;
inputFile >> value19;
inputFile >> value20;

// CLOSE FILE
input.close();

//CALCULATE THE SUM
sum = value1 + value2 + value3 + value4 + value5 + value6 + value7 + value8 + value9 + value10 +
    value11 + value12 + value13 + value14 + value15 + value16 + value17 + value18 + value19 + value20;


//CALCULATE THE AVERGAGE
average = sum / 20;

ofstream outputFile;
outputFile.open("results.txt");
outputFile << "The number of the values is: 20" << endl;
outputFile << "The total is: " << sum << endl;
outputFile << "The average is: " << average << endl;
outputFile.close();

return 0;

}

Chriskt
  • 87
  • 1
  • 2
  • 9

2 Answers2

3

You should learn about for loops and while loops. In this case, a while loop is probably most applicable:

int main() {
    std::ifstream inFile("data.txt");
    int value, sum = 0, count = 0;

    while(inFile >> value) {
        sum += value;
        count += 1;
    }

    assert(count > 0);
    std::cout << "sum: " << sum << "\n";
    std::cout << "average: " << (double) sum / count << "\n";
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • Thank you! I'm still getting use to C++. I have learned for loops, while, and do while loops. I just need to understand that loops and everything I've learned can be applied to almost anything on most occasions. – Chriskt Mar 19 '16 at 21:03
0

If it is not possible to have string on the file, you can split the whole string and sum each integer, otherwise you can first split and check if each one of them is an integer, if yes, add it to total sum.

For splitting I use this and for checking if a string is integer, you can use this one.

Community
  • 1
  • 1
smttsp
  • 4,011
  • 3
  • 33
  • 62
  • If you know how many numbers you're working with you could just read them into an array and sum them in a for loop, as well as count the amount. – suroh Mar 19 '16 at 22:09
  • It is almost never a good idea to work on fixed number. If you got different number of integers, your program will fail to work. So you need to be careful – smttsp Mar 20 '16 at 07:36