why this code not work ??

# include <stdio.h>

void get_values(float *r, float *v);

float do_calculations(float resistance, float voltage ) ;
void display_answer(float current);

main()

{

    float resistor;
    float volts;
    float current;

    //explain_program();

    get_values(*r, *v);

    current = do_calculations(resistor, volts);

    display_answer(current);

}

void explain_program()

{

    printf("This program calculates the current in amps. \n");
    printf("Please enter the value of R in ohms and v in volts .\n");

}

void get_values(float *r, float *v)
{
    float resistance;
    printf("\n\n Input the resistance in ohms = ");
    scanf("%f", &*r);  // scan resistance 

    printf("Input the voltage in volts =");  // scan volts 

    scanf("%f", &*v);

    //*r = resistance;
}


float do_calculations(float resistance, float voltage)
{

    float current;

    current = voltage / resistance;
    return (current);

}



void display_answer(float current)
{

    printf("the value of current is %f amps ", current);

}

In your main function, you are using r and v as parameters to a function (line 18), but your main function does not know what r and v are. Did you mean resistor and volts?

Edited 1 Year Ago by Moschops

On line 18, change get_values(*r, *v); with get_values(&resistor, &volts);

Also you can change line 39 from scanf("%f", &*r); to scanf("%f", r);
and line 43 from scanf("%f", &*v); to scanf("%f", v); but these are not stricly necessary.

Edited 1 Year Ago by Maritimo

Compile with something like CodeBlocks to get meaningfull error messages.

This should work:

// ohms+law101.c
// i = v/r

# include <stdio.h>

void get_values(float *r, float *v);

float do_calculations(float resistance, float voltage ) ;
void display_answer(float current);

int main()
{
    float resistor;
    float volts;
    float current;

    //explain_program();

    get_values(&resistor, &volts);  // changed * to &

    current = do_calculations(resistor, volts);

    display_answer(current);
    return 0;  // good habit, also use int main()
}

void explain_program()
{
    printf("This program calculates the current in amps. \n");
    printf("Please enter the value of R in ohms and v in volts .\n");
}

void get_values(float *r, float *v)
{
    float resistance;

    printf("\n\n Input the resistance in ohms = ");
    scanf("%f", r);  // scan resistance
    printf("Input the voltage in volts = ");
    scanf("%f", v);  // scan voltage
}


float do_calculations(float resistance, float voltage)
{
    float current;

    current = voltage / resistance;
    return (current);
}

void display_answer(float current)
{
    printf("The value of current is %f amps ", current);
}

Edited 1 Year Ago by HiHe

@HiHe I'm not downvoting you for your response, but specifying a particular IDE (even if you really like it) is not conducive to fixing the problem. Standard compiler and makefile error output will usually be satisfactory. Also, the IDE is often system specific, especially if the user is running on Windows.

This article has been dead for over six months. Start a new discussion instead.