This code is supposed to take two numbers, and multiply them, divide them and print out the product and quotient. ive checked it over numerous times and cannot figure out where my error is, when i type in a real number and 0, the result screen shuts off prematurely..can anyone tell me where the error is

``````#include <stdio.h>

int main () {
int x,y;
printf("\nPlease enter two real numbers: ");
scanf("%d%d", &x,&y);

if (x>0 && y==0) {
printf("\nError: Dividing by Zero");
printf("\nThe product of %d and %d is: %d.",x,y,x*y);
}
else if  (x==0 && y==0) {
printf("\nEnd of Program");
}

do {
printf("\nThe product of %d and %d is: %d.",x,y,x*y);
printf("\nThe Quotant of %d and %d is: %d.",x,y,x/y);
printf("\nPlease enter two real numbers: ");
scanf("%d%d", &x,&y);

if (x>0 && y==0) {
printf("\nError: Dividing by Zero");
printf("\nThe product of %d and %d is: %d.",x,y,x*y);
}
else if  (x==0 && y==0) {
printf("\nEnd of Program");
}

} while ( x>0 && y>0 );

return 0;
}``````

thanks in advance...also could someone tell me how to mark a thread as closed?

Real numbers don't have to be integers. 1.232 and 4/3 are both potential real numbers, but you're storing integers. Otherwise this looks correct to me.

thanks for pointing that out, and no disrespect, but that has nothing to do with what is happening. also. howw do i mark a thread as solved?

When you enter the second number (y) as 0, this line:

printf("\nThe Quotant of %d and %d is: %d.",x,y,x/y);

attempts to divide by zero. This is bad and it makes your program abort execution.

I see that your code contains a logic check for y being set to zero; unfortunately, your code doesn't actually do anything about it. Looks like you have misunderstood how a do-while loop works. The condition is checked after each execution of the loop.

how do you suggest i fix it? would a normal while loop work?

how do you suggest i fix it? would a normal while loop work?

Yes. A normal while loop checks the condition before running the loop.

Simply put getch();
then exit(0);

This will shows the Error message that you have given and exits the programme untill you press any key.

Here, in your coding it will just give error message and proceeds next coding below it that is do actual division.

uh...not sure what condition to put in the while loop
tried ((x>0)&&(y!=0)) but that made no diffrence

Simply put getch();
then exit(0);

This will shows the Error message that you have given and exits the programme untill you press any key.

Here, in your coding it will just give error message and proceeds next coding below it that is do actual division.

getch() is
1) not portable
2) not Standard C
3) defined in very few compilers

tried ((x>0)&&(y!=0)) but that made no diffrence

It does make a difference. This code does not crash when the second value entered is zero. However, your code is a mess and once the while loop has been entered, the user is free to replace that value with zero pon a subsequent loop, and cause a crash.

``````#include <stdio.h>

int main () {
int x,y;
printf("\nPlease enter two real numbers: ");
scanf("%d%d", &x,&y);

if (x>0 && y==0) {
printf("\nError: Dividing by Zero");
printf("\nThe product of %d and %d is: %d.",x,y,x*y);
}
else if  (x==0 && y==0) {
printf("\nEnd of Program");
}

while (x>0 && y!=0) {
printf("\nThe product of %d and %d is: %d.",x,y,x*y);
printf("\nThe Quotant of %d and %d is: %d.",x,y,x/y);
printf("\nPlease enter two real numbers: ");
scanf("%d%d", &x,&y);

if (x>0 && y==0) {
printf("\nError: Dividing by Zero");
printf("\nThe product of %d and %d is: %d.",x,y,x*y);
}
else if  (x==0 && y==0) {
printf("\nEnd of Program");
}

};

return 0;
}``````
``````#include <stdio.h>

int main () {
double x,y;
printf("\nPlease enter two real numbers: ");
scanf("%lf%lf", &x,&y);

while (x!=0 && y!=0) {
if (x>0 && y!=0) {
printf("\nThe product of %3.2lf and %3.2lf is: %3.2lf.",x,y,x*y);
printf("\nThe Quotant of %3.2lf and %3.2lf is: %3.2lf.",x,y,x/y);
printf("\nPlease enter two real numbers: ");
scanf("%lf%lf", &x,&y);
}
else if  (x>0 && y==0) {
printf("\nError: Dividing by Zero");
printf("\nThe product of %3.2lfand %3.2lf is: %3.2lf.",x,y,x*y);
}

};
printf("\nEnd of Program");

return 0;
}``````

i boiled it down to this, but its still crashing when a real number and a 0 is entered

Do you want the program to continue to run after the else if statement?
You do realize that if the value of y becomes 0 it will exit the loop right?

oh..i didnttake taht into consideration...thanks for piointing it out, and yes i want it to continue running after the else statement

In that case how do you the loop to end?

This is a bit tidier and easier to understand. You'll need to work out how to end the loop.

``````#include <stdio.h>

int main () {
double x,y;

while (1) {
printf("\nPlease enter two real numbers (second cannot be zero): ");
scanf("%lf%lf", &x,&y);
printf("\nThe product of %3.2lf and %3.2lf is: %3.2lf.",x,y,x*y);
if (y!=0) {
printf("\nThe Quotant of %3.2lf and %3.2lf is: %3.2lf.",x,y,x/y);
}
else
{
printf("\nError: Dividing by Zero");
}
}
}``````