As you will soon note, I am such a noob to this C++ thing...
I am creating (or at least trying) a calculator that allows you to make operation with mathematic vectors, as a way of getting some practice. I have recently come up with this error message when debuging: "An Access voilation was raised in your program". When running the program without debuging the error messaje displayed is the following: "The instruction in 0x0004015a6 refers to the memory in 0xfffffffe. The memory could not be written." (As I roughly translate from Spanish). The complier Im using is wxDev-C++.
Here I paste the code of the program, which is quite short and has explaining comments:
#include <cstdlib>
#include <iostream>
usingnamespace std;
int main(int argc, char *argv[])
{
char p[10];
cout<<"Vectors Calculator"<<endl<<endl; //introduction to the
cout<<"Developed by X"<<endl; //program
GOBACK:
cout<<"Do you wish to start? (y/n)"<<endl; //dumb question
cin>>p;
while ((*p!='y' && *p!='n')||p[1]!=00){ //will print "ERROR"
cout<<"ERROR"<<endl; //if you type any weird
cin>>p; //stuff different from 'y'
} //or "n".
if (*p=='y')
cout<<"Introduce the dimension of the vectors"<<endl; //Further instructions
elseif (*p=='n'){
char ex [10]; //Will let you leave the program
cout<<"Enter 'e' to exit"<<endl; //if you are not ready to start...
cin>>ex; //bleh...
if (*ex=='e'){
return EXIT_SUCCESS;
}
elseif (*ex!='e'||ex[1]!=00){ //If you dont want to leave...
goto GOBACK;
}
}
int R; //R woudl be the "dimension"
cin>>R; //which you will now enter.
longdouble **vectorStore; //Creates a matrix where vectors you
for(int i=0;i<100;i++){ //introduce will be stored.
vectorStore[i]= newlongdouble [R];//<-------------------
} //This loop creates
cout<<"Now enter the vectors elements one by one"<<endl; // 100 vectors of the decired size.
cout<<"Vectors will be printed for control."<<endl;
for (int i=0; i<100; i++){ //This loop lets you introduce
vectorStore [i]; //vecotors elements.
for (int j=0; j<R; j++){
cin>>vectorStore [i] [j];
}
cout<<"V"<<(i+1)<<"=[ ";
for (int j=0; j<R; j++){ //This loop prints each vector
cout<<vectorStore [i] [j]<<" "; //you introduce.
}
cout<<"]"<<endl;
cout<<"Do you wish to introduce another vector?(y/n)"<<endl;
char p[10];
cin>>p;
while ((*p!='y' && *p!='n')||p[1]!=00){ //Same as the first "while"
cout<<"ERROR"<<endl; //but for the vector adding.
cin>>p;
}
if (*p=='y'){ //Go on with the loop...
cout<<"Continue..."<<endl;
}
elseif (*p=='n'){
break; //Break the loop...
}
}
cout<<"Now you can:"<<endl;
cout<<"-Print all vectors.(p)"<<endl;
cout<<"-Operation with vectors.(o)"<<endl; //More work needs to be done.
system ("PAUSE");
return 0;
}
When I choose to break into the process (while debuging), it takes me to this line:
vectorStore[i]= new long double [R];//<-------------------
Which leaves me clueless, because I had been running this exact code, added some stuff, got the error for the first time, then went back to the good version and now I cant get this error off no matter what I do.
I havent used actual vector because my level of noobness doesnt allow it. Ill explain: I have roughly read a basic tutorial, one like the one this site provides. My intention is to keep reading, but I just wanted to give it a try, when I came up to this... a real P.I.A. I believe that using actual vectors may be more convenient, but am I doing something wrong here? or is just the complier and/or windows vista feels like making me misserable?
Your issue is that you are trying to access the ith element of your vectorStore, but you haven't initialized the first row yet. You need something like:
vectorStore = newlongdouble*[100];
Although I would really suggest you use vectors instead so you don't have to deal with memory management.
After you get around all the template nonsense, vectors are actually quite easy to use. Here...
1 2 3 4 5 6
vector<type> naem; //Declaring a vector.
//You can use vectors as type, but be sure to include a space to differentiate <vector<type> > and <vector<type>>.
naem.at(location); //Getting stuff from a vector.
//Where location is an address just like the one you would use in an array.
naem.push_back(new_stuff); //Appending stuff to a vector.
//Where new_stuff has type... type.