-1
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>

using namespace std;

int main()
{
    string zeile;
    ifstream file;
    file.open("zahlen.txt");
    int array[3][8];
    int i = 0;
    int u = 0;
    while (file) {

        file >> u;
        array[i] = u;
        i++;
    }
    int f = 0;
    while (f <= 7) {

        cout << array[f];

        f++;
    }

    return 0;
}

0 0 0 0 1 1 0 1
0 0 0 1 0 0 1 1
1 1 0 1 0 1 1 0

We tried to read the txt file with the numbers and to store the numbers in an array. The array should contain 3 8 bit long sequences We also have issues reading the second and third line in the file.

static_cast
  • 1,118
  • 1
  • 15
  • 21
include
  • 11
  • 4
  • 1
    Read [Why is `iostream::eof` inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). Your loop has the same problem. – molbdnilo Jan 21 '19 at 17:42
  • Please be more specific about your problem than that you have "issues". What happens that shouldn't happen, or doesn't happen that should? – molbdnilo Jan 21 '19 at 17:42

1 Answers1

0

Try something more like this instead:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

int main()
{
    std::ifstream file("zahlen.txt");
    if (!file.is_open())
        return -1;

    int array[3][8] = {};
    int num = 0;

    for (int i = 0; i < 3; ++i)
    {
        std::string line;
        if (!std::getline(file, line))
            break;

        std::istringstream iss(line);
        int u;

        for (int j = 0; j < 8; ++j)
        {
            if (!(iss >> u))
                break;

            array[num][j] = u;
        }

        ++num;
    }

    for (int i = 0; i < num; ++i)
    {
        for (int j = 0; j < 8; ++j)
        {
            std::cout << array[i][j];
        }
        std::cout << "\n";
    }

    return 0;
}

Live demo

Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620