Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I solve this if condition — I don't know where the problem is in this case?

float i, r1, r2, sum1, sum2; 

char resistance[30];

printf("what is the curent total: ");

scanf("%f", &i);
 
printf("what is the resistance1 and resistance2: ");

scanf("%f%f", &r1, &r2);

printf("what is the R you want to know there curren\n: ");

printf("notes:write your choes in capital example R1,R2:");

scanf("%s", resistance);

sum1 = (r2 / r1 + r1) * i;
sum2 = (r1 / r1 + r2) * i;

if (strcmp(resistance, "R1") == 0) {
    printf("the current in this resistance is %f", sum1);
} else if {
    printf("the current in this resistance is %f", sum2);
}

>Solution :

Your code fragment does not compile because there is an extra if after the else. You can test another condition in the else part with else if (condition) but this does not seem to be your intent.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is a modified version:

#include <stdio.h>
#include <string.h>

int main() {
    float i, r1, r2, sum1, sum2; 
    char resistance[30];
    
    printf("what is the total current: ");
    if (scanf("%f", &i) != 1)
        return 1;
     
    printf("what are the resistance1 and resistance2: ");
    if (scanf("%f%f", &r1, &r2) != 2)
        return 1;
    
    printf("what is the R you want to know there current\n: ");
    printf("note: write your choice in capitals example R1,R2: ");
    if (scanf("%29s", resistance) != 1)
        return 1;
    
    sum1 = (r2 / r1 + r1) * i;
    sum2 = (r1 / r1 + r2) * i;
    
    if (strcmp(resistance, "R1") == 0) {
        printf("the current in resistance R1 is %f\n", sum1);
    } else if (strcmp(resistance, "R2") == 0) {
        printf("the current in resistance R2 is %f\n", sum2);
    } else {
        printf("choice must be R1 or R2\n");
    }
    return 0;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading