Can you tell me how to resolve my problem.

i can reserve the matrix A and B into file, but how to read , i can't remember...above, shows what i pretend to do..

# include <iostream>
# include <fstream>

using namespace std;

int main()
{
 int matriz[3][3];
 int x,i,j;//variables for lines:.
 int y,z,l;//variables for columms:.
 int opt;
 ofstream write;
 ifstream read;

 cout<<"Select one opction"<<endl;
 cout<<"1.:Insert matriz A"<<endl;
 cout<<"2.:Insert matriz B"<<endl;
 cout<<"3.:To add"<<endl;
 cout<<"4.:To decuct"<<endl;
 cout<<"5.:Exit"<<endl;
 cin>>opt;





  switch(opt)
  {
   case 1:
       for(x=0; x<=1; x++)
        {
         for(y=0; y<=1; y++)
         {
          cin>>matriz[x][y];
         }
        }
        write.open("Resultado das Matrizes.txt",ios::app);
        if(write.is_open())
        {

         write<<matriz[x][y];
         write.close();
         }
         else
         {
         cout<<"the matrix(A) it's empty!";
         }
         break;
    case 2:
         for(i=0; i<=1; i++)
         {
          for(z=0; z<=1; z++)
          {
           cin>>matriz[i][z];
          }
         }
         escrever.open("Resultado das Matrizes.txt",ios::app);
         if(write.is_open())
         {
          write<<matriz[i][z];
          write.close();
         }
         else
         {
          cout<<"the matrix (B) it's empty!";
         }
         break;
    case 3:
           read.open("Resultado das Matrizes.txt",ios::out);
           if(read.is_open())
           {
            read>>matriz[x][z]<<endl<<matriz[i][z];


               read.close();
              }

             else cout<<"a matriz está vazia";

    case 4:
    }



 return 0;
}

Can you tell me?????

To read the matrix.

Open the file.

while(!infile.eof())
{
string x;
infile>>x;
dosomethingwith(x);
}
commented: Find the bug. -4

Thanks it works!!

Thanks it works!!

> From the comments skydiploma got on his post I read the following: "Find the bug. - Dave Sinkula"

>> Now to the OP: Welcome to the world of C++: it isn't because it seems to work that it actually works :)

> You could also adapt the technique described in this post :)

> The eof() function will tell you whether an attempt has been made to read past the end of available data, not whether the next read will fail due to lack of data.

Never write code that looks like: while (!infile.eof()) { read_some_more(); } > I hope this is explaining why skydiploma's code was bad ...

edit> Take a look here for some more information on the eof() function :) ...

Well i still donot understand. As I have not gone into filestreams yet. I have seen codes which use the same type of while loop and have come to a conclusion that this is the way to follow.

I still donot understand the reason of not to use eof(); Please Explain a lil more :)

I still donot understand the reason of not to use eof(); Please Explain a lil more :)

I didn't say you aren't allowed to use it anymore, you just have to make sure that you first read something from a file before using the eof() function, it might be advisable that you put all the instructions which are reading/writing from/to a file into a try-block so exceptions will be caught :) ...

If you really want to use a loop do something like this:

string x;
[B]infile>>x;[/B]
while(!infile.eof())
{
     infile>>x;
     /* your other instructions here */
}

or use a do-while statement:

string x;
do
{
     infile>>x;
     /* your other instructions here */
} while(!infile.eof());

or

string x;
while(infile>>x;)
{
     /* your instructions here */
}

i have another situation, with this program, i put it on run, but when the program reaches the case 3 he shutdown.. Why?

read.open("Resultado das Matrizes.txt",ios::out);

I am not an expert.
Maybe because.
You have declared it as an output file with ios::out
And you are reading from it .

If you really want to use a loop do something like this:

Your first and second examples are just as broken as most other loops using .eof() . But if you combine the two:

string x;
infile>>x;
do
{
    /* your other instructions here */
    infile>>x;
} while (!infile.eof() );

Reading needs to be done immediately before the test with .eof() . Try this explanation to see if you understand it better. ( feof() is the same as .eof() )

But, I checked it in a C++ Book(*) before I posted these examples ...

(*) C++ Beginner's Guide by Herb Schildt, a free legal ebook copy is available here, so you can check the do-while if you want (Chapter 11, p31-p33) ...

Reading needs to be done immediately before the test with .eof() .

But if you use a do-while statement, the eof() check is always performed after the reading, so what's wrong with it ?

But, I checked it in a C++ Book(*) before I posted these examples ...

(*) C++ Beginner's Guide by Herb Schildt, a free legal ebook copy is available here, so you can check the do-while if you want (Chapter 11, p31-p33) ...

So ol' Herbie is wrong, legal book or not. Publishing bad information has been know to happen -- a lot.

But if you use a do-while statement, the eof() check is always performed after the reading, so what's wrong with it ?

Yes, but you are processing bogus information before the eof() check . Did you look at the link I posted? Write your do-while loop and read the file described in that link. If it works, post it. If it doesn't, :icon_wink:

commented: Now I'm sure, It was confusing me a lot that in book x they're explaining this and on site y they just say that it isn't possible, but after your excellent posts I finally understand it :) !! +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.