Binary Files

Hello! So here are the instructions:

Write two seperate programs. The first one will write random data out to a binary file display the sum of every other number.

The second will read the file generated by the first program and add every other number.

Compare the two sums.


Here is my code so far for the first program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <string>

using std::fstream;
using std::endl;
using std::string;
using std::cout;
using std::ios;

int main()
{
    fstream theStream;
    theStream.open("Numbers.txt", ios::binary | ios::out);
    string ranNum = "abcdefghijklmnopqrstuvwxyz";
    
    for( char i : ranNum)
    {
        theStream.put(i);
    }
    theStream.close();
    return 0;
}


Since characters are technically numbers, can I do this? Also, then how can I display the sum of every other number?
The instructions call for random data, not iterative. So since you're already using x11 try something like the 'std::random_device' generator.

display the sum of every other number.

The way I read that, you would have to determine if the random character is an integer before you consider it for summation.

- std::random_device: http://www.cplusplus.com/reference/random/random_device/
Topic archived. No new replies allowed.