Thread: Problem with strings

  1. #1
    Registered User
    Join Date
    Mar 2006

    Problem with strings

    Hey guys, you were a huge help on my last problem, but here I am back again with another :\

    I just need someone to help me debug my program, I've done everything I could think of to fix it, but no luck. Here's the snippet where the problem is occuring (well where the program breaks, I hope the problem isn't elsewhere)

        string alg, algtemp;
        cout << endl << "Enter expression: ";
        cin >> algtemp;
        cout << algtemp << " =";
        while (algtemp.find_first_of(')') != string::npos)
          if (algtemp[0] != ' ' && algtemp[0] != '\t')
            alg[j] = algtemp[0];
          algtemp = algtemp.substr(1, string::npos);
        pos1 = alg.find_first_of(')');
        str1 = alg.substr(0, pos1);
        operat = alg[pos1+1];
        alg = alg.substr(pos1+2, string::npos);
        alg = algtemp;
        pos2 = alg.find_first_of(')');
        str2 = alg.substr(1, pos2);
    The program works fine until where I have the system("PAUSE"), and then I don't get an error message or anything, the program just closes. I thought I may have been trying to access a part of the string that wasn't there, but I've tried limits that shouldn't have been close to the endpoints of the string and it still didn't work.

    Oh for some backgrounds: the input I'm typing in is (2x^3)+(4x^2)

    I'm trying to seperate these two polynomials into seperate polynomials in their own seperate strings, the loop there is just to make sure there's no spaces or tabs in the input.

    Any help would be appreciated,


  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    1) don't use system("pause"); use std::cin.get(); and a corresponding output statement
    2) substr defaults to npos as it's second argument... leave that out.
    Join is in our Unofficial Cprog IRC channel
    Channel: #Tech

    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Jan 2005
    >> alg[j] = algtemp[0];

    At that point alg has a size of zero. The operator[] is used to access existing characters in a string, so you are attempting to access memory that doesn't belong to the string. In this case you should probably get rid of j and just use += to add the algtemp[0] character to alg.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare strings problem
    By chelito19 in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 08:01 PM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 10:44 PM
  3. Problem with Strings and Conversions
    By patso in forum C Programming
    Replies: 8
    Last Post: 04-09-2008, 12:01 PM
  4. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  5. copy strings problem
    By dgcampos in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2004, 08:05 PM