Thread: ???confused???

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    39

    Unhappy ???confused???

    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct create_account
    {
       char username[16];
       char pass[16];
       char conf_pass[16];
    };
    
    struct login
    {
       char username[16];
       char pass[16];
    };
    
    
    int main()
    {
       struct create_account c;
       struct login l;
       
       FILE *fp = NULL;
       
       char user[16];
       char pass[16];
       
       short int condition = 0;
       short int i = 0;
       short int j = 0;
       short int k = 0;
       char opt_no = 0;
       
       while(k = 0);
       {
       
          printf("\n**************ADMISSION*PROGRAM*FOR****************");
          printf("\n********************STUDENTS***********************");
       
          printf("\n\n");
        
          printf("\n1.LOG IN");
          printf("\n2.CREATE USER");
          printf("\n3.EXIT PROGRAM");
       
          printf("\nEnter Option No.:\t");
          opt_no = getchar();
       
          switch(opt_no)
          {
       
             case '1':
                   printf("\nUsername:\t");
                   scanf("%s", l.username);
                   
                   printf("\nPassword:\t");
                   scanf("%s", l.pass);
                   
                   fopen *fp = ("user_account.txt", "r");
                   while(fscanf(fp, "%s\t%s", user, pass) == 2)
                   {
                      if(user == l.username && pass == l.pass)
                      {
                         printf("YES.");
                      }
                   }
                   break;
                
             case '2':      
       
                   printf("\nEnter Desired Adiminisrator User Name:\t");
                   scanf("%s", c.username);
                   fp = fopen("user_account.txt", "a");
                   
                   fprintf(fp, c.username);
                   fprintf(fp, "\t");
       
      
                   printf("(Enter Desired Password Not Less Than 8 Characters");
                   printf("\nAnd Not More Than 16 Characters.)");
       
                   while(condition == 0) 
                   {
          
                      printf("\n\nPassword:\t\t");
                      scanf("%s", c.pass);
                      j = strlen(c.pass);
                      if(j < 8)
                      {
                         printf("\nError.Password is less than 8 characters.");
                      }
                      if(j > 16)
                      {
                         printf("\nError.Password is more than 16 characters.");
                      }
                      if(j >= 8 && j <= 16)
                      {
                         printf("Confirm Password:\t");
                         scanf("%s", c.conf_pass);
                         i = strcmp(c.pass, c.conf_pass);
             
                         if(i != 0)
                         {
                            printf("Error! Passwords do not match");
                         }
                         if(i == 0)
                         {
                            condition = 1;
                            fprintf(fp, c.pass);
                         }
                         
                         fclose(fp);
                      }
            case '3':
                  k =1;
                  break;
                   }   
          }
       }   
       return 0;
    }
    There's something wrong with my code it compiled before but then gave me errors after i tried to use FILE.

    This is the error:
    ideas.c: In function ‘main’:
    ideas.c:59:22: error: invalid operands to binary * (have ‘struct FILE * (*)(const char * __restrict__, const char * __restrict__)’ and ‘struct FILE *’)
    ideas.c:75:16: warning: format not a string literal and no format arguments
    ideas.c:109:25: warning: format not a string literal and no format arguments

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Code:
    fopen *fp = ("user_account.txt", "r");
    
    /* fopen is not a type, it's a function. instead, use: */
    
    FILE *fp = fopen("user_account.txt", "r");
    if (!fp) 
        break;
    Why do you have 2 structs that virtually do the same thing?

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You do realise that, if you receive error messages or warnings, it is a good idea to examine the lines the compiler highlights as a problem?????


    Line 59, the cause of the error, is
    Code:
        fopen *fp = ("user_account.txt", "r");
    fopen is a function, that returns a pointer to a FILE. fp has been previously declared (near the top of main()) is a pointer to FILE. So line 59 should presumably be
    Code:
       fp = fopen("user_account.txt", "r");
    Changing the order of things, as you did, is a guarantee of confusing the compiler.

    Line 75, which is triggering the warning, is
    Code:
        fprintf(fp, c.username);
    The first argument of fprintf() is supposed a pointer to FILE. Check. The second is a format string. You have left that out. Strictly speaking the compiler is not required to diagnose this case (as a format string is an array of char, and c.username is an array of char, so things technically match). However, your compiler has managed to pick up your mistake and warned you, despite not having to (which is good as, if the code is executed, it will probably do awful things). The line probably needs to be something like
    Code:
        fprintf(fp, "%s\n", c.username);
    Line 109 has the same type of problem.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    39
    Thanx i'll try that and as for the 2 structs even though they do the same thing they serve different purposes, i just like to try to make my code easy to interpret.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by GolDRoger View Post
    i just like to try to make my code easy to interpret.
    Apparently you failed in that as, if you could interpret your own code, you should have been able to associate the faulty code with the errors and warnings.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    39

    HOW???

    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct create_account
    {
       char username[16];
       char pass[16];
       char conf_pass[16];
    };
    
    struct login
    {
       char username[16];
       char pass[16];
    };
    
    
    int main()
    {
       struct create_account c;
       struct login l;
       
       FILE *fp = NULL;
       
       char user[16];
       char pass[16];
       
       short int condition = 0;
       short int i = 0;
       short int j = 0;
       short int k = 0;
       char opt_no = 0;
       
       while(k == 0)
       {
       
          printf("\n**************ADMISSION*PROGRAM*FOR****************");
          printf("\n********************STUDENTS***********************");
       
          printf("\n\n");
        
          printf("\n1.LOG IN");
          printf("\n2.CREATE USER");
          printf("\n3.EXIT PROGRAM");
       
          printf("\nEnter Option No.:\t");
          opt_no = getchar();
       
          switch(opt_no)
          {
       
             case '1':
                   printf("\nUsername:\t");
                   scanf("%s", l.username);
                   
                   printf("\nPassword:\t");
                   scanf("%s", l.pass);
                   
                   fp = fopen("user_account.txt", "r");
                   while(fscanf(fp, "%s\t%s", user, pass) == 2)
                   {
                      if(user == l.username && pass == l.pass)
                      {
                         printf("YES.");
                      }
                   }
                   break;
                
             case '2':      
       
                   printf("\nEnter Desired Adiminisrator User Name:\t");
                   scanf("%s", c.username);
                   FILE *fp = fopen("user_account.txt", "a");
                   
                   fprintf(fp, "%s\t", c.username);
                   fprintf(fp, "\t");
       
      
                   printf("(Enter Desired Password Not Less Than 8 Characters");
                   printf("\nAnd Not More Than 16 Characters.)");
       
                   while(condition == 0) 
                   {
          
                      printf("\n\nPassword:\t\t");
                      scanf("%s", c.pass);
                      j = strlen(c.pass);
                      if(j < 8)
                      {
                         printf("\nError.Password is less than 8 characters.");
                      }
                      if(j > 16)
                      {
                         printf("\nError.Password is more than 16 characters.");
                      }
                      if(j >= 8 && j <= 16)
                      {
                         printf("Confirm Password:\t");
                         scanf("%s", c.conf_pass);
                         i = strcmp(c.pass, c.conf_pass);
             
                         if(i != 0)
                         {
                            printf("Error! Passwords do not match");
                         }
                         if(i == 0)
                         {
                            condition = 1;
                            fprintf(fp, "%s\n", c.pass);
                         }
                         
                         fclose(fp);
                         break;
                      }
            case '3':
                  k =1;
                  break;
                   }   
          }
       }   
       return 0;
    }
    now what i want is for the program to scan a file for the list of available usernames and passwords and compare it to the username and password the user uses to login before continuing with the program, but i don't know how to go about it.

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    The last post came from another thread

    @GolDRoger - Please stop posting a new thread for the same old problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I'm confused..
    By .synq in forum C++ Programming
    Replies: 3
    Last Post: 03-12-2011, 05:05 PM
  2. Confused
    By (TNT) in forum C# Programming
    Replies: 1
    Last Post: 11-23-2005, 04:49 PM
  3. confused
    By mopar123 in forum C Programming
    Replies: 18
    Last Post: 09-01-2005, 04:06 PM
  4. confused...
    By Darkcoder in forum Game Programming
    Replies: 4
    Last Post: 03-09-2005, 01:07 AM
  5. Very confused
    By Aerie in forum C Programming
    Replies: 3
    Last Post: 01-23-2005, 01:08 PM