Hey guys, I am having a problem using pointers. In function process_current, I call the function assign_vals, at the end of assign vals, we have values for v1 and v2, but when printed out the values in process_current, we're back at zero?
Please help, here is the code:
Let me know if I am not making sense. Thanks for the time.Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char VOLTAGE[] = "voltage";
const char CURRENT[] = "current";
const char RESISTANCE[] = "resistance";
void process_voltage(double current, double resist);
void process_resistance(double current, double voltage);
void process_current(double voltage, double resist);
void assign_vals(const char s1[], double *v1, const char s2[],
double *v2);
void display_result(const char var1[], double *v1, const char var2[],
double *v2, const char var3[],double *v3);
int main()
{
double voltage = 0;
double resist = 0;
double current = 0;
char unknownval[12];
int read;
printf("What do you wish to know: ");
read = scanf("%s", unknownval);
while(read == 1)
{
if(!strcmp (unknownval, VOLTAGE))
{
process_voltage(resist, current);
}
else if(!strcmp (unknownval, RESISTANCE))
{
process_resistance(voltage, current);
}
else if(!strcmp (unknownval, CURRENT))
{
{
double voltage = 0;
double resist = 0;
double current = 0;
char unknownval[12];
int read;
printf("What do you wish to know: ");
read = scanf("%s", unknownval);
while(read == 1)
{
if(!strcmp (unknownval, VOLTAGE))
{
process_voltage(resist, current);
}
else if(!strcmp (unknownval, RESISTANCE))
{
process_resistance(voltage, current);
}
else if(!strcmp (unknownval, CURRENT))
{
process_current(voltage, resist);
}
else
{
printf("Incorrect unknown value %s. ", unknownval);
printf(" Exiting.\n");
exit(1);
}
read = scanf("%s", unknownval);
}
printf("\n");
return 0;
}
void process_voltage(double resist, double current)
{
double voltage;
assign_vals(RESISTANCE, &resist, CURRENT, ¤t);
voltage = resist * current;
}
else
{
printf("Incorrect unknown value %s. ", unknownval);
printf(" Exiting.\n");
exit(1);
}
read = scanf("%s", unknownval);
}
printf("\n");
return 0;
}
void process_voltage(double resist, double current)
{
double voltage;
assign_vals(RESISTANCE, &resist, CURRENT, ¤t);
voltage = resist * current;
display_result(RESISTANCE, &resist, CURRENT, ¤t,
VOLTAGE, &voltage);
}
void process_resistance(double current, double voltage)
{
double resist;
assign_vals(VOLTAGE, &voltage, CURRENT, ¤t);
resist = voltage / current;
display_result(VOLTAGE, &voltage, CURRENT, ¤t,
RESISTANCE, &resist);
}
void process_current(double voltage, double resist)
{
double current;
assign_vals(VOLTAGE, &voltage, RESISTANCE, &resist);
//Values in voltage and resist are zero, not what they were in assign_vals
printf("%lf", voltage);
printf("%lf", resist);
current = voltage / resist;
display_result(VOLTAGE, &voltage, RESISTANCE, &resist,
CURRENT, ¤t);
}
void assign_vals(const char var1[], double *v1, const char var2[],
double *v2)
{
double value1;
double value2;
char str1[12];
char str2[12];
printf("First variable: ");
scanf("%s", str1);
printf("Value: ");
scanf("%lf", &value1);
printf("Second variable: ");
scanf("%s", str2);
printf("Value: ");
scanf("%lf", &value2);
if(!strcmp(str1, str2))
{
printf("not enough unique variables. exiting\n");
exit(1);
}
else if((!strcmp(str1, var1) && !strcmp(str2, var2)))
{
v1 = &value1;
v2 = &value2;
}
else if((!strcmp(str1, var2) && !strcmp(str2, var1)))
{
v1 = &value2;
v2 = &value1;
}
else
{
printf("variables are not valid for this option.");
printf(" exiting\n");
exit(1);
}
//Prints out the values in v1 and v2 correctly, now go back to process_current //function
printf("%lf", *v1);
printf("%lf", *v2);
}
void display_result(const char str1[], double *v1, const char str2[],
double *v2, const char str3[], double *v3)
{
printf("\n");
printf("%s = %.2lf", str1, *v1);
printf("%s = %.2lf", str2, *v2);
printf("%s = %.2lf", str3, *v3);
}