I was writing a program on method of linear regression... everything was going fine until i tried to compile and run it..
it now seemed to compile 'successfully' superfast... and when it attempted to run, the black .exe window would come up empty. soon after, the XP "unknown error has occured" message came up..
i tried going back and putting the code back where it was.. didnt help.
i also tried to compile and run a hello world program. it worked... but hello world wont get me a grade in class!
i'm somewhat of a noob.
here is the code:
include<iostream>
include<iomanip>
include<cmath>
using namespace std;
int main()
{
int n;
float a=0;
float b=0;
float c=0;
float det;
> everything was going fine until i tried to compile and run it..
Which is really everything, so nothing went well apart from your typing perhaps! ;)
It is not unusual for code to contain bugs and so fail on first time execution. I would be amazed if it did work first time, but you seem genuinely surprised. Get used to it. The first course of action should be to work on debugging your code, and that does not necessarily involve posting a messge to a forum at the first sign of trouble. That's the slow way.
I built your code in VC++ 2008 and it does not actually compile. The reason is these:
float Xarray [n];
float Yarray [n];
Now you can do that in GNU C++ because it supports variable length arrays, however that is unusual. It is allowed uncer ISO C99 which few compilers support, it is not currently part of standard C++ so you should probably avoid it.
However it does point to the rather obvious problem in your code. The variable n is unitialised, so it is anyone's guess how large those two arrays are. They are likely either very short and your code is overrunning the buffer, or very large and you have exceeded the process stack limit.
You could move the array declarations to after the value for n is entered, and hope that the user enters a sensible value and does not blow your stack. This stills relies on the variable array support extension, so I would not recommend it.
You could set them to some reasonable maximum limit, and coerce n to that limit if the user enters a higher value or reject the user input and ask again.
You could dynamically allocate the arrays thus:
cout<<"How many data values?"<<endl;
cin>>n;
float* Xarray = new float[n];
float* Yarray = new float[n];
and then when done with the arrays:
delete [] Xarray ;
delete [] Yarray ;
Since you are expecting your users to enter the data manually, I suspect that the maximum number of values will be low, so the fixed array size and range check approach would be reasonable.
I modified the code using dynamic memory allocation and it runs fine. Note that the way cin works, any whitespece is a delimiter, so you don't need to press <enter> between each value, you can equally enter a space separated list.
Your user input has no error checking, it is easy to mess up the program or even make it crash by entering something other than the expected input. This may or may not be important in this assignment, but should be considered in real applications.
From a usability point of view, I would expect to enter the coordinates in x,y pairs, not all x's followed by all y's. People do not normally think like that, an their data is not usually provided like that. For example (omitting any error checking), replacing your two input loops with:
Which is both simpler code and more user friendly. Note it does not matter whether <enter> or <space> (or even <tab>!) is used in the input. I could have entered all values on one line, or on six separate lines.
Other issues:
Do not place projects in "My Documents", or "Desktop". The user profile path "C:\Documents and Settings\Philip Jr\" contains spaces, and this can cause problems with the toolchain.
For the same reason, you should not have installed Dev-C++ in "Program Files". Let it install in its default location to avoid problems.
Also your log suggests that you may be using an old version of Dev-C++. You did not state the version. Currently you should use 4.9.9.2. Follow the instructions in the "PLEASE READ BEFORE POSTING A QUESTION" thread to the letter for uninstalling and reinstalling. That thread also mentions the spaces in paths issue and thE request to include the version number in posts.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This is so frustrating!
I was writing a program on method of linear regression... everything was going fine until i tried to compile and run it..
it now seemed to compile 'successfully' superfast... and when it attempted to run, the black .exe window would come up empty. soon after, the XP "unknown error has occured" message came up..
i tried going back and putting the code back where it was.. didnt help.
i also tried to compile and run a hello world program. it worked... but hello world wont get me a grade in class!
i'm somewhat of a noob.
here is the code:
include<iostream>
include<iomanip>
include<cmath>
using namespace std;
int main()
{
int n;
float a=0;
float b=0;
float c=0;
float det;
}
and here is the compile log:
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Documents and Settings\Philip Jr\Desktop\LinearRegression.cpp" -o "C:\Documents and Settings\Philip Jr\Desktop\LinearRegression.exe" -g3 -I"C:\Program Files\Dev-Cpp\include\c++" -I"C:\Program Files\Dev-Cpp\include\c++\mingw32" -I"C:\Program Files\Dev-Cpp\include\c++\backward" -I"C:\Program Files\Dev-Cpp\include" -L"C:\Program Files\Dev-Cpp\lib"
Execution terminated
Compilation successful
What should I do?
I need help fast!
Wow!
Thank you so much.
Super helpful! Great tips.
Such a fast response too!
Thanks again
> everything was going fine until i tried to compile and run it..
Which is really everything, so nothing went well apart from your typing perhaps! ;)
It is not unusual for code to contain bugs and so fail on first time execution. I would be amazed if it did work first time, but you seem genuinely surprised. Get used to it. The first course of action should be to work on debugging your code, and that does not necessarily involve posting a messge to a forum at the first sign of trouble. That's the slow way.
I built your code in VC++ 2008 and it does not actually compile. The reason is these:
Now you can do that in GNU C++ because it supports variable length arrays, however that is unusual. It is allowed uncer ISO C99 which few compilers support, it is not currently part of standard C++ so you should probably avoid it.
However it does point to the rather obvious problem in your code. The variable n is unitialised, so it is anyone's guess how large those two arrays are. They are likely either very short and your code is overrunning the buffer, or very large and you have exceeded the process stack limit.
You could move the array declarations to after the value for n is entered, and hope that the user enters a sensible value and does not blow your stack. This stills relies on the variable array support extension, so I would not recommend it.
You could set them to some reasonable maximum limit, and coerce n to that limit if the user enters a higher value or reject the user input and ask again.
You could dynamically allocate the arrays thus:
and then when done with the arrays:
Since you are expecting your users to enter the data manually, I suspect that the maximum number of values will be low, so the fixed array size and range check approach would be reasonable.
I modified the code using dynamic memory allocation and it runs fine. Note that the way cin works, any whitespece is a delimiter, so you don't need to press <enter> between each value, you can equally enter a space separated list.
Your user input has no error checking, it is easy to mess up the program or even make it crash by entering something other than the expected input. This may or may not be important in this assignment, but should be considered in real applications.
From a usability point of view, I would expect to enter the coordinates in x,y pairs, not all x's followed by all y's. People do not normally think like that, an their data is not usually provided like that. For example (omitting any error checking), replacing your two input loops with:
allows the data to be entered thus:
Enter 3 X,Y value pairs:
1 1
2 2
3 3
Which is both simpler code and more user friendly. Note it does not matter whether <enter> or <space> (or even <tab>!) is used in the input. I could have entered all values on one line, or on six separate lines.
Other issues:
Do not place projects in "My Documents", or "Desktop". The user profile path "C:\Documents and Settings\Philip Jr\" contains spaces, and this can cause problems with the toolchain.
For the same reason, you should not have installed Dev-C++ in "Program Files". Let it install in its default location to avoid problems.
Also your log suggests that you may be using an old version of Dev-C++. You did not state the version. Currently you should use 4.9.9.2. Follow the instructions in the "PLEASE READ BEFORE POSTING A QUESTION" thread to the letter for uninstalling and reinstalling. That thread also mentions the spaces in paths issue and thE request to include the version number in posts.
Clifford