Federico Amato
Federico Amato

Reputation: 25

Problem with function that is not working in C

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>

int main(void){
    
    float lp=0;
    float cp=0;
    char rc[5];
    int yes=0;
    int country=0;
    char correct[5];
    int mod=0;
    float FunLength(float *lp);
    
          
    printf("Please follow the steps:\n")
                                                                    
    printf("------------------------------------------------ 1\n");
    printf("Enter Lenght:\n");
    float FunLength(float *lp);
    
    do{ 
        printf("-------------------------------------------------2\n");
        printf("do you know the circumference?: \n");
        scanf("%s",rc);
        if(!strcmp(rc,"yes")){
            yes=1;
            break;
        }
        if(!strcmp(rc,"no")){
            yes=2;
            break;
        }
        else{
            printf("You have to write 'yes' or 'no'\n");
            printf("it's not difficult, commit yourself and\n");
            printf("you will make it.\n");
        }
    }while(yes==0);
    
    if(yes==1){
        do{
            printf("-----------------------------------------------2.5\n");
            printf("Enter circumference: ");
            scanf("%f",&cp);
            if(cp>13){
                printf("tray again");
            }
            if(cp<8){
                printf("try again");
            }
        }while(cp<8 || cp>13);
    }
    
    printf("-----------------------------------------------3\n");                                      
    printf("Select country:\n");
    printf("1.Austria\n2.Belgium\n3.Bulgaria\n4.Cyprus\n5.Croatia\n");
    do{
        printf("N° ");
        scanf("%d",&country);
        if(country>5){
            printf("Wrong value\n");
            printf("Try Again\n");
        }
    }while(country==0 || country>5);
    
    printf("Summary:\n");
    printf("Length: %fcm\n",lp);
    if(yes==1){
        printf("Circumference: %fcm\n",cp);
    }
    else if(country==1){
        printf("Country: Austria");
    }
    else if(country==2){
        printf("Country: Belgium");
    }
    else if(country==3){
        printf("Country: Bulgaria");
    }
    else if(country==4){
        printf("Country: Cyprus");
    }
    else if(country==5){
        printf("Country: Croatia");
    }
    
    printf("Is it correct?: ");
    scanf("%s",correct);
    if(!strcmp(correct,"no")){
        printf("What do you want to change?:\n");
        printf("1.Length 2.Circumference 3.Country\n");
        do{
            printf("N° ");
            scanf("%d",&mod);
            if(mod>3){
                printf("Wrong Value\n");
                printf("Try Again\n");
            }
        }while(mod==0 || mod>3);
    }

    return 0;
}

float FunLength(float *lp){
    
    float temp=0;
    
    printf("Enter Length::\n");
    do{
        scanf("%f",&temp);
        *lp=temp;
        if(*lp>22){
                printf("Try Again\n");
        } 
        if(*lp<8){
                printf("Try Again\n");
        }
    }while(*lp>22 || *lp<8);
    return *lp;
}

So that's my code, that compare user sizes with european average sizes. I Would use function and structure. I think I'm gonna use structure with the comparison with every nation. Right now I would use function to make the program more "reusable". The problem is that the following function float FunLength(float *lp) is not working. What am I doing wrong? Also I wanna do the same thing with point 2 and 3, using a function. Please help me, and if you have some advice for the current code or for continuing it, i will appreciate.

Upvotes: 0

Views: 63

Answers (1)

ryyker
ryyker

Reputation: 23208

conflicting types for ‘FunLength’ code.c:16:10: note: previous declaration of ‘FunLength’ was here

In your code example, you are prototyping FunLength() twice. Remove one or the other:

float FunLength(float *lp);/// here

      
printf("Please follow the steps:\n")
                                                                
printf("------------------------------------------------ 1\n");
printf("Enter Lenght:\n");
float FunLength(float *lp);///and here

And then, per the conversation in comments, make all definitions consistent with each other, and such that they support what you are doing:

Change the following:

float FunLength(float *lp);//prototype

To:

void FunLength(float *lp);

And:

float FunLength(float *lp){//function definition

To:

void FunLength(float *lp){   

And because the function is now void, remove its return statement:

    //return *lp;
}

but the function it is as if it were not performed.

FunLength() is not performed because it is not called anywhere. Once you decide where you will call it, call it like this:

(simplified main() function)

int main(void)
{
    float val = 0.0;
    FunLength(&val);
    printf("%f\n", val);
    
    return 0;
}

Upvotes: 2

Related Questions