updating a string from aaaa to dddd
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: updating a string from aaaa to dddd

  1. #1
    Join Date
    Jan 2012
    Posts
    2

    updating a string from aaaa to dddd

    Hello I am writing some code that takes a string of n letters, so a, or aa or aaa or aaaa...a and transforms these into d or dd or ddd or dddd...d by essentially counting. e.g aaaa -> aaab -> aaac -> aaad -> aaba and so on. Also technically the user inputs what the ending is whether it be d or e,f,g; however, for practical reasons counting up to dddd should work. My main issue is I am having trouble getting the letters correct. I get up to aadd and then once I go to abaa I have trouble getting the second to last a to switch correctly.

    Code:
    #include<iostream>
    #include<string>
    #include<vector>
    using namespace std;
    
    int main()
    {
        vector<string> pleasework;
        string apple = "aaaaaa";
        int end = apple.size()-1;
        int counter = 1;
    for(int j = 0; j < 25; j++)
    {
        for (int i = 0; i < 3; i++)
        {
            apple[end] = (char)(apple[end]+1);
            cout << apple <<endl;
        }
        apple[end] = 'a';
        if(apple[end - counter]!= 'd')
        {
            apple[end - counter] = (char)(apple[end - counter]+1);
        }
        else
        {
            counter++;
        }
    cout << apple <<endl;
    }
    
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,446

    Re: updating a string from aaaa to dddd

    Quote Originally Posted by kjt1991 View Post
    Hello I am writing some code that takes a string of n letters, so a, or aa or aaa or aaaa...a and transforms these into d or dd or ddd or dddd...d by essentially counting. e.g aaaa -> aaab -> aaac -> aaad -> aaba and so on. Also technically the user inputs what the ending is whether it be d or e,f,g; however, for practical reasons counting up to dddd should work. My main issue is I am having trouble getting the letters correct. I get up to aadd and then once I go to abaa I have trouble getting the second to last a to switch correctly.
    OK, so what have you done to debug your code? Did you use your debugger? When that loop executes, what values of which variables are not what you expect?

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jan 2012
    Posts
    2

    Re: updating a string from aaaa to dddd

    Well my problem comes from the fact that my first loop always takes care of the last value in the string and the second for loop takes care of the rest of the values; however, once I get to the the beginning values in the string I do not know how to take care of the middle examples. i.e when I get to aadd the next instruction will be aada, but it won't increment to abaa like it should it will go to aada again and then after going through that cycle back to aadd it goes to abda. And no for some reason I can never get the debugger in code::blocks to work.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,446

    Re: updating a string from aaaa to dddd

    Quote Originally Posted by kjt1991 View Post
    Well my problem comes from the fact that my first loop always takes care of the last value in the string and the second for loop takes care of the rest of the values; however, once I get to the the beginning values in the string I do not know how to take care of the middle examples. i.e when I get to aadd the next instruction will be aada, but it won't increment to abaa like it should it will go to aada again and then after going through that cycle back to aadd it goes to abda. And no for some reason I can never get the debugger in code::blocks to work.
    Well

    1) place some print statements to see what the values of those variables are.

    2) Use a simpler 2 or 3 letter sequence and go through your code by hand. Make the ending character a 'b' instead of 'd', 'e' or some higher value character where you can easily lose track of your place.

    You wrote the code, so you must have had something in mind that worked. Otherwise you should never write code to "work out your thoughts". Always have the plan on paper first before giving it to the compiler, then you know exactly what should happen. When it doesn't happen, you know where to look for the error and fix it.

    As to the debugger -- without a debugger, you're making your job a lot more difficult. What happens if your program is 10 times larger and more complex than what you have written?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 29th, 2012 at 06:40 PM.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center