#String User Input not working

88 messages · Page 1 of 1 (latest)

shy fractal
#
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
    char hard_username[10] = "waseem";
    char hard_pass[10] = "waseem123";
    char username[10];
    char password[10];

    for (int i =0; i<4; i++){
        printf("Please Insert Username: \n");
        gets(hard_username);
        printf("Please Insert Password:\n");
        scanf("%s", hard_pass);

        if (hard_username == username && hard_pass == password){
            printf("Perfect Match !\nYou cab Log Successfully!");
            break;
        } else{
            printf("USERNAME OR PASSWORD DON'T MATCH!\n");
        }

    }
stray laurelBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

shy fractal
#

what's wrong!!

frosty valley
#

What do you want do do?

shy fractal
#
    char hard_username[10] = "waseem";
    char hard_pass[10] = "waseem123";
    char username[10];
    char password[10];

    for (int i =0; i<4; i++){
        printf("Please Insert Username: \n");
        gets(username);
        printf("Please Insert Password:\n");
        scanf("%s", hard_pass);

        if (hard_username == username && hard_pass == password){
            printf("Perfect Match !\nYou cab Log Successfully!");
            break;
        } else{
            printf("USERNAME OR PASSWORD DON'T MATCH!\n");
        }

    }
shy fractal
# frosty valley What do you want do do?

a C program that asks the user to enter a username and password. Your program should check whether both username and password match a hardcoded username and password, and display meaningful messages in case the username or the password is incorrect. The program gives the user three chances to enter the correct values. Note: assume that both the username and the password are numbers.

#

oh number

frosty valley
#

Then first of all it should be i < 3

shy fractal
#

but how to it using strigs

shy fractal
frosty valley
#

Also you can't use ==. You need to use strcmp

#

Also gets is inherently dangerous

#

as is scanf

#

you should use fgets instead

shy fractal
frosty valley
#

a super worthy article to read

shy fractal
#

the && is used correctly right?

frosty valley
#

yes

shy fractal
#

|| is for OR

frosty valley
#

yes

shy fractal
#

wait how to use strcmp()

#
strcmp(hard_username, username);
frosty valley
#

!man 3 strcmp

nocturne ventureBOT
#

strcmp, strncmp - compare two strings

The strcmp() function compares the two strings s1 and s2. The
locale is not taken into account (for a locale-aware comparison,
see strcoll(3)). The comparison is done using unsigned
characters.

Synopsis

#include <string.h>

int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
shy fractal
#

should I use pointers too?

#

same as example

frosty valley
#

arrays, when passed to a function, automatically decay to pointers

#

So you're good with just using arrays

rancid thunder
shy fractal
#

yes

#
   char hard_username[10] = "waseem";
    char hard_pass[10] = "waseem123";
    char username[10];
    char password[10];

    for (int i =0; i<3; i++){
        printf("Please Insert Username: \n");
        gets(username);
        printf("Please Insert Password:\n");
        scanf("%s", password);

        if ( strcmp(hard_username, username) && strcmp(hard_pass, password)){
            printf("Perfect Match !\nYou cab Log Successfully!");
            break;
        } else{
            printf("USERNAME OR PASSWORD DON'T MATCH!\n");
        }

    }
#

still not working
note: I am using gets and scanf for practice purposes

frosty valley
#

what's not working

shy fractal
#

ooooh i think it's the strcmp I have to fix one sec

#
    char hard_username[10] = "waseem";
    char hard_pass[10] = "waseem123";
    char username[10];
    char password[10];

    for (int i =0; i<3; i++){
        printf("Please Insert Username: \n");
        gets(username);
        printf("Please Insert Password:\n");
        scanf("%s", password);

        if ( strcmp(hard_username, username) ==0 && strcmp(hard_pass, password) == 0){
            printf("Perfect Match !\nYou cab Log Successfully!");
            break;
        } else{
            printf("USERNAME OR PASSWORD DON'T MATCH!\n");
        }

    }

This works for perfect match Thanks

shy fractal
#

wait wait

frosty valley
#

ah yes. strcmp returns 0 if they're equal.

silent bobcat
#

^

shy fractal
#

Please Insert Username:
waseem
Please Insert Password:
waseem321
USERNAME OR PASSWORD DON'T MATCH!
Please Insert Username:
Please Insert Password:
waseem
USERNAME OR PASSWORD DON'T MATCH!
Please Insert Username:
Please Insert Password:

why's this happening?!

#

for matching password and username worked now

silent bobcat
shy fractal
#

works thanks!

#

no idea why tho ?

silent bobcat
#

بكرا بشرحلك

shy fractal
#
char[10] palindrome_checker(char msg[10]){
    return msg;
}

how do we return arrays of character aka strings

#

@frosty valley got an extra minute plz

frosty valley
#

you can't return arrays from functions

rancid thunder
frosty valley
#

or return a pointer

rancid thunder
#

also the way to declare a function returning an array would really be:

char f(char a[10])[10];
silent bobcat
nocturne ventureBOT
#
char f(char a[10])[10];

Declare f as function(array[10] of char named a) returning array[10] of char

Diagnostics

⛔ Return type may not be an array
ℹ Parameter of array type decays to pointer

shy fractal
#

how do we return an array pointer again?

rancid thunder
#

you should just write the pointer in the arguments

#

the array type will decay into a pointer type, this will make it clearer

shy fractal
#
char*palindrome_checker(char msg[10]){return msg;}

same same or?

rancid thunder
#

equivalent but more confusing

shy fractal
#

one last question

#

how would we read a string backward

#

msg[::-1]
boom i miss python

frosty valley
#

you just iterate from the last character to the first character

shy fractal
#

negative index?

frosty valley
#

nope.

shy fractal
#

hmmm ooh u saying assuming I know the total length right?

#

backward for loop I c what u mean

#

if msg[10] but the actual string length is 5 it won't work as expected orr

#

unless I use strlen() of course

rancid thunder
#

;compile

#include<string.h>
#include<stdio.h>
void foo(const char*s){
    const char*p=s+strlen(s);
    while(p!=s){
        printf("%c\n",(int)(unsigned char)*--p);
    }
}
int main(void){
    foo("abcd");
}
drowsy grottoBOT
#
Program Output
d
c
b
a
frosty valley
#

yeah, you will need to use strlen, unless you know the length already

silent bobcat
shy fractal
#

we can use len - 1 directly inside the loop.

#
void palindrome_checker(char msg[10]){
    int len = strlen(msg);
    int j = len - 1; // last index
    for (int i = 0; i < len; i++){
        if (msg[i] != msg[j]){
            printf("Not Palindrome!\n");
            break;
        }
        printf("Palindrome!\n");
    }
}
#
Insert A string:
a7a
Palindrome!
Not Palindrome!

what's with the output!

#

!solved

stray laurelBOT
#

Thank you and let us know if you have any more questions!

silent bobcat
#

!solved