Thread: sorting and the use of isdigit

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    72

    sorting and the use of isdigit

    i need help with checking if there's a number or a digit in an imput which is here the password

    Code:
    void fill_account(account *c)
    {
         int flag=0,i,sum;
    	 
    	 printf("give the name of account to add");
         scanf("%s",&c->name);
        
         printf("give the password");
         
    	 scanf("%s",&c->password);
         
    	 printf("give previliges");
         
    	 scanf("%s",&c->previleges);
         
    	/* while(sum == 0)
    	{
    	i = 0;
    		while (c->.password[i])
      		{
        		if (isdigit(c->password[i]))
    				sum++;
    				
    			i++;
       	
    		}	
    			if(sum == 0)
    			{
    				printf("\n Warning!\nYou have entered no digits in your password, please make sure that you enter at least 1 digit\a\a\a\a\n");
    				printf("\nPlease enter the account Password:      \n");
    				printf("\n");
    				scanf("%s", c->password);
    			}		
    					
    	
    	*/}

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    72
    need to adapt the check_password function with the program above

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And what's wrong with it? (Other than the fact that it's commented out, and you have c->. instead of just c-> I mean.)

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    72
    the comment represent the standard method of checking the digits but it didn't work and i don't realy know how to implement it in the fill_account

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You think you are saying something when you say "it didn't work" but I assure you you are not. When you tried it, what happened? If the answer is "I forgot to initialize sum to 0", then do so.

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    72
    for the sorting part here's the whole program but the sort() function crashes
    Code:
    #include <stdio.h>
    #include  <ctype.h>
    #include  <string.h>
    
    typedef struct account
    
    {
         char name[20];
         char password[20];
         char previleges[20];   
            
            
            
            }account;
    
    
    void sort_account(char* );
    void fill_account(account);
    int choice(void);
    void account_write(char *,account);        
    void account_search(char *);
    void account_delete(char *);
    void change_previlege(char *);
    void account_write2(char *,account );
    
    void view_account(char *);
    
    void change_password(char *);
    void change_countype(char *);
    int main(){
        account c;
        int n;
        char *filename="account.txt";
        int option;
    	while( (option=choice())!=8)
    		{
    	switch(option){
    		
        case 1: account_write(filename,c);
        		break;
    	case 2: account_search(filename);
        		break;
    	case 3: account_delete(filename);
    			break;
        case 4: sort_account(filename);
    	case 5: view_account(filename);
    			break;
    	    
        case 6: change_password(filename);
        		 break;
        case 7: change_previlege(filename);		 
        default: printf("unavailable option");
        
        
    	}
    	
    	//view_account(filename,n);
           
         
       
        
        
        
        }
    }
    void fill_account(account *c)
    {
         int flag,i=0,sum=0;
    	 
    	 printf("give the name of account to add");
         scanf("&#37;s",&c->name);
         
         printf("give the password");
         
    	 scanf("%s",&c->password);
         
    	 
         
    while (sum==0)          
             {
               while(c->password[i])
                {
                       if(isdigit(c->password[i]))
                          sum++;
                           i++;
                       
                }
                
                if(sum==0)
                         {
                          printf("re-enter the password the last one is wrong");
                          scanf("%s",&c->password);                       
                                   
                          }                    
                                  
                                  
             }
    printf("give previliges");
         
    scanf("%s",&c->previleges);
    }
    void account_write(char *filename,account c)
    {
         
        FILE  *file=fopen(filename,"ab");
        fflush(file);
        if(file!=NULL)
          { 
            
            fill_account(&c);
            fwrite(&c,sizeof (account),1,file);
            
          
                      
                      
              } 
              
           fclose(file) ; 
     
               
            }    
         
         
     
    void account_search(char *filename)
       {int count=0,i=0,j=0; 
         char NAME[20];
         account t[20];
         FILE *file=fopen(filename,"rb");
         if(file!=NULL)
         {
                         
                                        
                              while(fread(&t[i],sizeof (account),1,file)==1)   
                                {   
                                    i++;                           
                                    count++; 
                                }                
         
                         
         
         }
         printf("give the name of accountto search for");
         scanf("%s",&NAME);
         for(j=0;j<count;j++)
          {
             if(strcmp(t[j].name,NAME)==0)
               printf("%s \t %s\t %s \t",t[j].name,t[j].password,t[j].previleges);          
                  
                  }
              fclose(file);
        
         }
    
         
    void view_account(char *filename)
    {
         int count=0;
         int i=0;
         account t[30];
         FILE *file=fopen(filename,"rb");
         if(file!=NULL)
         {
                         
                                        
                              while(fread(&t[i],sizeof (account),1,file)==1)   
                                {   
                                    i++;                           
                                    count++; 
                                }                
         
                         
         
         }
         printf(  "Acc.Name    |     Password|       Privilege");
    	 for(i=0;i<count;i++)
         {
             printf("\n   %s   |   %s          |   %s\n ",t[i].name,t[i].password,t[i].previleges);    
                 
                 }
         
         
         
         
         
         
         }
    void account_delete(char *filename)
    {
         
         
         
         int count=0,i=0,g,h;
         char NAME[20];
         account t[20];
         FILE *file=fopen(filename,"rb");
         if(file!=NULL)
         {
                         
                                        
                              while(fread(&t[i],sizeof (account),1,file)==1)   
                                {   
                                    i++;                           
                                    count++; 
                                }                
         
                         
         
         }
         printf("give the name of account to delete");
         scanf("%s",&NAME);
         for( g=0;g<count;g++)
           {if(strcmp(t[g].name,NAME)==0)
               {  for(h=g;h<count;h++)
                    {
                         t[h]=t[h+1];
                        }
                }    
            }  
            count--;  
         
            for(i=0;i<count;i++)
              { 
                    account_write2(filename,t[i]);
                    }             
              view_account(filename);
              fclose(file);                                          
         
         
         
         }
    void account_write2(char *filename,account c )
    {
       
        FILE  *file=fopen(filename,"wb");
        fflush(file);
        if(file!=NULL)
          { 
            
          
            fwrite(&c,sizeof (account),1,file);
            
          
                      
                      
              } 
              
           fclose(file) ; 
        
         
    }
    
    void change_password(char *filename)
    {
         
         
         
         int count=0,i=0; 
         char pwd[20];
         char pwd1[20];
         char pwd2[20]; 
         char NAME[20];
         account t[20];
         int k,j;
         FILE *file=fopen(filename,"rb");
         if(file!=NULL)
         {
                         
                                        
                              while(fread(&t[i],sizeof (account),1,file)==1)   
                                {   
                                    i++;                           
                                    count++; 
                                }                
         
                         
         
         }
          printf("give the name of accountyou want to change the password");
         scanf("%s",&NAME);
         for(j=0;j<count;j++)
          {
             if(strcmp(t[j].name,NAME)==0)
               
           {    
               printf("give the old password (contains at least one alphabetic character)" );
                scanf("%s",&pwd);
        
                 if(strcmp(t[j].password,pwd)==0)
                                 {  printf("give the new password ");
                                    scanf("%s",&pwd1);
                                    printf("confirm the new password ");
                                    scanf("%s",&pwd2);
                                   }               
                                 
                   if(strcmp(pwd1,pwd2)==0) strcpy(t[j].password,pwd1);            
         
         }          
              
               }          
                  
                  FILE  *file1=fopen(filename,"wb");
        
                      for(int i=0;i<count;i++)
              { 
                    if(file1!=NULL)
                          { 
            
          
                           fwrite(&t[i],sizeof (account),1,file1);
            
          
                      
                      
                                } 
                    }             
              fclose(file1);
              
              view_account(filename);
                          
         
           
           
            }    
    
    int choice (void)  {
    	 int menu;
    	 
    	 printf("Please enter your choice :\n\n");
    	 printf("\t1- Create a new account\n"
                "\t2- View information about an account\n"
                "\t3- delete an acount\n"
                "\t4- view all acou\n"
                "\t- sort\n"
    			"\t5- Change the privilege of an account\n"
                "\t6- End programm\n?");
                
      scanf("%d",&menu);
      return(menu);
    }
         
    
     void change_previlege(char *filename)
     {
          int i=0,count=0;
          char pass[20],name[20];
          char previleges[20];
          account t[20];
          FILE *file=fopen(filename,"rb");
         if(file!=NULL)
         {
                         
                                        
                              while(fread(&t[i],sizeof (account),1,file)==1)   
                                {   
                                    i++;                           
                                    count++; 
                                }                
         
                         
         
         }
          
         printf("give the name for wich you would like to change the previleges\n");
         scanf("%s",&name);
         printf("give the password");
         scanf("%s",&pass);
         for (int i=0;i<count;i++)
          {
            if ((strcmp(pass,t[i].password)==0)&&(strcmp(name,t[i].name)==0))
                    {
                           printf("give the new previlege");                  
                           scanf("%s",&previleges);
                           strcpy(t[i].previleges,previleges);                      
                                             
                                             
                     }        
             
             } 
        fclose(file);
           FILE  *file1=fopen(filename,"wb");
        
                      for(int i=0;i<count;i++)
              { 
                    if(file1!=NULL)
                          { 
            
          
                           fwrite(&t[i],sizeof (account),1,file1);
            
          
                      
                      
                                } 
                    }             
              fclose(file1);
              view_account(filename);
         
          }    
          
    void sort_account(char *filename)
    {
         int i,j,count=0;
         FILE *file=fopen(filename,"rb");
         account t[30];
         account temp;
         if(file!=NULL)
                       {
                         
                                        
                              while(fread(&t[i],sizeof (account),1,file)==1)   
                                {   
                                    i++;                           
                                    count++; 
                                }                
         
                         
         
                        }
                        
         for (i=0; i<count; ++i)
    		    {           for (j = i+1; j <count	; ++j)
                           {
    			                if (strcmp(t[i].name,t[j].name)>0)
                                {
    				               temp=t[i];
    				               t[i]=t[j];
    				               t[j]=temp;
                                 }
               }
                                 }   
                        
                        
         view_account(filename);
         fclose(file);               
                        
          
         
         
         
         }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Please indent your code properly. It is hard to read when everything is all over the place.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And if you want to start with i=0, why don't you set i to 0 instead of leaving it at "indeterminate value", because who knows whether t[indeterminate value] is valid.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    72
    in the for loop its already initialized no? (for i=0....)

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by overlord21 View Post
    in the for loop its already initialized no? (for i=0....)
    And does the line
    Code:
    while(fread(&t[i],sizeof (account),1,file)==1)
    appear in that for loop?

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    72
    in the for loop its already initialized no? (for i=0....)

  12. #12
    Registered User
    Join Date
    May 2008
    Posts
    72
    ok even if declared the "i" this doesn't sort any explanation

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You forgot to re-write your array back to disk before trying to read it again with view_account?

  14. #14
    Registered User
    Join Date
    May 2008
    Posts
    72
    even if out of topic can anyone tell why did function removes all but on account as i only want to remove one?
    Code:
    void account_delete(char *filename)
    {
         
         
         
         int count=0,i=0,g,h;
         char NAME[20];
         account t[20];
         FILE *file=fopen(filename,"rb");
         if(file!=NULL)
         {
                         
                                        
                              while(fread(&t[i],sizeof (account),1,file)==1)   
                                {   
                                    i++;                           
                                    count++; 
                                }                
         
                         
         
         }
         printf("give the name of account to delete");
         scanf("&#37;s",&NAME);
         for( g=0;g<count;g++)
           {if(strcmp(t[g].name,NAME)==0)
               {  for(h=g;h<count;h++)
                    {
                         t[h]=t[h+1];
                        }
                }    
            }  
            count--;  
         
            for(i=0;i<count;i++)
              { 
                    printf("%s", t[i].name);
                    }             
             
              fclose(file);                                          
              view_account(filename);
         
         
         }

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by overlord21 View Post
    even if out of topic can anyone tell why did function removes all but on account as i only want to remove one?
    That's surprisingly hard to do, since the code removes only one and not all but one. Do you not see all the names you expect to see in the print loop? (Granted, bad things are going to happen in your view_account, because you have yet again forgotten to write the file before you try to re-read it.)

Popular pages Recent additions subscribe to a feed