There are two values a and b, if the user enter the value of a and b and print t
ID: 3629674 • Letter: T
Question
There are two values a and b, if the user enter the value of a and b and print the result of a/b (like a =4 , b = 2, on screen a/b=4/2=2)But if a =2, b=0, the code will crash, how to compile the code
Thank you very much !
Please help me check my programs, is there any problems
#include<stdio.h>
#include<stdlib.h>
#include<clib.h>
#include<iostream.h>
int main()
{
float a,b;
printf(" Please enter the valuse of a:");
cin>>a;
printf(" Please enter the valuse of b:");
cin>>b;
{
if (b == 0)
printf("Undefined");
}
printf(" The result of the equation is %f", a/b);
return 0;
}
Explanation / Answer
This is how I would write the program:
---------------------------------------------------------
int main()
{
float a,b;
printf(" Please enter the value of a:");
cin>>a;
printf(" Please enter the value of b:");
cin>>b;
{ //Get rid of this brace!
if (b == 0) {
printf("Undefined");
return 0;
}
printf(" The result of the equation is %f", a/b);
return 0;
}
---------------------------------------------------------
You're receiving an error because after the if statement completes, it continues with the rest of the code. You need a return 0 statement within the if statement so the program stops right then and there. Also, I fixed up the braces for the if statement and spelled value correctly. Let me know if you still have problems.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.