0
votes

I'm trying to write a code to check a password for an uppercase letter. When I type a password with an uppercase letter, the program runs as expected (prints out '1' and the entered password). However, if I type a password without an uppercase letter, the program crashes.

My code:

#include < stdio.h >
#include < stdlib.h >
#include < ctype.h >
#include < string.h >
#include <math.h>

int main() {

    char password[100];
    int i = 0;
    int upper = 0;

    printf("Enter a password with an uppercase letter: ");
    scanf("%s", password);

    for (i = 0; i <= 100; i++) {
        if (isupper(password[i])) {
            upper = 1;
            break;
        }
    }

    printf("%d\n", upper);
    printf("%s\n", password);

    system("pause");
    return (0);
}

The errors:

Debug Assertion Failed!

Program: ...15\Projects\ConsoleApplication3\Debug\ConsoleApplication3.exe File: minkernel\crts\ucrt\src\appcrt\convert\isctype.cpp Line: 36

Expression: c >= -1 && c <= 255

For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application) ConsoleApplication3.exe has triggered a breakpoint.

Debug Assertion Failed!

Program: ...15\Projects\ConsoleApplication3\Debug\ConsoleApplication3.exe File: minkernel\crts\ucrt\src\appcrt\convert\isctype.cpp Line: 42

Expression: c >= -1 && c <= 255

For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application) ConsoleApplication3.exe has triggered a breakpoint.

The program '[13188] ConsoleApplication3.exe' has exited with code 0 (0x0).

2
You have an array of 100 character, but you are accessing character number 101. You might also go outside of the string entered since you don't check for the string terminator, thereby accessing uninitialized memory. LAstly you don't have any limit on the input, which means the user might write more than 100 characters to your array. All of these problems leads to undefined behavior. - Some programmer dude
And you could stop at the first null char encountered by the way. but the message is funny. How can c >= -1 && c <= 255 fail for a char? - Jean-François Fabre
And the part of that array not filled by scanf is uninitialised, so checking its values is worthless. - Weather Vane
@RawN: This apparently is compiled as C++ code, so the C++ tag is correct, the C tag wrong! - too honest for this site
@Jean-FrançoisFabre: The lower bound indeed should be c > -1 resp. c >=0. But isupper seems to be only defined for the standard character set code. and on x86/Windows char is signed. - too honest for this site

2 Answers

2
votes

Change this:

for (i = 0; i <= 100; i++) {

to

for (i = 0; i < 100 && password[i] != '\0'; i++) {
              ^^^      ^^^^^^^^^^^^^^^^^^
        note: no =     check for end of string

You have

char password[100];

so the legal index is only 0 to 99. Further you need to check if you have reached end of string (aka password[I] != '\0')

A better way of doing what you want to achieve (inspired by @Jean-Francois Fabre):

     scanf("%99s", password);
             ^^
             To prevent buffer overflow

     for (i = 0; i < strlen(password); i++) {
         ....
0
votes

change :

for (i = 0; i <= 100; i++) 

to :

for (i = 0; i < strlen(password); i++)

Note : You are going out of bound if there is no uppercase letter.