Thread: Problem of declaration.

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    103

    Problem of declaration.

    Hi!!
    I've trying to implement this function:
    Code:
    void display(account *x,FILE *ptr){
                                account temp;
                                strcpy(temp.user_name , "doesn't exist.");
                                strcpy(temp.password , "Doesn't exist.");
                                temp.privilige = 4;
                                int i = 0;
                                while(!feof(ptr)){
                                                  fread(&temp,sizeof(account),1,ptr);
                                                  if(!strcmp(temp.user_name , x.user_name)){
                                                                                            temp = *x;
                                                                                            exit(1);
                                                                                            }
                                                  }
                                                  printf("\tThe name is: %s\n",temp.user_name);
                                                  printf("\tThe password is: %s\n",temp.password);
                                                  switch(temp.privilige){
                                                                         case 1 :
                                                                              printf("\tThe privilige is admin.\n");
                                                                              break;
                                                                         case 2:
                                                                              printf("\tThe privilige is: general\n");
                                                                              break;
                                                                         case 3:
                                                                              printf("\tThe privilige is mail-only.\n");
                                                                              break;
                                                                         default :
                                                                                 printf("Doesn't exist.\n");
                                                                         }
                                                                         exit(1);
                                                  
                                                  
                              }
         }
    Why the compilter says "user_name" undeclared in the first strcmp call??
    I didn't finish debugging it so..it is full of problems I guess!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The easy guess is that the struct account doesn't contain a field called user_name.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Well, it does!
    Code:
    typedef struct{
                   char user_name[MAX2],password[MAX2];
                   int privilige;
            }account;
    or Am I missing something?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If that typedef is visible to the compiler (in a header file?), then you didn't get the error user_name undeclared. If you did get that error, then the typedef isn't visible.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    It was declared as a global variable, I guess that is equivalent to being in a header file. the problem still persists.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Alright I misread strcpy for strcmp. I was going to spring this on you later when you got there, but we're here now: the thing on the left side of . must be an actual thing. If it is a pointer, well, that doesn't work. Fortunately, the C gods have given us -> which does the same thing, but the left side is a pointer. So you can't ever do x., you must do x-> .

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    JUst to make things clearer for you, you can easily notice that an "}" is added. when I remove it, a lot of similar problems with other variables of type x.user_name arise in the same manner.
    Don't what is going on really!!

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Ok that worked for that one.
    but for this is didn't:
    That is a simple sorting.
    Code:
    void sort(account x[],int n){
                           int result;
                           int min;
                           for (int i = 0 ; i < n  - 1 ; i++){
                                 min = i;
                                 for (int j = i + 1 ; j < n; j++){
                                          if ((result = strcmp(x[i].use_name,x[j].user_name)) > 0)  (here I'm having problems)
                                                      min = j;
                                          }
                                 account temp;
                                 temp = x[i];
                                 x[i] = x[min];
                                 x[min] = temp;
                                 }
         }

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Leojeen View Post
    Ok that worked for that one.
    but for this is didn't:
    That is a simple sorting.
    Code:
    void sort(account x[],int n){
                           int result;
                           int min;
                           for (int i = 0 ; i < n  - 1 ; i++){
                                 min = i;
                                 for (int j = i + 1 ; j < n; j++){
                                          if ((result = strcmp(x[i].use_name,x[j].user_name)) > 0)  (here I'm having problems)
                                                      min = j;
                                          }
                                 account temp;
                                 temp = x[i];
                                 x[i] = x[min];
                                 x[min] = temp;
                                 }
         }
    You should use intellisense or something to keep you from typing "use_name" instead of "user_name".

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Ya ya sorry,
    It is 5am here, I'm awfully tired that's why!
    last thing! what does this mean:
    [Warning] unreachable code at beginning of switch statement .
    Thank you

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Leojeen View Post
    Ya ya sorry,
    It is 5am here, I'm awfully tired that's why!
    last thing! what does this mean:
    [Warning] unreachable code at beginning of switch statement .
    Thank you
    Anything between the switch and the first case label is guaranteed to never happen.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. yet another forward declaration problem.
    By sloppy_coder in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2005, 02:59 AM
  3. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. cannot start a parameter declaration
    By Dark Nemesis in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2005, 02:09 PM