This is the code I have so far. It works well until you try and input a value fo
ID: 3549537 • Letter: T
Question
This is the code I have so far. It works well until you try and input a value for the first variable. It returns segmentation error.
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char **argv) {
char input01;
char input02;
double Volt;
double Res;
double Current;
char V;
char I;
char R;
printf("Current Calculator ");
printf(" ");
printf("Enter the first unit type (V, R, I): ");
scanf("%c", input01);
getchar();
if (input01 == V)
{printf("Enter the voltage: ");
scanf("%lf", Volt);}
if (input01 == R)
{printf("Enter the resistance: ");
scanf("%lf", Res);}
if (input01 == I)
{printf("Enter the current: ");
scanf("%lf", Current);}
printf("Enter the second unit type (V, R, I): ");
scanf("%c ", input02);
getchar();
if (input01 == V)
{printf("Enter the voltage: ");
scanf("%lf", Volt);}
if (input01 == R)
{printf("Enter the resistance: ");
scanf("%lf", Res);}
if (input01 == I)
{printf("Enter the current: ");
scanf("%lf", Current);}
if (input01 == V && input02 == R)
{Current = Volt/Res;
printf("The corresponding current is %f amps. ", Current);}
if (input01 == V && input02 == I)
{Res = Volt/Current;
printf("The corresponding resistance is %f Ohms. ", Res);}
if (input01 == R && input02 == V)
{Current = Volt/Res;
printf("The corresponding current is %f amps. ", Current);}
if (input01 == R && input02 == I)
{Volt = Current * Res;
printf("The corresponding voltage is %f volts. ", Volt);}
if (input01 == I && input02 == R)
{Volt = Current * Res;
printf("The corresponding voltage is %f volts. ", Volt);}
if (input01 == I && input02 == V)
{Res = Volt/Current;
printf("The corresponding resistance is %f Ohms. ", Res);}
return 0;
}
Explanation / Answer
#include <stdio.h>
int main()
{
int option;
float V, I, R, result;
printf( "Press 1 to calculate the electric current; press 2 to calculate the resistance; press 3 to calculate the voltage " );
scanf( "%d", &option );
if( option == 1 ){
printf( "Enter the voltage in volts: " );
scanf( "%f", &V );
printf( "Enter the resistance in ohms: " );
scanf( "%f", &R );
result = V / R;
printf( "The current is %0.2f amps", &result );
}
else if( option == 2 ){
printf( "Enter the voltage in volts: " );
scanf( "%f", &V );
printf( "Enter the current in amps: " );
scanf( "%f", &I );
result = V / I;
printf( "The resistance is %0.2f ohms.", &result );
}
else if( option == 3 ){
printf( "Enter the resistance in ohms: " );
scanf( "%f", &R );
printf( "Enter the current in amps: " );
scanf( "%f", &I );
result = I * R;
printf( "The voltage is %0.2f volts", &result );
}
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.