Thread: Getting wrong output

  1. #1
    Registered User
    Join Date
    Jun 2011
    Location
    Bolpur, India
    Posts
    42

    Getting wrong output

    Hi all.I executed a small program without any error but not getting the correct output.Please help me finding where is it going wrong?
    Code:
    //Program to execute jannat.exe
    #include<string.h>
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    int main()
    {
    	struct date
    	{
    		 int dd;int mm;int yy;
    
    	};
    
    	struct masterlist
    	{
    		struct date bday;
    		char name[20];
    		char name1[20];
    		char live[10];
    		char password1[15];
    		char password2[15];
    		char col[15];
    		int key;
    	 };
    	 struct masterlist m;
    //	int nom=0;
    	int ch,n=0,flag,ptr,ssize;
    //	long int msize;
    	FILE *fp,*ft;
    	fp=fopen("JANNATINFO.DAT","rb+");
    		if(fp==NULL)
    	{
    		 fp=fopen("JANNATINFO.DAT","wb+");
    		{
    			 if(fp==NULL)
    			{
    				printf("\n Cannot Access File!!!");
    				exit(1);
    			 }
    		  }
    	  }
    	  ssize=sizeof(m);
    	while(1)
    	{
    
    		  printf("\n 1>Create An Account");
    
    		printf("\n 2>Log In To An Account");
    		printf("\n 3>Statistics for a particular month->");
    		printf("\n 4>Credits");
    		printf("\n 5>Information about this user");
    		printf("\n 6>Exit");
    		printf("\nNow enter your choice->");
    		scanf("%d",&ch);
    		switch(ch)
    		{
    			case 1:  fseek(fp,0,SEEK_CUR);
    					 printf("\n Enter your full name->");
    					 scanf("%s",m.name);
    					 fflush(stdin);
    					 flag=0;
    //					 if(flag==0)
    //			  {
    
    					 printf("\n %s Please Enter your birthdate(dd/mm/yyyy)",m.name);
    					 scanf("%d%*c%d%%*c%d",&m.bday.dd,&m.bday.mm,&m.bday.yy);
    /*					 if((m.bday.dd<0||m.bday.dd>31)||(m.bday.mm>12||m.bday.mm<0)||(m.bday.yy<1900||m.bday.yy>2011))
    				  {
    					  printf("\n You entered improper date");
    					  printf("\n Try again:");
    					  flag=0;
    				  }
    					  else
    					  flag=1;
    
    			} */
    						printf("\n Enter name of your college:");
    						fflush(stdin);
    					 scanf("%s",m.col);
    					 fflush(stdin);
    
    					 printf("\n Enter where you live->");
    					 scanf("%s",m.live);
    	//				 puts(m.name);
    					 fflush(stdin);
    					 while(1)
    				 {
    					 printf("\n Enter the password you wish->");
    					 scanf("%s",m.password1);
    
    					 fflush(stdin);
    					 printf("\n Please retype your password->");
    					 scanf("%s",m.password2);
    
    				     ptr=strcmp((m.password2),(m.password1));
    					 if(ptr==0)
    					 {
    						  printf("\n Your password is set!!!");
    						  break;
    					 }
    					 else
    					 {
                          printf("\n Try Again!!!");
    					  continue;
                         }
    				 }
    					n++;
    					printf("\nCongratulations!!! Your Account is successfully registered with Jannat.exe");
    					printf("\n you are the %d th member",n);
    					m.key=n;
    					getch();
    					system("cls");
    					break;
    					case 2:   system("cls");
    					          printf("\n <<---------------LOGIN PAGE---------------->>");
    							  printf("\n Enter yor name:");
    							  scanf("%s",m.name1);
    							  printf("\n Entre your Password:");
    							  scanf("%s",m.password2);
                                  rewind(fp);
                                  while(fread(&m,ssize,1,fp)==1)
                                  {
    
    
    							  if(strcmp(m.name1,m.name)==0 && strcmp(m.password1,m.password2)==0)
    							  {
    
                                     printf("\n Welcome Back %s!!We are happy to have you!!!",m.name);
    								 printf("\n You have successfully Logged IN!!");
    								 printf("\n Your membership ID is %d",m.key);
    								 goto control;
    								 break;
                                  }
    								else
    								{
                                     printf("\nYou are not Authorised to Enter!!!");
    								 break;
    								}
    control:						 printf("\n Now here are the list of actions you can do:");
    								 printf("\n 1.View Profile \n 2.Edit Profile \n 3. Enter Bazzar");
    								 printf("'\n 4.Delete Profile \n5.Exit");
    								 printf("\n Enter your Choice->");
    								 scanf("%d",&ch);
    								 do
    								 {
    
    								 switch(ch)
    								 {
    								     case 2:
    					                         printf("\n Enter your full name->");
    					                         scanf("%s",m.name);
                                                 fflush(stdin);
                                                 printf("\n %s Please Enter your birthdate(dd/mm/yyyy)",m.name);
                                                 scanf("%d%*c%d%%*c%d",&m.bday.dd,&m.bday.mm,&m.bday.yy);
                                                 printf("\n Enter name of your college:");
    						                     fflush(stdin);
    					                         scanf("%s",m.col);
                                                 fflush(stdin);
                                                 printf("\n Enter where you live->");
    					                         scanf("%s",m.live);
    					                        fseek(fp,-ssize,SEEK_CUR);
    					                         fwrite(&m,ssize,1,fp);
    								             break;
    								     case 1: printf("\n Your Name-%s",m.name);
                                                 printf("\n Your Birthday is-%d/%d/%d",m.bday.dd,m.bday.mm,m.bday.yy);
                                                 printf("\n Your Live At-%s",m.live);
                                                 printf("\n Name of Your college is-%s",m.col);
    								             break;
    /*								     case 3:entbaz();
    								             break;
    								     case 4:delpro();
    								             break;
                                         case 5:exit(0);
                                                 break;*/
    								      default:printf("\n Invalid Choice!!!Try Again");
    								              break;
    								 }
    								 }while(ch!=(5||4));
                                  }
    								 break;
    								 }
    				 }
    				}
    I input correct name and password but getting the message "You are not authorised to Enter".Please help!!
    "1st allow yourself to make the basics clear and then step ahead to make a glorious victory over the subject and unleash your power to manipulate it !"
    Regards MISTU4U

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok, your masterlist includes 2 usernames and 2 passwords... when you ask a person for their username and password half way down the file you are overwriting these values... Or, if they are unused from elsewhere you are a introducing garbage into your struct.

    Code:
    char name[20], password[15];
    printf("\n <<---------------LOGIN PAGE---------------->>");
    printf("\n Enter yor name:");
    scanf("%s",name);
    printf("\n Entre your Password:");
    scanf("%s",password);
    
    
    rewind(fp);
    while(fread(&m,ssize,1,fp)==1)
       {  if (! strcmp(name,m.name) && !strcmp(password,m.password))
             { printf("\n Welcome Back %s!!We are happy to have you!!!",m.name);
                printf("\n You have successfully Logged IN!!");
                printf("\n Your membership ID is %d",m.key);
    	goto control;
    	break;  }
          else
            { printf("\nYou are not Authorised to Enter!!!"); 
              break;  }
    Now look closely at this slightly repaired loop... It's going to load file record #0, it's not going to match and suddenly you're an unauthorized user... It will not test the rest of the file...

    You need to rearrange your program flow so that the only time someone is considered "unauthorized" is after ALL membership records have been searched. That is... you can't declare someone to be unauthorized from inside the loop.

    Also you need to lose that goto in there... it's generally frowned upon as creating poor program structures.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Location
    Bolpur, India
    Posts
    42
    I used "while(fread(&m,ssize,1,fp)==1)" to check all the records.And I canno understand how can I avoid "goto" here?
    "1st allow yourself to make the basics clear and then step ahead to make a glorious victory over the subject and unleash your power to manipulate it !"
    Regards MISTU4U

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by mistu4u View Post
    I used "while(fread(&m,ssize,1,fp)==1)" to check all the records.
    Except, of course, you don't. You check an entry with an if -- but both the true and the false branches contain a "break", hence no loop.

    If you write your loops in any kind of reasonable way, you will find you won't need a goto.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You do seem to have coded yourself into a corner, that's for sure...

    First with respect to your loop structure... it's going to exit on the first mismatch. This means that unless the person loging on is the first in the file, they will be disallowed. What it needs to do is test the entire file and then, after the loop exits, declare there is no matching user.

    For the other problems...

    Try to break the program down into a more modular form. Use functions. Instead of putting code inside switch() statements have the switch statments call your functions.

    First I would restructure the program to be more concisely menu driven, perhaps with account creation, account management and account logon as 3 entirely separate modules, perhaps even in separate .c source files.

    Then as support functions you could use find user in file, verify logon, etc. as independent functions called by the three larger modules.

    As it is right now, what you have is "spagetti code" very typical of BASIC programming back in the bad old days before function calls and include files... you can do much better now.

    Just think in smaller blobs...
    Last edited by CommonTater; 06-23-2011 at 01:02 PM.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by mistu4u View Post
    I used "while(fread(&m,ssize,1,fp)==1)" to check all the records...
    No, you are not checking all the records here. Your loop just reads the first record and then evaluates yes/no then exits.

    Code:
         while(fread(&m,ssize,1,fp)==1)//read first record from file
         {
                 if record matches matches login and break from loop
                 if not break from loop
         }
    ...
    Nowhere do you allow the loop to continue to check the other records. Something like:

    Code:
         while(fread(&m,ssize,1,fp)==1)//read first record from file
         {
                 if record matches matches login and break from loop
                 if not 
                       Are we at the end of list
                                  if not continue checking records
                                  break from loop
                 
         }

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Following up on Andrews suggestions....
    Code:
    int CheckPassword(char *name, char *password)
      { while not at end of file
           if name is found
             if pasword matches
              return 1  <-- success!
         repeat
        
         return 0  <-- not found  }

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    As always, thanks for cleaning up my responses CommonTater.

    As for:
    Quote Originally Posted by CommonTater View Post
    ...As it is right now, what you have is "spagetti code" very typical of BASIC programming back in the bad old days before function calls and include files...
    What is wrong with a little BASIC. Brings me back......
    Code:
    10 INPUT "What is your name: ", U$
    20 PRINT "Hello "; U$
    30 INPUT "How many stars do you want: ", N
    40 S$ = ""
    50 FOR I = 1 TO N
    60 S$ = S$ + "*"
    70 NEXT I
    80 PRINT S$
    90 INPUT "Do you want more stars? ", A$
    100 IF LEN(A$) = 0 THEN GOTO 90
    110 A$ = LEFT$(A$, 1)
    120 IF A$ = "Y" OR A$ = "y" THEN GOTO 30
    130 PRINT "Goodbye "; U$
    140 END

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    As always, thanks for cleaning up my responses CommonTater.
    Naaa... just following your lead....

  10. #10
    Registered User
    Join Date
    Jun 2011
    Location
    Bolpur, India
    Posts
    42
    Thank you friends for making the point clear.I will apply these modifications as you suggested.
    "1st allow yourself to make the basics clear and then step ahead to make a glorious victory over the subject and unleash your power to manipulate it !"
    Regards MISTU4U

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your code looks like it was indented by your cat jumping onto the keyboard!
    Code:
    //Program to execute jannat.exe
    #include<string.h>
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    int main()
    {
        struct date {
            int dd;
            int mm;
            int yy;
    
        };
    
        struct masterlist {
            struct date bday;
            char name[20];
            char name1[20];
            char live[10];
            char password1[15];
            char password2[15];
            char col[15];
            int key;
        };
        struct masterlist m;
    //  int nom=0;
        int ch, n = 0, flag, ptr, ssize;
    //  long int msize;
        FILE *fp, *ft;
        fp = fopen("JANNATINFO.DAT", "rb+");
        if (fp == NULL) {
            fp = fopen("JANNATINFO.DAT", "wb+");
            {
                if (fp == NULL) {
                    printf("\n Cannot Access File!!!");
                    exit(1);
                }
            }
        }
        ssize = sizeof(m);
        while (1) {
    
            printf("\n 1>Create An Account");
    
            printf("\n 2>Log In To An Account");
            printf("\n 3>Statistics for a particular month->");
            printf("\n 4>Credits");
            printf("\n 5>Information about this user");
            printf("\n 6>Exit");
            printf("\nNow enter your choice->");
            scanf("%d", &ch);
            switch (ch) {
            case 1:
                fseek(fp, 0, SEEK_CUR);
                printf("\n Enter your full name->");
                scanf("%s", m.name);
                fflush(stdin);
                flag = 0;
    //                   if(flag==0)
    //            {
    
                printf("\n %s Please Enter your birthdate(dd/mm/yyyy)",
                       m.name);
                scanf("%d%*c%d%%*c%d", &m.bday.dd, &m.bday.mm, &m.bday.yy);
    /*                   if((m.bday.dd<0||m.bday.dd>31)||(m.bday.mm>12||m.bday.mm<0)||(m.bday.yy<1900||m.bday.yy>2011))
                      {
                          printf("\n You entered improper date");
                          printf("\n Try again:");
                          flag=0;
                      }
                          else
                          flag=1;
    
                } */
                printf("\n Enter name of your college:");
                fflush(stdin);
                scanf("%s", m.col);
                fflush(stdin);
    
                printf("\n Enter where you live->");
                scanf("%s", m.live);
                //               puts(m.name);
                fflush(stdin);
                while (1) {
                    printf("\n Enter the password you wish->");
                    scanf("%s", m.password1);
    
                    fflush(stdin);
                    printf("\n Please retype your password->");
                    scanf("%s", m.password2);
    
                    ptr = strcmp((m.password2), (m.password1));
                    if (ptr == 0) {
                        printf("\n Your password is set!!!");
                        break;
                    } else {
                        printf("\n Try Again!!!");
                        continue;
                    }
                }
                n++;
                printf("\nCongratulations!!! Your Account is successfully registered with Jannat.exe");
                printf("\n you are the %d th member", n);
                m.key = n;
                getch();
                system("cls");
                break;
            case 2:
                system("cls");
                printf("\n <<---------------LOGIN PAGE---------------->>");
                printf("\n Enter yor name:");
                scanf("%s", m.name1);
                printf("\n Entre your Password:");
                scanf("%s", m.password2);
                rewind(fp);
                while (fread(&m, ssize, 1, fp) == 1) {
                    if (strcmp(m.name1, m.name) == 0
                        && strcmp(m.password1, m.password2) == 0) {
    
                        printf("\n Welcome Back %s!!We are happy to have you!!!",
                             m.name);
                        printf("\n You have successfully Logged IN!!");
                        printf("\n Your membership ID is %d", m.key);
                        goto control;
                        break;
                    } else {
                        printf("\nYou are not Authorised to Enter!!!");
                        break;
                    }
                  control:
                    printf("\n Now here are the list of actions you can do:");
                    printf("\n 1.View Profile \n 2.Edit Profile \n 3. Enter Bazzar");
                    printf("'\n 4.Delete Profile \n5.Exit");
                    printf("\n Enter your Choice->");
                    scanf("%d", &ch);
                    do {
    
                        switch (ch) {
                        case 2:
                            printf("\n Enter your full name->");
                            scanf("%s", m.name);
                            fflush(stdin);
                            printf("\n %s Please Enter your birthdate(dd/mm/yyyy)",
                                 m.name);
                            scanf("%d%*c%d%%*c%d", &m.bday.dd, &m.bday.mm,
                                  &m.bday.yy);
                            printf("\n Enter name of your college:");
                            fflush(stdin);
                            scanf("%s", m.col);
                            fflush(stdin);
                            printf("\n Enter where you live->");
                            scanf("%s", m.live);
                            fseek(fp, -ssize, SEEK_CUR);
                            fwrite(&m, ssize, 1, fp);
                            break;
                        case 1:
                            printf("\n Your Name-%s", m.name);
                            printf("\n Your Birthday is-%d/%d/%d", m.bday.dd,
                                   m.bday.mm, m.bday.yy);
                            printf("\n Your Live At-%s", m.live);
                            printf("\n Name of Your college is-%s", m.col);
                            break;
    /*                                   case 3:entbaz();
                                                 break;
                                         case 4:delpro();
                                                 break;
                                         case 5:exit(0);
                                                 break;*/
                        default:
                            printf("\n Invalid Choice!!!Try Again");
                            break;
                        }
                    } while (ch != (5 || 4));
                }
                break;
            }
        }
    }
    First thing you need to do is decide on some FUNCTIONS to move blocks of code out of main.

    You have nested while(1) statements, and nested switch/case statements, and gotos!

    Actually, the best thing you can do with this code is delete it and start again, with a PLAN
    Spend 5 minutes (or an hour) actually sketching out what the program is supposed to do.

    And not as it seems, just hack away at the code to produce some monstrostity.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting wrong output
    By rafacnsoccer in forum C++ Programming
    Replies: 1
    Last Post: 05-24-2010, 10:45 PM
  2. Wrong output
    By Grad in forum C Programming
    Replies: 4
    Last Post: 12-27-2005, 01:20 PM
  3. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  4. wrong output...
    By _Need-Your_Help in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 12:38 PM
  5. Help! I am getting wrong output
    By mastamoo in forum C Programming
    Replies: 0
    Last Post: 10-08-2001, 10:12 AM