Will Johnson
Will Johnson

Reputation: 21

Undefined refrence to function in codeblocks?

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

void LoginMenu(char *usr[30],char *pass[30]);
void portal(char *usr[30],char *pass[30]);
void SalesDepartement( );
main(){
    char usr[30]={0},pass[30]={0};
    int ans=0;
    LoginMenu(&usr,&pass);
    portal(&usr,&pass);
}

void LoginMenu(char *usr[30],char *pass[30]) {

    printf("\t\t\t\t\t\tLogin Menu\n\n");
    printf("Username:");
    scanf("%s",&*usr);
    printf("Password:");
    scanf("%s",&*pass);
    //printf("%s\t %s",usr,pass);
}

void portal(char *usr[30],char *pass[30]){
    int ans=0,nt;
    if(strcmp(usr,"SalesAdmins")==0 && strcmp(pass,"Sales2020")==0){

        system("cls");
        printf("\n\t\t\t\t\t\t\t\t\t\tDepartement Name : Sales Departement");
        printf("\n\t\t\t\t\t\t\t\t\t\tAccount Status : limited access !");
        printf("\nAvailable Functions:");
        printf("\n\n1-customer account mangament\n\n2-go to sales\n\n3-Log Out\n\nyour answer (in numbers):");
        scanf ("%d",&ans);


        if(ans==1){
            //CustomerID();
        }
        else if (ans==2){
            SalesDepartement();
        }
        else if (ans==3){
            system("cls");
            main();
        }
        else if(ans>3 || ans<1){
            printf("\a\nPlease Enter A Valid Choice!\n\n");
            system("pause");
            system("cls");
            portal(&*usr,&*pass);
        }
    }
    else if(strcmp(usr,"FinancialAdmins")==0 && strcmp(pass,"Financial2020")==0){
        system("cls");
        printf("\n\t\t\t\t\t\t\t\t\t\tDepartement Name : Financial Departement");
        printf("\n\t\t\t\t\t\t\t\t\t\tAccount Status : limited access !");
        printf("\nAvailable Functions:");
        printf("\n\n1-Create A Financial Report\n\n2-View Analytics\n\n3-Log Out\n\nyour answer (in numbers):");
        scanf ("%d",&ans);


        if(ans==1){
            //CustomerID();
        }
        else if (ans==2){
            //SalesDepartemet();
        }

        else if (ans==3){
            system("cls");
            main();

        }

        else if(ans>3 || ans<1){
            printf("\a\nPlease Enter A Valid Choice!\n\n");
            system("pause");
            system("cls");
            portal(&*usr,&*pass);
        }

    }


    else if(strcmp(usr,"InventoryAdmins")==0 && strcmp(pass,"Iventory2020")==0){
        system("cls");
        printf("\n\t\t\t\t\t\t\t\t\t\tDepartement Name : Inventory Departement");
        printf("\n\t\t\t\t\t\t\t\t\t\tAccount Status : limited access !");
        printf("\nAvailable Functions:");
        printf("\n\n1-Go to Inventory\n\n2-Log Out\n\nyour answer (in numbers):");
        scanf ("%d",&ans);


        if(ans==1){
            //CustomerID();
        }

        else if (ans==2){
            system("cls");
            main();

        }

        else if(ans>2 || ans<1){
            printf("\a\nPlease Enter A Valid Choice!\n\n");
            system("pause");
            system("cls");
            portal(&*usr,&*pass);
        }
        //  InventoryDepartement();

    }

    else if(strcmp(usr,"HrAdmins")==0 && strcmp(pass,"Hr2020")==0){
        system("cls");
        printf("\n\t\t\t\t\t\t\t\t\t\tDepartement Name : Hr Departement");
        printf("\n\t\t\t\t\t\t\t\t\t\tAccount Status : limited access !");
        printf("\nAvailable Functions:");
        printf("\n\n1-Go to Hr Management\n\n2-Log Out\n\nyour answer (in numbers):");
        scanf ("%d",&ans);


        if(ans==1){
            //CustomerID();
        }

        else if (ans==2){
            system("cls");
            main();

        }

        else if(ans>2 || ans<1){
            printf("\a\nPlease Enter A Valid Choice!\n\n");
            system("pause");
            system("cls");
            portal(&*usr,&*pass);
        }
        //  HRDepartement()

    }

    else if(strcmp(usr,"CEO")==0 && strcmp(pass,"19104381")==0){
        system("cls");\
            printf("\n\t\t\t\t\t\t\t\t\t\tDepartement Name : Head Departement");
        printf("\n\t\t\t\t\t\t\t\t\t\tAccount Status : Full Access !");
        printf("\nAvailable Functions:");
        printf("\n\n1-customer account mangament\n\n2-go to sales\n\n3-Create A Financial Report\n\n4-View Analytics\n\n5-Go to Inventory\n\n6-Go to Hr Management\n\n7-Log Out\n\nyour answer (in numbers):");
        scanf ("%d",&ans);


        if(ans==1){
            //CustomerID();
        }

        else if (ans==2){


        }

        else if (ans==3){


        }

        else if (ans==4){


        }

        else if (ans==5){


        }

        else if (ans==6){

        }

        else if (ans==7){
            system("cls");
            main();

        }


        else if(ans>2 || ans<1){
            printf("\a\nPlease Enter A Valid Choice!\n\n");
            system("pause");
            system("cls");
            portal(&*usr,&*pass);
        }
        //  SalesDepartemet();

    }
}

void SalesDepartemet()  {
    //first
    system("cls");
    double tax=0.14;
    struct Sales{

        int custid;
        int employid;
        char method[30];
        int paid;
        int total;
        int recieve;
    }sale;

    printf("Enter Customer ID:");
    scanf("%d",&sale.custid);
    printf("Enter Employee ID:");
    scanf("%d",&sale.employid);
    printf("Enter Payment Method:");
    scanf("%s",&sale.method);
    //fisrt

}

the compiler shows me undefined reference to the function SalesDepartement(); when I try to call it from the portal function from an if statement, I don't know what is this silly error I almost tried everything and did not work , why is it showing me that in the first place when my function is defined above , is this a problem with the compiler? or have I messed up the code

Upvotes: 1

Views: 85

Answers (2)

user3629249
user3629249

Reputation: 16540

regarding;

char usr[30]={0},pass[30]={0}; 
.... 
portal(&usr,&pass);

in C, a 'bare' reference to the name of an array degrades to the address of the first byte of the array, so the call to portal() should be:

portal( usr, pass ); 

then the signature:

void portal(char *usr[30],char *pass[30]);

is claiming that the parameters are pointers to arrays of pointers. This should be:

void portal( char *usr, char *pass );
--OR-- 
void portal( char usr[ 30 ], char pass[ 30 ] ); 

the problem of passed parameters, that the compiler passes as a pointer (not an array of pointers) is found in several places in the posted code.

regarding:

main(){ 

this is not a valid signature for main() and will cause the compiler to output a warning message:

untitled1.c:8:1: warning: return type defaults to ‘int’ [-Wimplicit-int].

A valid signature would be:

int main( void )

regarding the prototype:

void SalesDepartement( ); 

When prototyping a function signature that will not receive parameters, always insert void between the parens. Other wise the compiler produces code that can take any number of parameters of any type. Suggest:

void SalesDepartement( void ); 

OT: for ease of readability and understanding (the compiler does not care)

  1. follow the axiom: only one statement per line and (at most) one variable declaration per statement.

  2. insert a 'reasonable' space: inside parens, after commas, after semicolons, inside brackets, inside braces, around C operators.

  3. insert a blank line around code blocks: for if else while do...while switch case default.

  4. insert 2 or 3 blank lines (be consistent) between functions.

regarding:

system("cls");

this is not portable ( for instance linux does not recognize cls as a valid shell command

regarding:

 printf("\n\t\t\t\t\t\t\t\t\t\tDepartement Name : Sales Departement");
 printf("\n\t\t\t\t\t\t\t\t\t\tAccount Status : limited access !");
 printf("\nAvailable Functions:");
 printf("\n\n1-customer account mangament\n\n2-go to sales\n\n3-Log Out\n\nyour answer (in numbers):");

it is always best to 'honor' the right margin. (not all terminals have 'infinite' width for lines and lines will be chopped off when printing. Suggest: (including a few spelling corrections)

printf("\n\t\t\t\t\t\t\t\t\t\tDepartement Name : Sales Department"
       "\n\t\t\t\t\t\t\t\t\t\tAccount Status : limited access !"
       "\nAvailable Functions:\n\n"
       "1-customer account management\n\n"
       "2-go to sales\n\n"
       "3-Log Out\n\n"
       "your answer (in numbers):");

regarding;

scanf ("%d",&ans);

The user can do lots of things to make this call to scanf() fail, like entering a EOF or entering a letter. Always check the returned value to assure the operation was successful. and what happens if the user enters a number outside the range 1...3 or only a return? Suggest:

while( 1 )
{
    if( scanf( "%d", &ans ) == 1 )
    {
        // insert the switch() statement (from later in this answer) here
    }

    else
    {
        fprintf( stderr, "scanf failed to input the menu selection\n" );
        int ch;
        while( ( ch = getchar() ) != EOF && ch != '\n' );
    }
}

regarding:

    if(ans==1){
        //CustomerID();
    }
    else if (ans==2){
        SalesDepartement();
    }
    else if (ans==3){
        system("cls");
        main();
    }
    else if(ans>3 || ans<1){
        printf("\a\nPlease Enter A Valid Choice!\n\n");
        system("pause");
        system("cls");
        portal(&*usr,&*pass);
    }

This recursive call to portal() is a very poor idea as it never 'unwinds' the stack.

This sequence of if() statements results in the target variable ans being repeatedly tested. Much better to only test it once. Suggest:

    switch(ans)
    {
        case 1:
            //CustomerID();
            break;

        case 2:
            SalesDepartement();
            break;

        case 3:
            exit( EXIT_SUCCESS );
            break;
      
        default:
            printf( "%d is invalid, try again\n", ans );
            break;
    }

regarding:

system("cls");
main();

NEVER call the function: main() from your code. If you want to 'start over' then implement a loop in the main() function

Similar considerations exist for the code blocks starting with:

 else if(strcmp(usr,"InventoryAdmins")==0 && strcmp(pass,"Iventory2020")==0){

and

else if(strcmp(usr,"CEO")==0 && strcmp(pass,"19104381")==0){

in the above code, the spelling: SalesDepartement(); does not match the actual function, those spelling is: SalesDepartemet()

regarding:

printf( "Username:" );
scanf( "%s", usr );

There is nothing stopping the user from entering a string that is over 29 characters long. The result would be a buffer overflow and undefined behavior. The 'input format conversion' specifier should have a MAX CHARACTERS modifier that is 1 less than the length of the input buffer because the %s specifier always appends a NUL byte to the input. and always check for failure: Suggest:

printf( "Username:" );
if( scanf( "%29s", usr ) != 1 )
{
    fprintf( stderr, "scanf for user failed\n" );
    exit( EXIT_FAILURE );
}

Upvotes: 0

jkb
jkb

Reputation: 2450

You left the 'n' out of SalesDepartement in your definition of the function.

Upvotes: 1

Related Questions