Results 1 to 2 of 2

Thread: [C++] Logic Error (std::logic_error)

  1. #1
    Join Date
    May 2008
    Beans
    1,029

    Question [C++] Logic Error (std::logic_error)

    My program compiles fine but a logic error is thrown if I invoke it without any arguments. Otherwise it works fine.

    Code:
    PHP Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;

    #include <cstdlib>

    void ShowUsage(charme)
    {
      
    cout << "\n\tUsage:\t" << (string)me << " <infile> <outfile>\n\n";
    }

    int main(int argcchar** argv)
    {
      
    char me[] = "blah";
      
      if (
    argc 3)
      {
        
    cerr << "\nToo many arguments\n";
        
    ShowUsage(me);
        return 
    1;
      }
      
      if ((string)
    argv[1] == "--help" or (string)argv[1] == "-h")
      {
        
    ShowUsage(me);
        return 
    0;
      }
      
      if (
    argc 3)
      {
        
    cerr << "\nNot enough arguments\n";
        
    ShowUsage(me);
        return 
    1;
      }
      
      return 
    0;

    Output:
    Code:
    $ ./test
    terminate called after throwing an instance of 'std::logic_error'
      what():  basic_string::_S_construct NULL not valid
    Aborted
    I can't seem to see where the problem is.


    ----- SOLVED -----

    Oh! Caught it. It's this line:
    PHP Code:
    if ((string)argv[1] == "--help" or (string)argv[1] == "-h"
    There is no argv[1].


    ----- EDIT -----

    New code:
    PHP Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;

    #include <cstdlib>

    void ShowUsage(charme)
    {
      
    cout << "\n\tUsage:\t" << (string)me << " <infile> <outfile>\n\n";
    }

    int main(int argcchar** argv)
    {
      
    char me[] = "blah";
      
      if (
    argc 3)
      {
        
    cerr << "\nToo many arguments\n";
        
    ShowUsage(me);
        return 
    1;
      }
      
      if (
    argc 3)
      {
        if (
    argc == && ((string)argv[1] == "--help" or (string)argv[1] == "-h"))
        {
          
    ShowUsage(me);
          return 
    0;
        }
        else
        {
          
    cerr << "\nNot enough arguments\n";
          
    ShowUsage(me);
          return 
    1;
        }
      }
      
      return 
    0;

    Last edited by dodle; June 4th, 2012 at 07:54 AM. Reason: Solved

  2. #2
    Join Date
    May 2007
    Beans
    251

    Re: [C++] Logic Error (std::logic_error)

    Quote Originally Posted by dodle View Post
    My program compiles fine but a logic error is thrown if I invoke it without any arguments. Otherwise it works fine.

    Code:
    PHP Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;

    #include <cstdlib>

    void ShowUsage(charme)
    {
      
    cout << "\n\tUsage:\t" << (string)me << " <infile> <outfile>\n\n";
    }

    int main(int argcchar** argv)
    {
      
    char me[] = "blah";
      
      if (
    argc 3)
      {
        
    cerr << "\nToo many arguments\n";
        
    ShowUsage(me);
        return 
    1;
      }
      
      if ((string)
    argv[1] == "--help" or (string)argv[1] == "-h")
      {
        
    ShowUsage(me);
        return 
    0;
      }
      
      if (
    argc 3)
      {
        
    cerr << "\nNot enough arguments\n";
        
    ShowUsage(me);
        return 
    1;
      }
      
      return 
    0;

    Output:
    Code:
    $ ./test
    terminate called after throwing an instance of 'std::logic_error'
      what():  basic_string::_S_construct NULL not valid
    Aborted
    I can't seem to see where the problem is.


    ----- SOLVED -----

    Oh! Caught it. It's this line:
    PHP Code:
    if ((string)argv[1] == "--help" or (string)argv[1] == "-h"
    There is no argv[1].
    More like, argv[1] is NULL at that point and string constructor fails when passed a NULL.
    The Unforgiven

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •