C/C++ C++; How to read-in a unknown amount of numbers from an external file

  • Thread starter Thread starter burton95
  • Start date Start date
  • Tags Tags
    File Numbers
AI Thread Summary
To sum an unknown number of integers from a file in C++, read the numbers one at a time in a loop until the end of the file is reached. Use an ifstream to open the file and read each number, adding it to a sum as you go. If you want to store the numbers for later use, utilize a vector to dynamically hold the values. An efficient approach to sum the numbers directly is to use the `accumulate` function with iterators, allowing for a concise one-liner solution. The discussion emphasizes that there’s no need to predefine the number of variables, making the code flexible and efficient for handling varying input sizes.
burton95
Messages
54
Reaction score
0
Basically I want to take an unknown amount of variables and sum them up. I'm sure it's simple. I know there is some way to tell VS to keep reading the opened stream from my numbers.txt file.

Code:
#include <iostream>
#include <fstream>
using namespace std;

ifstream data_input;
data_input.open("numbers.txt");
...

Before I just knew the number of variables and would declare them and read them in seperatley and sum them up. How do I say "When no more int's are left in the "numbers.txt" file quite reading and then sum them up??

Anthony
 
Technology news on Phys.org
Read the numbers one at a time in a loop. Terminate the loop when there is an error reading the next number. (Hitting the end of the file will cause an error, but you might have other errors like invalid input in the file.)

You don't need to read all the numbers first and then add them all up. You can add each number as you read it.

If you really want to store all the numbers, use a class like vector, where you don't have to declare the number of variables at the start of the program.
 
An idiomatic way to approach this is
Code:
double d;
while(data_input >> d)
{
//add d to your sum
}

Here is another way which takes just one line
Code:
#include <fstream>
#include <iterator>
#include <numeric>

int main()
{
    using namespace std;
    ifstream data_input("numbers.txt");
    double s = accumulate(istream_iterator<double>(data_input), 
                                istream_iterator<double>(), 0.0); 
}
 
  • Like
Likes 1 person
And if you really do want to save all the numbers for later use:

Code:
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main ()
{
    ifstream data_input;
    data_input.open("numbers.txt");

    double d;
    vector<double> v;  // size is zero initially

    while (data_input >> d)
    {
        // append to the end of the vector,
        // increasing its size by one
        v.push_back(d);
    }

    // verify that we read the data we wanted
    for (int k = 0; k < v.size(); ++k)
    {
        cout << v[k] << " ";
    }
    cout << endl;

    return 0;
}

I'll leave it to you to write the code to calculate the sum.
 
  • Like
Likes 1 person
Hey thanks. I got it with your help. Sorry it took so long to get back to you.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...

Similar threads

Replies
8
Views
6K
Replies
13
Views
2K
Replies
25
Views
2K
Replies
6
Views
2K
Replies
5
Views
3K
Replies
10
Views
2K
Replies
15
Views
3K
Replies
3
Views
2K
Back
Top