hello
I want to write a 2d array to a text file just for testing.
when my loop is not larger than 4 loops it works but when it is larger,this error occurs:"test.exe has encountered a problem and needs to close. We are sorry for the inconvenience".
my compiler is "devC++".
thank you for your help.
//definition of variables
#include <iostream>
#include <stdio.h>
#include <fstream>
usingnamespace std;
int main ()
{
double C [10][10];
int i = 1;
int j = 1;
ofstream myfile;
myfile.open ("Cexp.txt");
for (j =1;j<6;j++){
for (i=1;i<6;i++){
C[i][j]=1;
myfile << C[i][j] << endl;
}
myfile.close();
return(0);
}
#include <iostream>
#include <fstream>
usingnamespace std;
int main ()
{
double[10][10];
int i = 1;
int j = 1;
ofstream myfile;
myfile.open ("Cexp.txt");
for (j =1;j<6;j++)
{
for (i=1;i<6;i++)
{
C[i][j]=(j * 10) + i;
myfile << C[i][j] << " ";
}
myfile << endl;
}
myfile.close();
return(0);
}
Line 2: #include <stdio.h> was removed -- you don't need it, that's C. Stick to C++.
Lines 6 & 23; 13 & 20; 15 & 18: notice the consistent and easy to read brace style. It does not matter what style you choose to use. What matters is that you choose one and stick to it throughout your code.
Your original code combined lines 18 and 20 into a single brace. (Hence, the compiler thought that your original line 20 belonged to the for loop on your original line 13.) I don't know how Dev-C++ compiled the program though... it should have not compiled.
Line 16 & 19: Just a couple of simple additions to see the matrix at work... the number assigned is a simple row(tens)-column(ones) value, and each row is printed all on the same line. This is, of course, just for playing around with it. If you want an identity matrix, assign all the values zero except when i==j assign the value one.
Thanks alot for your help.
your program worked correctly. but I still don't understand my fault.
notice: That missing brace existed in my program after line 17 and i only missed it when i coped it to this forum.
although Finally I think I found my problem there are some problems.
When I changed my braces style and put them one line after the for line my problem solved.
But why c++ didn't give me error and close the program instead?
When C++ give error for mistakes and when doesn't give?
I do not talk about compile errors but run errors.
I have another bigger program and it has same problem.I change all of my braces style but the error still showing.
how do I can get rid of this error?
C++ gives you a language you can somehow communicate with computers via the translator, the compiler. After your compiler translates for you, your computer tries to run a piece of code. Afterwards you have to be prepared: it crashes; informs the user and exit; informs the user and asks to try again...
The compiler hardly tells you about run-time errors, which are commonly due to wrong logic algorithms and syntactic problems, and thus hard to spot when one goes through the codes and may become nasty bugs in the end. The compiler may or may not give you warnings, then if you are lucky, the program crashes, otherwise you get a wrong result unnoticed!
Try to debug. But for simple and short codes, you may let your program cout something, or system("pause"); it at certain lines (oh, be a cat, try and catch). Of course, the correct solution is to think carefully when writing your codes - make sure you type every } gracefully without regrets. Consider writing } whenever your { appears, so you don't get confused by huge blocks or nests.
Be cool to use newlines and tabs and spaces, making your codes clearer ;)