简体   繁体   中英

How to chain append in C++

#include <iostream>
#include <cstring>

using namespace std;

int main() {
    char chars[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    int e = 0;
    int f = 0;
    int g = 0;
    int h = 0;
    int i = 0;
    int j = 0;
    int k = 0;
    string pwd;
    string attempt;

    cout << "Type the password: ";
    cin >> pwd;

while (a < 26) {
    attempt = chars[a];
    a = a + 1;
    if (attempt == pwd) {
        cout << "The password is: " << attempt;
        break;
    }
    if (a >= 26) {
        a = 0;
        break;
    }
}

while (a < 26 && b < 26) {
    attempt =  "";
    attempt.append( 1, chars[a] ).append( 1, chars[b] );
    b = b + 1;
    if (attempt == pwd) {
        cout << "The password is: " << attempt;
        break;
    }
    if (b >= 26) {
        a = a + 1;
        b = 0;
    }
    if (a >= 26 && b >= 26) {
        a = 0;
        b = 0;
        break;
    }
}

while (a < 26 && b < 26 && c < 26) {
    attempt =  "";
    attempt.append( 1, chars[a] ).append( 1, chars[b] ).append( 1, chars[c] );
    c = c + 1;
    if (attempt == pwd) {
        cout << "The password is: " << attempt;
        break;
    }
    if (c >= 26) {
        b = b + 1;
        c = 0;
    }
    if (b >= 26) {
        a = a + 1;
        b = 0;
    }
    if (a >= 26 && b >= 26 && c >= 26) {
        a = 0;
        b = 0;
        c = 0;
        break;
    }
}

return 0;
}

I am confused with attempt.append

This works:

    attempt.append( 1, chars[a] ).append( 1, chars[b] );

But this doesn't:

    attempt.append( 1, chars[a] ).append( 1, chars[b] ).append( 1, chars[c] );

It doesn't display the text. Can you please help. How do you chain multiple appends? I have looked everywhere but I can't understand the solution. This is a brute force program and I know it is probably the most inefficient one. If you do have a simpler solution, please tell me! Thanks.

There is nothing wrong with attempt.append( 1, chars[a] ).append( 1, chars[b] ).append( 1, chars[c] ); (well, aside from it being a relatively slow/verbose way to do the simpler C++11 initializer list approach of attempt.assign({chars[a], chars[b], chars[c]}); ). You've got a logic error elsewhere.

Looking closer, the error appears to be in the final if of your second while loop (the one testing two character passwords):

if (a >= 26 && b >= 26) {
    a = 0;
    b = 0;
    break;
}

That if only fires if both a and b reach 26 , but the prior if (b >= 26) ensures that when a hits 26, b is reset to 0, which terminates your loop when while (a < 26 && b < 26) is evaluated with a == 26 and b == 0 .

Thus, you reach your third loop with a == 26 and b == 0 . while (a < 26 && b < 26 && c < 26) { immediately determines that a is not less than 26, and the loop is never entered at all.

A simple fix for this is to explicitly reset your loop variables before each while loop begins, eg a = b = c = 0 . This would also allow you to remove the otherwise unnecessary final if in each loop (allowing the while condition to handle exiting the loop without resetting indices within the loop itself).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM