Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7535069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:13:13+00:00 2026-05-30T06:13:13+00:00

It is probably better to say I am doing something wrong rather then say

  • 0

It is probably better to say I am doing something wrong rather then say the comparison isn’t working. But, I have been going over this code for some time now.

I have a recursive function. Most of it is working well, so I will only put the part that isn’t working:

//In main
string C[] = {"S=>bS",
                    "S=>aaT",
                    "T=>aT",
                    "T=>bU",
                    "U=>Ua",
                    "U=>aa"};
CFG CFG1(C);

...

string *code;
char startNT;
//The CFG constructor 
CFG::CFG(string C[])
{
    code = C;
    startNT = code[0][0];
}

...

//Inside of processData recursive function
for(unsigned int i = 0; i < code->size(); i++)
{
    if(code[i][0] == startNT)
    {
        string newStr = code[i].substr(code[i].find(">")+1);
        string old = wkString;
        //This is the recursive call
        if(processData(inString, wkString.replace(wkString.find_first_of(startNT), wkString.find_first_of(startNT)+1, newStr)))
        {
            return true;
        }
        cout << wkString << endl;
        wkString = old;

    }
}

The comparison that isn’t working is code[i][0] == startNT . Well… I should say, isn’t working 100% of the time. It works great until half way through the recursive function, code[i][0] becomes ‘S’ and startNT becomes ‘T’ (after already proving that it can compare ‘S’ and ‘T’ properly somewhere during the call), and it still evaluates to true, which causes the wkString.replace() to break since it can’t find the ‘T’.

It has been awhile since I have used C++, so I am probably making a stupid mistake. Thanks for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T06:13:14+00:00Added an answer on May 30, 2026 at 6:13 am

    code is a pointer to the first string in an array of strings. So when you say code->size(), that’s the size(number of characters) of the first string(5 in the example you gave). I’m pretty sure you’re trying to iterate over the string array, not the characters in the first string. So that’s wrong.

    Unfortunately, since you store a pointer in the class, and not an array, the size of the array is unknown to the class. So you can’t iterate over it properly. You’ll need to restructure your code somehow. Without being able to see it more fully, I cannot make any concrete suggestions.

    What you probably want to do is store a collection of strings in the actual class. By default, I recommend a vector. Your code might look something like this then:

    // in class CFG
    std::vector<std::string> code;
    char startNT;
    
    CFG(const string * C, int N)
        :code(C, C+N),
        startNT(code[0][0]) // strong assumption made here
    {}
    ...
    // processData
    for(unsigned int i = 0; i < code.size(); i++) // note, dot instead of arrow
    {
    ...
    // in main
    std::string C[] = {
        "S=>bS",
        "S=>aaT",
        "T=>aT",
        "T=>bU",
        "U=>Ua",
        "U=>aa"
    };
    
    CFG CFG1(C, sizeof(C)/sizeof(*C));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a silly question, but curiosity has gotten the better of me.
This is probably an easy question, but I want to understand better how Apache
Probably a long question for a simple solution, but here goes... I have a
Probably a bit off topic question, but it's something I'm really interested in getting
This question is not about timing something accurately on Windows (XP or better), but
I'm fairly new to Apache Camel, but have to say that I love it
This is probably simple, but im trying to try and understand pointers better. Lets
This question is probably better phrased: how does running a Java server on a
I've created a salt using; md5(rand(0,10000000)); (there is probably a better way?) There doesn't
I probably couldn't depict it better in the title (and if it has a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.