简体   繁体   中英

How to fix “Debug Error!, Stack around the variable 'x' was corrupted”?

I am really new to C-language and was testing a simple if-statement program which, from your age and gender tells one answer from 4 different outcomes.

Which goes something like this: If you are under the age of 55 and are male, it will print: "You are a man at his best age!"

But I come across a error code which says: Run-Time Check Failure #2 - Stack around the variable 'miesVaiNainen' was corrupted.

I myself, think it might have something to do with: if (manOrFemale == "f" && age <= 55)

How can I fix this issue so the error wont come across?

I've tried to look for help in stackoverflow and tried to change the code a lot.

    int age;
    char manOrFemale;

    printf("Are you a man or a female(m/f)");
    scanf("%s", &manOrFemale);

    printf("Input age:");
    scanf("%d", &age);

    if (manOrFemale == "f" && age <= 55)
    {
        printf("\nYou are a lady at her best!");
    } else if (manOrFemale == "f" && age >= 56)
    {
        printf("\nYou look young for your age!");
    }


    if (manOrFemale == "m" && age <= 55)
    {
        printf("\nYou are a man at his best age!");
    } else if (manOrFemale == "m" && age >= 56)
    {
        printf("\nYou are a wise man!");
    } else {
        printf("There has been an error in the program!");
    }
}

As of now you are using %s specifier to read char . you need to use %c specifier to read char , %s is for char * .

  scanf(" %c", &manOrFemale);

And

you need to use single quotes to compare chars . As of now you are comparing char with pointer.

if (manOrFemale == 'f'; && age <= 55)

Here's a very large problem:

scanf("%s", &manOrFemale);

The variable manOrFemale is a single character. The format "%s" is to read null-terminated byte strings.

A string of only a single character needs space for two characters, to fit the null-terminator. Since you don't have space for the terminator, the scanf function will write into memory you don't own, clobbering the stack (where compilers usually store local variables) leading to the error you get.

If you want to read a single character, then use the "%c" format, as in

scanf(" %c", &manOrFemale);

Do note the leading space in the format string, it's needed to ignore possible leading white-space (like newlines from any previous input).


Also note that with the comparison manOrFemale == "f" you compare the single character in manOrFemale with the string "f" . A literal string in C is really a read-only array of characters, and as any array it decays to a pointer to its first element. So you're not comparing characters, you're comparing a character with a pointer.

Your compiler should have warned you about this.

To solve this you need to compare to a single character instead: manOrFemale == 'f' . Note the use of single-quotes instead.


Lastly a more stylistic note...

The statements

if (manOrFemale == 'f' && age <= 55)
{
    printf("\nYou are a lady at her best!");
} else if (manOrFemale == 'f' && age >= 56)
{
    printf("\nYou look young for your age!");
}

could be rewritten as

if (manOrFemale == 'f')
{
    if (age <= 55)
        printf("You are a lady at her best!\n");
    else
        printf("You look young for your age!\n");
}

One extra thing to note here is that I use trailing newline in the output. That's because otherwise there might not be a newline after the program finishes (so the output seems to be intermingled with the command-line prompt), but also because output to stdout (where printf writes) is by default line-buffered . Line-buffering means that output is buffered internally, and not actually written to the terminal until either the buffer is full, or a newline is written . If you use leading newlines in your output, then the previous line will be written, not the current line.

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