#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");
}
}
#String User Input not working
88 messages · Page 1 of 1 (latest)
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.
what's wrong!!
What do you want do do?
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");
}
}
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
Then first of all it should be i < 3
but how to it using strigs
ah yes
Also you can't use ==. You need to use strcmp
Also gets is inherently dangerous
as is scanf
you should use fgets instead
oh i c java flashbacks
the && is used correctly right?
yes
|| is for OR
yes
!man 3 strcmp
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);
Also See
arrays, when passed to a function, automatically decay to pointers
So you're good with just using arrays
both the calls to gets and scanf there have no way of knowing when to stop writing to the buffer, so they may overflow the buffers
did you mean to write to password in your call to scanf? password never gets initialized anywhere
oooh
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
what's not working
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
this should word
work
wait wait
ah yes. strcmp returns 0 if they're equal.
^
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
you would have to replace gets(username) with scanf
بكرا بشرحلك
I think it's related to the buffer thing
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
you can't return arrays from functions
you need to wrap the array in a structure and return that
or return a pointer
to some dynamically allocated memory or static memory?
way easier to just return a structure
also the way to declare a function returning an array would really be:
char f(char a[10])[10];
can you even declare a function using "char[10]"
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
oh wait never seen this before
how do we return an array pointer again?
char*palindrome_checker(char*msg){return msg;} would be way simpler
you should just write the pointer in the arguments
the array type will decay into a pointer type, this will make it clearer
char*palindrome_checker(char msg[10]){return msg;}
same same or?
equivalent but more confusing
based
one last question
how would we read a string backward
msg[::-1]
boom i miss python
you just iterate from the last character to the first character
negative index?
nope.
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
;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");
}
Program Output
d
c
b
a
Halalaluyafail3#6046 | 49ms | c | x86-64 gcc 12.2 | godbolt.org
yeah, you will need to use strlen, unless you know the length already
something like this?
it can be something similar to this
int i = 0;
int j = len - 1;
for (i = 0; i < len; i++){
if (message[i] != message[j]){
printf("Not Palindrome");
break;
}
j--;
}```
hmm
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
Thank you and let us know if you have any more questions!
!solved