away a while
Printable View
away a while
i'm back, laserlight, are u there?
Why don't you swap the stakes, you ask step by step instead of the "tutor" telling you step by step.
not so understand what are you talking.. i really try myself many times, just some parts not so understand and i am still a new learner. so, have try myself step by step and ask step by step.. very sad, my group member more newbie than me, i have to type all myself T.T
laserlight, first have to thank you. i tried d and adjust, the code works, but i got some questions, since the max username and max password are 30chars, but let says i am the user, i key in >30 chars and then i have debug error. how to fix it?
here is the lastest code that i have adjust
and these few declarations:Code:#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void librarian_module ( );
int main ( void )
{
FILE* lib_login_pass;
char username [ 30 ];
char password [ 30 ];
char lib_username [ 30 ];
char lib_password [ 30 ];
lib_login_pass = fopen("Librarian_ID.txt", "r" );
if ( !lib_login_pass)
{
printf("Could not open file\a\n" );
exit (101);
}
printf("Please enter the following: \n");
printf("Username: ");
scanf(" %s", username);
printf("Password: ");
scanf(" %s", password);
while ( fscanf ( lib_login_pass, " %s %s", lib_username, lib_password ) == 2 )
{
if ( strcmp(username, lib_username) == 0 )
{
if ( strcmp( password, lib_password ) == 0 )
{
librarian_module ( );
break;
}
}
else
printf("Wrong username or password!\n");
}
return 0;
}
void librarian_module ( )
{
printf("successful\n");
return;
}
char username [ 30 ];
char password [ 30 ];
char lib_username [ 30 ];
char lib_password [ 30 ];
i feel they are wrong, is it correct?
where are you, laserlight T.T i still wan to learn from you, are you free tonight?
fix your %s format to include maximum widthQuote:
i key in >30 chars and then i have debug error. how to fix it?
also check what will happen if the user nam is correct and password will be wrong
i check already, the username and password part nothing error.
what is you meaning? fix your %s format to include maximum width? fix which part?
my questions username max is 30chars. then i set char username [ 30 ]; but when i key in >30 chars, it debug error, how? waht should i write?
Ideally, use fgets() to read a whole line into a (large) buffer, then parse the line.
Quick fix for what you have at the moment - use "%29s"
I say "quick", because no sooner have you prevented buffer overflow, that you introduce yet another problem. Namely, if the input exceeds 30 chars, you no longer know where you are in the file.
Which is why you should separate "input" from "validation".
As it seems your first reaction to anyone fresh who tries to help is "can you do it for me", I'll say "no!".
4 minutes elapsed between my reply, and your Pavlovian response.
1 minute to notify you that a reply had been received.
1 minute for you to fire up the browser and get back to the site
1 minute for you to compose your reaction
Leaving a grand total of ONE WHOLE MINUTE of you attempting to research the answer and figure some stuff out for yourself.
Did you even bother to read the manual page for fgets(), or did you just react "Show me how to use fgets".
If you want to make it as a programmer, you need much more nouse than that!
i'm not tat meaning, i really read through the fgets chapters, my reference book is 'A strutured programming approach using C by behrouz', my teacher said this project should be typed in this book few topics. i have tried the fgets before you tell me, tat is why i reply you in short. i tried it, cant works, sry, i'm still new learner. i just want a simple example, then i try compare myself and do it first before ask
i typed fgets( username, sizeof (username ), stdin ); it is same only, and now my problem is i have debug error if an user key in the username or password more than 30chars.
From FAQ:
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
Search the forum, fgets() is pretty much "covered" in all these posts...
Just so you know, when I saw this thread title yesterday I intentionally ignored it because it was probably from one of those people:
with very poor grammar and spelling who types as though they're texting,
has an assignment due very shortly that they've been procrastinating on,
can't program to save their life and expects everything to be spoon-fed to them,
asks about stuff that a quick google search or a read of the FAQ would answer,
and is generally very fustrating to deal with.
It looks like my assumption wasn't far off, as usual.
How about you start here: http://www.catb.org/~esr/faqs/smart-questions.html