Thread: Getting error in Visual C++

  1. #1
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154

    Getting error in Visual C++

    I usually use Dev-C++. I've tried this code in Dev-c++, and it works, but I then tried it in Visual C++ and all I got was a program that crashed when it ran. Here is the code.
    Code:
    using namespace std;
    int main(int argc, char* argv[])
    {
    cout << argv[1];
    cout << "" << endl;
    return 0;
    }


  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    15
    why even have the output of an empty string? Just put
    Code:
    cout <<argv[1]<<endl;
    As to reasons why it would crash, are you passing the correct variables to the program? Wouldn't it be an invalid pointer if you attmpted to access something that wasn't there (in the case no arguments are passed)?
    -Sean

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Finchie_88
    I usually use Dev-C++. I've tried this code in Dev-c++, and it works, but I then tried it in Visual C++ and all I got was a program that crashed when it ran. Here is the code.
    [
    Did you give any command-line arguments?

    Maybe you could try something like this to see what's happening:

    Code:
    int main(int argc, char* argv[])
    {
      cout << "argc = " << argc << endl;
      if (argc > 1) {
        cout << "argv[1] : " << argv[1] << endl;
      }
      return 0;
    }
    In any case, you shouldn't use argv[x] unless you know that argv[x] is defined.

    Programs should never be written in a way that invalid user input can cause a crash.

    Regards,

    Dave
    Last edited by Dave Evans; 03-18-2005 at 10:58 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM