Thread: .exe has stopped working

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    4

    Question .exe has stopped working

    Each time I use

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

    to try to get input from command line, there is a crash of the and it says:
    "xxx.exe has stopped working". What's the problem?

    I am trying to run the example code:


    Code:
    #include <iostream>
    #include <stdlib.h>
    #include "POINT.cpp"
    using namespace std;
    
    int main(int argc, char *argv[])
     { float d = atof(argv[2]);
       int i, cnt = 0, N = atoi(argv[1]);
       POINT *a = new POINT[N];
       for (i = 0; i < N; i++)
         for (int j = i+1; j < N; j++)
           if (a[i].distance(a[j]) < d) cnt++;
       cout << cnt << " pairs within " << d << endl;
       return 0;
     }
    
    
    
    #include <math.h>
    class POINT
      {
        private:
          float x, y;
        public:
          POINT()
            { x = 1.0*rand()/RAND_MAX;
              y = 1.0*rand()/RAND_MAX; }
          float distance(POINT a)
            { float dx = x-a.x, dy = y-a.y;
              return sqrt(dx*dx + dy*dy); }
       };
    I am new for C++, and desperate to fix the problem. Can anyone help me out?

    Thanks very much!

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Why have you written a header include halfway down the page in something like this? Its not even an ifdef, or is that supposed to show the contents of the other source file? if not what is happening in POINT source file? why is it an inlcude instead of just added to the project? It could be buffer overflow, out of bounds, anything, you need to run it in the debugger, you will probably immediately find the line it crashes on...with a seg fault perhaps.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    4

    Unhappy

    Thanks for your reply!

    The lower part is the source code for "POINT.cpp", sorry for the confusion. I tried putting these two files into a project(One still needs to include "POINT.cpp" in the main file, right? Otherwise, a lot of things will not be declared.). There is still the problem.

    This may be a stupid question, but I can't figure it out.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    When using debugger:
    I recevied the following message:

    Program received signal SIGSEGV, Segmentation fault.
    In msvcrt!_atof_l () (C:\Windows\system32\msvcrt.dll)
    At D:\Documents\C++\Algorithms in C++\cloest-point.cpp:7

    What does it mean? Is there anything wrong with line 7 in the main file, which is

    Code:
    { float d = atof(argv[2]);
    I appreciate any help!

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    It probably means one of two things. One are you sure that argv[2] even exists? You should check that the number of arguments are correct. Two are you sure that argv[2] starts with a number and not a character?

    What are your command line arguments?


    Jim

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by bluesky16 View Post
    Is there anything wrong with line 7 in the main file, which is

    Code:
    { float d = atof(argv[2]);
    Yes there is.
    you haven't checked that argc > 2 before attempting to access element [2].
    On the next line you have about the same problem. You're not checking that argc > 1 before attempting to access element [1].
    You are also using the horible coding style of having the opening brace on the same line, which I consider about as undesireable as the crashing.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Why are you including a .cpp file??? Bad, bad, bad!!!

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    Quote Originally Posted by jimblumberg View Post
    It probably means one of two things. One are you sure that argv[2] even exists? You should check that the number of arguments are correct. Two are you sure that argv[2] starts with a number and not a character?

    What are your command line arguments?


    Jim
    I am not even allowed to input anything in the command line before the crash. It comes out with a black command window(.exe) and a microsoft window saying that ".exe has stopped working". It happens with every file with
    Code:
    int main(int argc, char *argv[])
    BTW, I copied the code from a book about C++.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You add the command line arguments to the command line like: "myprogram test 100.0" see this link:Command line arguments.


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy an .exe to a new .exe file using C/C++
    By Avenger625 in forum C++ Programming
    Replies: 6
    Last Post: 02-07-2011, 12:49 AM
  2. why .exe file is not working in windows of c program
    By suryak in forum C Programming
    Replies: 9
    Last Post: 01-07-2011, 12:23 PM
  3. Working Directory and Links
    By scwizzo in forum Windows Programming
    Replies: 5
    Last Post: 08-07-2010, 04:51 PM
  4. Help explaining test questions
    By Sentral in forum General Discussions
    Replies: 26
    Last Post: 11-09-2009, 11:10 PM
  5. Mouse scroll stopped working
    By Waldo2k2 in forum Tech Board
    Replies: 2
    Last Post: 10-12-2004, 11:25 AM

Tags for this Thread