-1

So,this is a code for reading matrix A and B from txt files. The following code that i found was able to read both files. However, the output for the matrix A is overlapped with the Matrix B (picture below) enter image description here

Matrix A -----
1       2       3
4       5       6
7       8       99 

Matrix B -----
0       1       1
2       0       0
2       0       00 

So, how to avoid it overlapping, please help :)

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

int main()
{

    {   
        char ch;
        const char *fileName="MatrixA.txt";     // FOR MATRIX A

        ifstream file;

            file.open(fileName,ios::in);
                if(!file)
                {
                    cout<<"Error in opening file!!!"<<endl;
                    return -1; 
                }


                while (!file.eof()) 
                {
                    file >> noskipws >> ch; 
                    cout << ch; 
                }

        file.close();
    }

    {
        char ch;
        const char *fileName="MatrixB.txt";     // FOR MATRIX A

        ifstream file;

            file.open(fileName,ios::in);
                if(!file)
                {
                    cout<<"Error in opening file!!!"<<endl;
                    return -1; 
                }


                while (!file.eof()) 
                {
                    file >> noskipws >> ch; 
                    cout << ch; 
                }

        file.close();
    }


    return 0;
}

EDIT: Thanks everyone! Fixed it and yes i know that this is not a code for reading matrices (sorry for the misinformation). i just want it to look like one hehe so thanks again

  • 2
    Use `std::cout << std::endl` between both ? – Jarod42 May 09 '18 at 12:58
  • 2
    Compile with all warnings and debug info, so `g++ -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/). Improve your code to get no warnings. **Use the debugger**, [e.g. `gdb`](https://sourceware.org/gdb/current/onlinedocs/gdb/) to understand the behavior of your program. Improve and repeat till satisfied – Basile Starynkevitch May 09 '18 at 12:58
  • Btw, you don't read matrix, but just file content. – Jarod42 May 09 '18 at 12:58
  • 1
    Don't copy paste code (you don't fix comment of copied code), create function instead. – Jarod42 May 09 '18 at 13:00
  • 1
    @Jarod42 std::endl is not equal to newline, as it flushes – Gladaed May 09 '18 at 13:01
  • 2
    @Gladaed: which is "better" for beginners (previous (debug) output is printed before crash, which help to locate the bug). – Jarod42 May 09 '18 at 13:04
  • 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) – molbdnilo May 09 '18 at 13:46

1 Answers1

3

You've just printed a character-by-character copy of the two files, with nothing between them. Apparently "MatrixA.txt" does not have a newline at the end of the file.

You can just add a '\n' character after the output of the first file.

std::cout << '\n';

In a very real sense you have not read in two matrices, as you don't utilise any arithmetic values present in the files. If you want to do that, you will first have to come up with some representation of a Matrix in your program, and only then can you think about reading it from your files.

Caleth
  • 35,377
  • 2
  • 31
  • 53