I wrote this program... Just gave a try!!
i got a problem with searching a string in file.. the "checkp" used in the login function, returns a value "1", whereas it must return a value "0" Hence my program fails...
what i felt is, the file i'm opening, "web_reg.txt", in that, extra spaces are coming...

Does anyone knows, to sort this out???? Please help me!!
please check it out in your PC's.
Thanks a lottt

The code is as following:

#include<conio.h>
#include<stdio.h>
#include<string.h>
struct web
{
char name[30],pass[30];
}w;
int n;
void login(void);
void reg(void);
void main()
{
clrscr();
printf("\n\n\n\n\n\t\t\t\tWELCOME TO MY WEBSITE");
printf("\n\t\t\t\t=====================");
printf("\n\n\n\n\t\t\tPress Enter to proceed...!!");
if(getchar()=='\n')
  clrscr();
printf("\n\n\n\t\t\t1. LOGIN\t\t2. REGISTER");
printf("\n\n\n\t\t\t\tENTER YOUR CHOICE: ");
scanf("%d",&n);
switch(n)
  {
    case 1: clrscr();
        login();

    case 2: clrscr();
        reg();

  }
}
void reg()
  {
    FILE *fp;
    char c; int i=0;
    fp=fopen("Web_reg.txt","ab+");
    printf("\n\n\t\t\t\tWELCOME TO REGISTER ZONE");
    printf("\n\t\t\t\t^^^^^^^^^^^^^^^^^^^^^^^^");
    printf("\n\n\t\t\t\t  ENTER USERNAME: ");
    scanf("%s",w.name);
    printf("\n\n\t\t\t(press spacebar after ryping the pasword)");
    printf("\n\n\t\t\t\t  DESIRED PASSWORD: ");
    while((c=getch())!=' ')
        {
        w.pass[i++]=c;
        printf("%c",'*');
        }
    fwrite(&w,sizeof(w),1,fp);
    fclose(fp);
      printf("\n\n\tPress enter if you agree with Username and Password");
      if(getchar()=='\n')
        {
        clrscr();
        printf("\n\n\t\tYou are successfully registered");
        printf("\n\n\t\tTry login your account??\n\n\t\t  ");
        printf("> PRESS 1 FOR YES\n\n\t\t  > PRESS 2 FOR NO");
        scanf("%d",&n);
        switch(n)
          {
              case 1: clrscr();
                    login();
                    break;
              case 2: clrscr();
                    printf("\n\n\n\t\t\t\t\tTHANK YOU FOR REGISTERING");
                    break;
          }

        }
    getch();
  }

  void login()
    {
      FILE *fp;
      char spc,c,name[30],pass[30]; int i=0;
      int checku,checkp;
      fp=fopen("Web_reg.txt","rb");
      printf("\n\n\t\t\t\tWELCOME TO LOG IN ZONE");
      printf("\n\t\t\t\t^^^^^^^^^^^^^^^^^^^^^^");
      printf("\n\n\t\t\t\t  ENTER USERNAME: ");
      scanf("%s",name);
      printf("\n\n\t\t\t(press spacebar after ryping the pasword)");
      printf("\n\n\t\t\t\t  ENTER PASSWORD: ");
      while((c=getch())!=' ')
        {
        pass[i++]=c;
        printf("%c",'*');
        }
      while(!feof(fp))
        {
        fread(&w,sizeof(w),1,fp);
          checku=strcmp(name,w.name);
          checkp=strcmp(pass,w.pass);
          if(checku==0&&checkp==0)
          {
            clrscr();
            printf("\n\n\n\t\t\tYOU HAVE LOGGED IN SUCCESSFULLY!!");
            printf("\n\n\n\t\t\t\tWELCOME, HAVE A NICE DAY");
            break;
          }
        else if(checku==0&&checkp!=0)
          {
            printf("\n\n\n\t\t\tWRONG PASSWORD!! Not %s??",name);
            printf("\n\n\t\t\t\t\t(Press 'Y' to re-login)");
            if(getch()=='y'||getch()=='Y')
              login();
          }
        else if(checku!=0&&checkp!=0)
          {
            printf("\n\n\n\t\t\tYou are not a Registered User\n \t\t\tPress enter to register yourself");
            if(getchar()=='\n')
            reg();
          }

        }
      getch();

    }

Recommended Answers

All 11 Replies

The character array pass cannot find a null value after the last password character. It will continue to read beyond the last character until it finds a null value (on or before the 30th byte after the first character). That is why the pass read at Line 93 may include additional garbage characters, hence the inequality with the password read in the file. To rectify it, add a pass[i] = 0; between lines 88 and 89 to let the program know where the true password ends.

I added:

pass[i]='\0'

between 88 and 89.. That works fine!! :) Thanks a lot. I really forgot about the NULL.
@scudzilla..
I had one more thing to ask!! How and where can i use the fflush()?? I want to use it because 50th line doesn't show up in the output, as the buffer has stored previous "enter" keys.. any suggestionfor this??

Try changing line 51 with if((c = getch())==13), 13 being the ascii of the character of carriage return.

Try changing line 51 with if((c = getch())==13), 13 being the ascii of the character of carriage return.

AWESOME!! i got it! sorry, but what is this 13 actually is?? and why is it used there??
I need more explanation on it!!

The keyboard's Enter key gives a combination of two characters: carriage return (\r, ascii=13) and newline (\n, ascii=10). Since you're reading only one character, the character that is read is the first one which is carriage return. Look them up for their functions :) (Forgot to mention the combination is for Windows systems. Other systems may differ)

wow!! got to learn something new.. :) Thanks a lott!!!

means i can even write if((c=getchar())=='\r') ???

Yes, that's correct.

okay!! thanks.. you told me things which my teacher didn't tell :)

but after writing getchar()=='\r' when i press enter the program stops working.. ?? :P

Oh, sorry I thought you wrote getch and not getchar. getchar doesn't work because it reads a stored newline in the buffer. Depending on the condition of your if statement, if((c=getchar())=='\n') will force the program to continue to lines 53 and after, while if((c=getchar())=='\r') will skip those lines and go directly to line 69. On neither situations will the program wait for an input from you at line 51. The program waits for you to press any key before it ends because line 69 contains getch();

Instead, at line 51, use if((c=getch())=='\r') or even just if(getch()=='\r') works.

hmmm.. thanks for that!! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.