Crash on string compare

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MrHenry
    New Member
    • Mar 2009
    • 2

    #1

    Crash on string compare

    Hello, i'm having a problem where my program is crashing everytime it reachs a certain point. I get the error "test001.ex e has encountered a problem and needs to close. we are sorry for the inconvenience".

    Now, i've put cout << "display"; on every line to see where the crash occurs, and it happens with this statement
    "if (strcmp(line.c_ str(), "Importance : High")==0 )",
    i've tried replacing it with "if (line == "Importance : High"){", yet it still crashes.

    Im using visual studio 2008, but when compiled on my friends dev-c++ the code works fine. I can't switch IDE though. Im not sure what is going on here.

    If I press debug I get the message "An unhandled win32 exception occurred in test001.exe [4612]. Then when I open the debugger I get "unhandled exception at 0x1049191c (msvcp90d.dll) in test001.exe:0xC 0000005:Access violation reading location 0x00000018.

    It then goes to the xstring file and goes to this line:
    size_type __CLR_OR_THIS_C ALL length() const
    { // return length of sequence
    return ( Mysoze);
    }

    the following screenshot shows more detail:
    Host your images online with an easy to use image uploader, albums, photo hosting, sharing, dynamic image resizing on web and mobile.


    Code------
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <string>

    using namespace std;

    string checkpriority (string filenameandpath );

    int main( )
    {

    string passtocheckprio rityfunction;
    string priorityresult;

    cout << "Enter the file path and name: ";
    getline( cin, passtocheckprio rityfunction );

    priorityresult = checkpriority(p asstocheckprior ityfunction);

    ofstream myoutputfile( "result.txt " );
    myoutputfile << priorityresult;
    myoutputfile.cl ose();


    return 0;
    }



    string checkpriority (string filenameandpath ) { // my function

    string line;

    cout << "Opening file..." << std::endl;

    cout << filenameandpath << std::endl;

    fstream myinputfile;

    myinputfile.ope n( filenameandpath .c_str(), ios::in | ios::out );

    if (myinputfile.is _open())
    {

    while ( !(myinputfile.e of()) )
    {
    getline(myinput file,line);

    if (strcmp(line.c_ str(), "Importance : High")==0 )
    {
    cout << "Found match." << std::endl;

    return "Importance : 1.\n";
    }
    }

    myinputfile.clo se();
    }
    }
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    Really strange: according to your watch window, 'this' pointer during call to some member of 'line' is null. Did you try to debug it?

    Comment

    • ricardopf
      New Member
      • Mar 2009
      • 5

      #3
      Try someting like this:

      if (strcasecmp(lin e, "Importance : High") == 0) {
      }

      Comment

      Working...