why this code not work ??

# include <stdio.h>

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

float do_calculations(float resistance, float voltage ) ;

main()

{

float resistor;
float volts;
float current;

//explain_program();

get_values(*r, *v);

current = do_calculations(resistor, volts);

}

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);

}

{

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?

All 4 Replies

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?

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.

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 ) ;

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

//explain_program();

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

current = do_calculations(resistor, volts);

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);
}