0

I'm writing a program to generate a string of random uppercase letters, then take user input of uppercase letters, along with a character form the user. For any instance of the user input letter in the random string, it replaces that letter with the character entered by the user.

For example, s1 = {BDHFKYL} s2 = {YEIGH} c = '*'

Output = BD*FK*L

The program is based in a loop and asks if you would like to enter in another string to replace. When I enter a 'y' to enter another loop I get this:

Please enter at least 2 capital letters and a maximum of 20.

HAJSKSMDHSJ

HAJSKSMDHSJ

NWLRBB*QB*C**RZOW**Y*I**Q*C*XR**OWFRX**Y

Would you like to enter another string?

y          -(HERE"S WHERE THE PROBLEM IS)-

Please enter at least 2 capital letters and a maximum of 20.

You need at least two letters

Would you like to enter another string?

Any suggestions? Thank you in advance.

void fillS1(char x[]);

void fillS2(char x[], char y[], char z);

void strFilter(char a[], char b[], char c);

int main(int argc, const char * argv[])
{
    char s1[42];
    char s2[22];

    fillS2(s2, s1, '*');

    return 0;
}

void fillS1(char x[])
{
    for (int i = 0; i < 40; i++)
        x[i] = 'A' + random() % 26;
    x[40] = (char)0;
}

void fillS2(char x[], char y[], char z){
    char loopContinue = 0;

    do {

        int i = 0;

        printf("Please enter at least 2 capital letters and a maximum of 20.\n");
        while (( x[i] = getchar()) != '\n' ) {
            i++;
        }

        x[i] = '\0';

        if (i < 3) {
            printf("You need at least two letters\n");
        }
        else if (i > 21){
            printf("You cannot have more than twenty letters\n");
        }
        else if (i > 0){
            for (i = 0; i < 20; i++) {
                if ((x[i] >= 'A') && (x[i] <= 'Z')) {
                    puts(x);

                    fillS1(y);

                    strFilter(y, x, '*');
                    break;
                }
            }
        }

        printf("Would you like to enter another string?\n");
        scanf("%c", &loopContinue);

    } while (loopContinue != 'n');

}

void strFilter(char a[], char b[], char c){
    int i = 0;
    int n = 0;

    while (n < 20) {
        for (i = 0; i < 40; i++) {
            if (a[i] == b[n]){
                a[i] = c;
            }
        }
        i = 0;
        n++;
    }

    puts(a);
}
Haris
  • 12,120
  • 6
  • 43
  • 70
user1681673
  • 368
  • 1
  • 6
  • 28
  • 2
    You need to clear the buffer I think before running while. When you press enter to either continue (y) or stop (n) the loop the "Enter" key you are pressing is stored to the buffer and is extracted on the next loop, therefore ending the loop prematurely. You can clean the buffer by using **fflush(stdin);** – Theocharis K. Oct 30 '12 at 00:51
  • Thanks for the help. This is for a class and I don't think I'm allowed to use that function. I think you're right about the problem though. I'm just not sure on how to clear it. – user1681673 Oct 30 '12 at 01:06
  • Running a getchar(); right before the loop initiates will clear out the "Enter" key. – Theocharis K. Oct 30 '12 at 01:09

3 Answers3

0

Mixing scanf and getchar seems like a bad idea to me. Why not also use getchar to determine if the user indicated they want to continue? (And don't forget to handle upper and lower case.)

You might want to read the article Get scanf to quit when it reads a newline?

Community
  • 1
  • 1
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
0

When you use:

    scanf("%c", &loopContinue);

It just reads the letter, but not the newline that the user typed after it. When you go back to the top of the loop, the first getchar() reads that newline that was still waiting to be processed.

You need to read the entire line, not just a single character, when you get the response to this question.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Do you mean use gets() instead of getchar()? I would rather use getchar() because it Use that to check how many letters the user entered. Thank you. – user1681673 Oct 31 '12 at 00:24
  • By the way, I changed it to this. printf("Would you like to enter another string?\n"); loopContinue = getchar(); } while (loopContinue != 'n'); – user1681673 Oct 31 '12 at 00:27
  • That doesn't help because it still leaves the newline in the input buffer, where it will be read by the next iteration of the main loop. If you want to read whole lines, gets() is better. Actually, fgets() is even better, because you can specify a maximum length of the buffer; gets() is bad because it will cause a buffer overflow if the line is longer than the buffer (not a problem in a toy program like this, but in the real world this is how security vulnerabilities are caused). – Barmar Oct 31 '12 at 13:57
0

Try to clear x, y and z using memset()

bcmacariola
  • 106
  • 2
  • 15