I don\'t know where I am going wrong but I have a feeling that it\'s with the fi
ID: 3787446 • Letter: I
Question
I don't know where I am going wrong but I have a feeling that it's with the first 2 'printf' of the code.
The problem comes with terminal not letting me enter a conditional variable.
This is the full code if it does not show well in the picture:
#include<stdio.h>
#include<math.h>
int main(void) {
double a;
double b;
double c;
int angle;
char solveFor;
// Prompt user to ente value of angle
printf("Value for angle of triangle: ");
scanf("%d", &angle);
printf("Input variable solving for: ");
scanf("%c", &solveFor);
// Conditions for each solving for variable
if(solveFor == 'C' || solveFor == 'c') {
printf("Input a value for a: ");
scanf("%lf", &a);
printf("Input a value for b: ");
scanf("%lf", &b);
c = sqrt(( a * a ) + ( b * b ));
printf("Value of c is: %f ", c);
} else if(solveFor == 'A' || solveFor == 'a') {
printf("Input a value for b: ");
scanf("%lf", &b);
printf("Input a value for c: ");
scanf("%lf", &c);
a = sqrt(( b * b ) + ( c * c ));
printf("Value of a is: %f ", a);
} else if(solveFor == 'B' || solveFor == 'b') {
printf("Input a value for a: ");
scanf("%lf", &a);
printf("Input a value for c: ");
scanf("%lf", &c);
b = sqrt(( a * a ) + ( c * c ));
printf("Value of b is: %f ", b);
} else {
printf("Invalid variable. Please re-run program. ");
}
return 0;
}
Explanation / Answer
The problme in your code is " You have not handled the new line character". So after taking value of angle variable it takes new line character as value of "solveFor"
You can call "getchar()" method in the code to consume that new line character. So your modified code will be like this:
#include<stdio.h>
#include<math.h>
int main(void) {
double a;
double b;
double c;
int angle;
char solveFor;
// Prompt user to ente value of angle
printf("Value for angle of triangle: ");
scanf("%d", &angle);
getchar();
printf("Input variable solving for: ");
scanf("%c", &solveFor);
// Conditions for each solving for variable
if(solveFor == 'C' || solveFor == 'c') {
printf("Input a value for a: ");
scanf("%lf", &a);
printf("Input a value for b: ");
scanf("%lf", &b);
c = sqrt(( a * a ) + ( b * b ));
printf("Value of c is: %f ", c);
} else if(solveFor == 'A' || solveFor == 'a') {
printf("Input a value for b: ");
scanf("%lf", &b);
printf("Input a value for c: ");
scanf("%lf", &c);
a = sqrt(( b * b ) + ( c * c ));
printf("Value of a is: %f ", a);
} else if(solveFor == 'B' || solveFor == 'b') {
printf("Input a value for a: ");
scanf("%lf", &a);
printf("Input a value for c: ");
scanf("%lf", &c);
b = sqrt(( a * a ) + ( c * c ));
printf("Value of b is: %f ", b);
} else {
printf("Invalid variable. Please re-run program. ");
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.