efficiency problem?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mohaakilla51
    New Member
    • Jul 2007
    • 39

    efficiency problem?

    OK, so I am writing a flashcard script for the command prompt (hence, all of my other questions). I had the script working fine, except for the fact that when I displayed a question (or answer) that was more than 1 line long, then it would break in the middle of the word. So I tried to fix this by dividing the question/answer into lines of 73 chars (or less), and then display it line by line.

    here is what I came up with:
    [code=cpp]
    vector<string> q;
    int qsize = ((question.leng th()/73) + 1);
    switch (qsize)
    {
    case 1:
    q.push_back(app end(question," ", 73));
    break;
    default:
    int temp = 73;
    int temp2 = 0;
    while (temp < (question.lengt h()+73) )
    {
    temp = question.rfind( " ", temp);
    q.push_back(app end(question.su bstr(temp2,temp )," ", 73));
    temp2 = temp+1;
    temp = temp + 73;
    }
    break;
    }
    [/code]
    Append function:
    [code=cpp]
    //Append function: only works well with append strings of length 1.
    string append(string str, string app, int length)
    {
    while(str.lengt h()<length)
    {
    str = str + app;
    }
    return str;
    }
    [/code]
    It compiles fine (under Dev-C++), and will display a question with a length of only one line just fine. However, it will not display any questions that are multiple lines. The program just freezes, and sits there. Can anyone tell me why?
    Last edited by mohaakilla51; Oct 29 '07, 03:12 AM. Reason: forgot the rest of post :O
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    Originally posted by mohaakilla51
    OK, so I am writing a flashcard script for the command prompt (hence, all of my other questions). I had the script working fine, except for the fact that when I displayed a question (or answer) that was more than 1 line long, then it would break in the middle of the word. So I tried to fix this by dividing the question/answer into lines of 73 chars (or less), and then display it line by line.

    here is what I came up with:
    [code=cpp]
    vector<string> q;
    int qsize = ((question.leng th()/73) + 1);
    switch (qsize)
    {
    case 1:
    q.push_back(app end(question," ", 73));
    break;
    default:
    int temp = 73;
    int temp2 = 0;
    while (temp < (question.lengt h()+73) )
    {
    temp = question.rfind( " ", temp);
    q.push_back(app end(question.su bstr(temp2,temp )," ", 73));
    temp2 = temp+1;
    temp = temp + 73;
    }
    break;
    }
    [/code]
    Append function:
    [code=cpp]
    //Append function: only works well with append strings of length 1.
    string append(string str, string app, int length)
    {
    while(str.lengt h()<length)
    {
    str = str + app;
    }
    return str;
    }
    [/code]
    It compiles fine (under Dev-C++), and will display a question with a length of only one line just fine. However, it will not display any questions that are multiple lines. The program just freezes, and sits there. Can anyone tell me why?
    I'll comment on the endless looping...
    Your getting stuck in the while loop because rfind keeps giving you the same position (when your looking at the last 73 characters).

    Here's an example: assume the line your breaking apart is 90 characters long with spaces at say, 70 and 80 . The first time through you break out the first 70 characters. Then the next time through rfind tells you about the space at position 80, so you break out that stuff. The while condition evaluates to
    Code:
    while( (80 + 73) < (90 + 73) )
    so you go through again (and again), but things don't change - rfind tells you about the space at position 80 again

    I won't suggest a solution because I'm sure you can figure that out on your own

    Comment

    • mohaakilla51
      New Member
      • Jul 2007
      • 39

      #3
      THANK YOU SO MUCH!!! I went through My logic soooooo many times but I never found the problem!!

      Comment

      • mohaakilla51
        New Member
        • Jul 2007
        • 39

        #4
        OK, so I was able to fix it, or so I thought, the problem is now that it goes back into the infinite loop if a word wraps around on to two lines normally, such as a word that goes from char 70, to char 80. Here is theh code, any help would be greatly appreciated.
        [code=cpp]
        vector<string> q;
        int qsize = ((question.leng th()/73) + 1);
        switch (qsize)
        {
        case 1:
        q.push_back(app end(question," ", 73));
        break;
        default:
        int temp = 73;
        int temp2 = 0;
        while (temp < (question.lengt h()+73) )
        {
        if(question.rfi nd(" ",temp) == temp)
        temp = question.length () + 73;
        else
        temp = question.rfind( " ", temp);
        q.push_back(app end(question.su bstr(temp2,temp )," ", 73));
        temp2 = temp+1;
        temp = temp + 73;
        }
        break;
        }
        [/code]

        Comment

        • mac11
          Contributor
          • Apr 2007
          • 256

          #5
          you know how many lines you will have (computed and stored in qsize) and therefore how many times you need to loop - so use qsize to control your loop

          Comment

          • mohaakilla51
            New Member
            • Jul 2007
            • 39

            #6
            that will not work because, say I have a string that is 200 chars long. 200/73 = approx(2.73) + 1 thus qsize is 3 (once typecasted). However, say the string has a space at char 50, and then not again until 74. OK, so that gives you a first line of length 50. Then you go, and the last space in the next line is at char 120. thus, you get a line of 50, and a line of 70. 50 + 70 = 120. It would only loop through 1 more time, and would not get the rest of the string, because 200 - 120 = 80. You see the problem with using the loop from qsize? however, I figured out a different method to do, and it works just fine.

            Comment

            Working...