Reputation: 61
My code:
I have tried multiple ways to fix this but to no avail.
I have to use scanf
in the code so I cannot replace it with fgets
as many people have told me to.
Can someone tell me what's wrong with my code?
#include <stdio.h>
float Parallel(float R1, float R2);
void main(void) {
char circ;
float R1, R2, R3;
printf("Select Circuit [A/B/X]: ");
scanf("%c", &circ);
if (circ == 'A') {
printf("Enter R1 and R2: ");
scanf("%f%f", &R1, &R2);
printf("%f%f", R1, R2);
Parallel(float R1, float R2);
printf("Total resistance for Circuit A is %.2f Ohm", RT);
}
if (circ == 'B') {
printf("Enter R1, R2 and R3: ");
scanf("%f%f%f", &R1, &R2, &R3);
Parallel(float R1, float R2);
RT = RT + R3;
printf("Total resistance for Circuit B is %.2f Ohm", RT);
}
if (circ == 'X') {
printf("That's All");
}
}
float Parallel(float R1, float R2);
{
float RT;
RT = 1 / (1 / R1 + 1 / R2);
return RT;
}
Upvotes: 2
Views: 63
Reputation: 144685
There is a syntax error in the definition of Parallel
: the extra ;
should be removed. Putting the {
on the same line would have made this mistake obvious:
float Parallel(float R1, float R2) {
float RT;
RT = 1 / (1 / R1 + 1 / R2);
return RT;
}
The calls to the function Parallel
are incorrect too: Parallel(float R1, float R2)
should be changed to:
RT = Parallel(R1, R2);
Also note that main
must be defined with a return type of int
.
Here is a modified version with a loop to perform tasks iteratively:
#include <stdio.h>
float Parallel(float R1, float R2);
int main() {
char circ;
float R1, R2, R3;
for (;;) {
printf("Select Circuit [A/B/X]: ");
if (scanf(" %c", &circ) != 1)
return 1;
if (circ == 'A') {
printf("Enter R1 and R2: ");
if (scanf("%f%f", &R1, &R2) != 2)
return 1;
printf("%f%f", R1, R2);
RT = Parallel(R1, R2);
printf("Total resistance for Circuit A is %.2f Ohm\n", RT);
} else
if (circ == 'B') {
printf("Enter R1, R2 and R3: ");
if (scanf("%f%f%f", &R1, &R2, &R3) != 3)
return 1;
RT = Parallel(R1, R2);
RT = RT + R3;
printf("Total resistance for Circuit B is %.2f Ohm\n", RT);
} else
if (circ == 'X') {
printf("That's All\n");
return 0;
} else {
printf("Invalid option\n");
}
}
}
float Parallel(float R1, float R2) {
float RT;
RT = 1 / (1 / R1 + 1 / R2);
return RT;
}
Upvotes: 1
Reputation: 8579
Replace 2 occurrences of Parallel(float R1, float R2);
with float RT=Parallel(R1,R2);
.
As pointed out by others, the code you have is not how to call a function nor do you use the result.
Remove a stray semi-colon in the line float Parallel(float R1, float R2);
toward the end of the code.
The one at the start is correct in a prototype but not in a function definition.
Amend the line void main(void)
to int main(void)
and add return 0;
to the end of main()
. You may not want a return value but this keeps C sweet. ;)
PS: It's a long time since I studied electricity. I can't validate you calculations!
#include <stdio.h>
float Parallel(float R1, float R2);
int main(void)
{
char circ;
float R1, R2, R3;
printf("Select Circuit [A/B/X]: ");
scanf("%c", &circ);
if(circ == 'A')
{
printf("Enter R1 and R2: ");
scanf("%f%f", &R1, &R2);
printf("%f%f", R1, R2);
float RT=Parallel(R1,R2);
printf("Total resistance for Circuit A is %.2f Ohm", RT);
}
if(circ == 'B')
{
printf("Enter R1, R2 and R3: ");
scanf("%f%f%f", &R1, &R2, &R3);
float RT=Parallel(R1,R2);
RT = RT + R3;
printf("Total resistance for Circuit B is %.2f Ohm", RT);
}
if(circ == 'X')
{
printf("That's All");
}
return 0;
}
float Parallel(float R1, float R2)
{
float RT;
RT = 1/(1/R1+1/R2);
return RT;
}
Upvotes: 0