0

I can't seem to figure out why the program below places the second "r" into the even string when the index is an odd number. I'm new to the debugger but it just showed me the steps. It doesn't explain why. I'm new to programming and am learning on my own so I don't have a teacher or classmates to help out. I appreciate anyone taking the time to help out. Thank you.

#include <iostream>
#include <string>

std::string evenOddString(std::string);

int main()
{
    std::string txt("airforce");
    txt = evenOddString(txt);
    std::cout << txt << std::endl;

    return 0;
}

std::string evenOddString(std::string txt)
{
    std::string even{""}, odd{""}, result{""};
    for (char c : txt)
    {
        if (txt.find(c) % 2 == 0)
        {
            even.push_back(c);
        }
        else
        {
            odd.push_back(c);
        }
    }
    result = even + " " + odd;
    return result;
}

Output:
arorc ife
Program ended with exit code: 0
Churchill
  • 19
  • 1
  • 2
    This seems like the perfect time to learn how to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your programs. To help with that I suggest you save results of complex expressions in temporary variables, like the result of `txt.find(c)` and the result of that modulo with ´2`. – Some programmer dude May 07 '22 at 20:15
  • 2
    When you debug your program, I suggest using the word `abracadabra`. – Eljay May 07 '22 at 20:18
  • 1
    *I'm new to the debugger but it just showed me the steps. It doesn't explain why.* -- The debugger has no idea what your program is supposed to do -- all it does is run the program, a single step/function at a time. Given that, the job of the debugger is to show you the steps your program takes, show you the values of variables, etc. It is your job to determine why your program takes a wrong path or why the variable(s) do not contain the correct values. – PaulMcKenzie May 07 '22 at 20:24
  • `find` will return the first occurence. Instead, use indexes. – ChrisMM May 07 '22 at 20:26
  • 3
    Unless I'm missing something, I don't see the need to even use `find` at all. All the loop has to do is alternate between inserting into the even/odd vectors on each loop iteration. – PaulMcKenzie May 07 '22 at 20:30
  • Paul, that's the problem I had with the debugger. If it runs with no errors, then how can I find the answer? I have about 8 hours of self-taught debugger time so I'm still trying to figure out how to do things other than going through line by line. I used find() because it came up on google as an answer to get the indexes of elements in a string. – Churchill May 07 '22 at 20:48
  • *If it runs with no errors, then how can I find the answer?* -- How does the debugger know what your program is supposed to do? Take a simple example -- I ask you to write a program that adds two numbers, but instead, it subtracts two numbers. Does the program compile? Yes. Does the program run? Yes. Is the program correct? No. So who is the one that knows what the program is supposed to do? It isn't the debugger, it's you. What you're supposed to do is *single-step* through the program, watch variables, etc. and see where the program takes a different path than what you expected. – PaulMcKenzie May 07 '22 at 20:50
  • @Churchill *I'm still trying to figure out how to do things other than going through line by line* -- If you mean line by line using hand calculations and eyeballing the code, attempting to run the program in your head, that is *exactly* what the debugger relieves you of doing. You don't need to do any of that -- [the debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) automates this process. That's all it does. – PaulMcKenzie May 07 '22 at 20:54
  • There's another form of find `txt.find(c, index);` look up how this works and you'll know why and how to fix it although the answer is not the most efficient algorithm out there by a long shot. – QuentinUK May 07 '22 at 20:55
  • Fair enough. I meant "line by line" as in "stepping in". That's about all I really know with the debugger is to "step in" go line by line and watch values change in variables. Like I said, I'm new to programming in general so I'm trying to learn everything on my own. The C++ Udemy course I bought has yet to introduce the debugger so I'm just now getting familiar with it. It's challenging to try and learn the debugger when I'm still learning the language. Anyway, thanks to you, ChrisMM, and QuentinUK for taking the time to reply. I do appreciate it. – Churchill May 07 '22 at 21:13
  • @Churchill-- Basically, when you write a program, you must know what every line is supposed to do, what every function you write is supposed to do, what every `for` loop, `while` loop, setting of a variable, etc. is supposed to do so as to accomplish the program's goal. Even when we know this, hardly any programmer, whether beginner or advanced, writes their initial attempt at the program without logical errors. Once the program is written, but does not do what we expect, the debugger is used to figure out why the program took a different path. – PaulMcKenzie May 07 '22 at 23:51
  • @Churchill -- That's why you must *never* write any code until you have the plan written down first. Another mistake that new programmers make is to dive in and write code right away, without any plan as to what they have written. Then they are asking third-party individuals to figure out the program that *they* wrote. That makes no sense, and you don't want to find yourself in that situation. – PaulMcKenzie May 07 '22 at 23:53

1 Answers1

0

When you call find('r') for the second 'r', it finds the first occurrence of 'r'. And, as the first occurrence is in an even index, it's going to add to the even string. In order to fix it, you must use the indexes to check if they are even or odd:

std::string evenOddString(std::string txt)
{
    std::string even{""}, odd{""}, result{""};
    for (int i = 0; i < txt.size(); i++)
    {
        char c = txt[i];
        if ( i % 2 == 0)
        {
            even.push_back(c);
        }
        else
        {
            odd.push_back(c);
        }
    }
    result = even + " " + odd;
    return result;
}
Paulo
  • 1,458
  • 2
  • 12
  • 26