C++ Not Working At All

Asked By 20 points N/A Posted on -
qa-featured

Hi! I am having error on my C++ code. Can someone please help me fix it? The access violation error has occurred on the following C++ code:

char num2[80]

strcpy(num2,argv[2];

I need to copy the argv[2] to a new array but it always has errors. Please help me in resolving this issue. Thank you very much.

SHARE
Best Answer by Berkova Gerald
Best Answer
Best Answer
Answered By 0 points N/A #106331

C++ Not Working At All

qa-featured

Hi Adrian!

The thing is, in C++ or C programming you can pass these two arguments to the main function by command line:

1)      int argc

2)      char *argv[]

for example:

int main (int argc, char *argv[])

{

 // your program

Return 0;

}

These two arguments are “argument count” and “argument vector”.  Argument vector, here, is an single dimensional array of strings. These strings are those arguments or strings that you’ve passed from command line (e.g. you write “gcc –o program prog.c” in command line while using gcc compiler there argc will be 4 and argv[0]=”gcc”, argv[1]=”-o” and so on ). 

What happening in your case is that probably you are not passing a string (argument) at all from command line or you have passed only two arguments and trying to copy empty memory location on 3rd location of argv[] i.e. argv[2]. So please check that you are sending an argument at all to your main. In your main function before “strcpy()”, write :

if(argc==2)

{

// paste your strcpy here

}

else

{

Cout<<”You are accessing a memory location where you did’nt pass anything.”<<endl;

}

I hope this will help.

Answered By 0 points N/A #106332

C++ Not Working At All

qa-featured

In the first line of the code you are declaring an array of 80 characters and in second you are just using 2 items as an array list that is a major programming error. The array list must be same in this case or the data will either be lost or it will cause a programming error.

Smith Thompson

Login/Register to Answer

Related Questions