if statement not working properly? [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
When i run the program, if i enter 0 for the "d" option, it doesn't print what i set the if statement to print when a 0 is entered as u can see almost at the end of the program. The comments in this program are part of the program i just made it like this so you can see the code that is not in comment form is my problem.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
main()
{
// David Brand
int result, num, num1, calc, calc1, mark;
char A1, A2, ch;
float num2, num3, result1;
printf("\n\n\tMenu");
printf("\n\n\ta. Addition");
printf("\n\tb. Subtraction");
printf("\n\tc. Multiplication");
printf("\n\td. Division");
printf("\n\n\tSelect a, b, c or d: ");
ch = getch();
if (ch == 'a')
{
printf("\n\n\tEnter a number ");
scanf("%d", &num);
printf("\n\n\tEnter another number ");
scanf("%d", &num1);
printf("\n\n\tPlease add the two numbers ");
scanf("%d", &result);
if (result == num + num1)
printf("\n\n\tCorrect");
if (result != num + num1)
printf("\n\n\tWrong");
}
if (ch == 'b')
{
printf("\n\n\tEnter a number ");
scanf("%d", &num);
printf("\n\n\tEnter another number ");
scanf("%d", &num1);
printf("\n\n\tPlease subtract from the first number ");
scanf("%d", &result);
if (result == num - num1)
printf("\n\n\tCorrect");
if (result != num - num1)
printf("\n\n\tWrong");
}
if (ch == 'c')
{
printf("\n\n\tEnter a number ");
scanf("%d", &num);
printf("\n\n\tEnter another number ");
scanf("%d", &num1);
printf("\n\n\tPlease multiply the two ");
scanf("%d", &result);
if (result == num * num1)
printf("\n\n\tCorrect");
if (result != num * num1)
printf("\n\n\tWrong");
}
if (ch == 'd')
{
printf("\n\n\tEnter a number ");
scanf("%f", &num2);
if (num2 != 0)
printf("\n\n\tEnter another number ");
scanf("%f", &num3);
printf("\n\n\tPlease divide the two numbers ");
scanf("%f", &result1);
if (num2 == 0)
printf("\n\n\tZero divisor");
printf("\n\tHit a key to end the program");
getch();
system("cls");
exit(0);
}
if (result1 == num2 / num3)
printf("\n\n\tCorrect");
if (result1 != num2 / num3)
printf("\n\n\tWrong");
getch();
system("cls");
}

Change your code to
if(ch=='d')
{
printf("\n\n\tEnter a number ");
scanf("%f", &num2);
if(num2==0)
{
printf("\n\n\tZero divisor");
printf("\n\tHit a key to end the program");
getch();
system("cls");
exit(0);
}
else if(num2!=0)
{
printf("\n\n\tEnter another number ");
scanf("%f", &num3);
printf("\n\n\tPlease divide the two numbers ");
scanf("%f", &result1);
}
}

Here is that section of code after running through auto-layout:
if(ch=='d')
{
printf("\n\n\tEnter a number ");
scanf("%f", &num2);
if(num2!=0)
printf("\n\n\tEnter another number ");
scanf("%f", &num3);
printf("\n\n\tPlease divide the two numbers ");
scanf("%f", &result1);
if(num2==0)
printf("\n\n\tZero divisor");
printf("\n\tHit a key to end the program");
getch();
system("cls");
exit(0);
}
It doesn't print anything after you enter 0 because it falls straight through to another scanf. If you want more than one statement to be conditionally executed with if, you need to enclose those statements in curly brackets.
I expect you wanted something like this:
if(ch=='d')
{
printf("\n\n\tEnter a number ");
scanf("%f", &num2);
if(num2!=0) {
printf("\n\n\tEnter another number ");
scanf("%f", &num3);
printf("\n\n\tPlease divide the two numbers ");
scanf("%f", &result1);
} else {
printf("\n\n\tZero divisor");
printf("\n\tHit a key to end the program");
getch();
system("cls");
exit(0);
}
}

I don't have a c compiler at work but I think this should fix most of the issues your having
Also don't you need to check for num3 to be 0 not num2? 0 / 5 is fine 5 / 0 is not
You don't need to check for == and then immediately check for != you know from the == that the opposite of that using else is !=
You should be able to input the two numbers or even the answer as well into a single line to make it easier on the user using a single scanf line also you could make part of this into functions as there is alot of repeated code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
main()
{
//David Brand
int result,num,num1,calc,calc1,mark;
char A1,A2,ch;
float num2,num3,result1;
printf("\n\n\tMenu");
printf("\n\n\ta. Addition");
printf("\n\tb. Subtraction");
printf("\n\tc. Multiplication");
printf("\n\td. Division");
printf("\n\n\tSelect a, b, c or d: ");
ch=getch();
if(ch=='a')
{
printf("\n\n\tEnter two numbers separated by a space i.e. 5 7");
scanf("%d %d", &num, &num1);
printf("\n\n\tPlease add the two numbers ");
scanf("%d", &result);
if (result == num + num1)
printf("\n\n\tCorrect");
else
printf("\n\n\tWrong");
}
else if(ch=='b')
{
printf("\n\n\tEnter two numbers separated by a space i.e. 5 7");
scanf("%d %d", &num, &num1);
printf("\n\n\tPlease subtract from the first number ");
scanf("%d", &result);
if(result == num-num1)
printf("\n\n\tCorrect");
else
printf("\n\n\tWrong");
}
else if(ch=='c')
{
printf("\n\n\tEnter two numbers separated by a space i.e. 5 7");
scanf("%d %d", &num, &num1);
printf("\n\n\tPlease multiply the two ");
scanf("%d", &result);
if(result == num * num1)
printf("\n\n\tCorrect");
else
printf("\n\n\tWrong");
}
else if(ch=='d')
{
printf("\n\n\tEnter two numbers separated by a space i.e. 5.7 7.5");
scanf("%f %f", &num2, &num3);
if(num3 == 0)
{
printf("\n\n\tZero divisor");
}
else
{
printf("\n\n\tPlease divide the two numbers ");
scanf("%f", &result1);
if(result1 == (num2 / num3))
printf("\n\n\tCorrect");
else
printf("\n\n\tWrong");
}
}
printf("\n\tHit a key to end the program");
getch();
exit(0);
}

Related

Calculator code wont work at all despite compiler showing 0 warnings [duplicate]

This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 1 year ago.
I was doing some amateur C practice. As you can see I'm trying to make a slightly more advanced calculator that allows the user to decide what they want to do. Even though I clearly defined what to write in if() function, it wont work at all. I would type in "add" or "mul" in the console but it always returns, "type a given mathematical function"
which is an outcome that I put in there to tell the user that they've given the wrong input. So far I've tried adding and removing the quotation mark in if() and replaced == with =. None of these work.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1;
int num2;
int mfunc;
printf("type in a mathematical function: add, subtract, divide or multiply ");
scanf_s("%d", &mfunc);
if (mfunc == "add") {
printf("enter first number: ");
scanf_s("%d", &num1);
printf("enter a second number: ");
scanf_s("%d", &num2);
printf("sum of two number is: %d", num1 + num2);
}
else if (mfunc == "sub") {
printf("enter first number: ");
scanf_s("%d", &num1);
printf("enter a second number: ");
scanf_s("%d", &num2);
printf("difference is: %d", num1 - num2);
}
else if (mfunc == "div") {
printf("enter first number: ");
scanf_s("%d", &num1);
printf("enter a second number: ");
scanf_s("%d", &num2);
printf("quotent is: %d\n", num1 / num2);
}
else if (mfunc == "mul") {
printf("enter first number: ");
scanf_s("%d", &num1);
printf("enter a second number: ");
scanf_s("%d", &num2);
printf("product is: %d", num1 * num2);
}
else {
printf("type a given mathematical function");
}
return 0;
}
you can't compare "add" with myfunc. add is a string (array of chars of size 4), myfunc is an int. In C you can't compare int to string. you can compare string to string so you can ask from the user to input his func by write "add", but it is a bit more complex for you right now (you need to scan it to a proper array of chars and compare the strings using function which might be case sensitive, etc.). also, you can use instruction for the user - some kind of map so the user could insert 0 for add, 1 for sub and so on..
also, I used "define" witch is macro (an pre-processor directive) to more readable code;
#include <stdio.h>
#include <stdlib.h>
#define ADD 0
#define SUB 1
#define DIV 3
#define MUL 4
int main()
{
int num1;
int num2;
int mfunc;
printf("type in a mathematical function: add, subtract, divide or multiply \n");
printf("to add insert 0,\nto subtract insert 1,\n");
printf("to divide insert 2,\nto multiply insert 3\n");
scanf("%d", &mfunc);
if (mfunc == ADD) {
printf("enter first number: ");
scanf("%d", &num1);
printf("enter a second number: ");
scanf("%d", &num2);
printf("sum of two number is: %d", num1 + num2);
}
else if (mfunc == SUB) {
printf("enter first number: ");
scanf("%d", &num1);
printf("enter a second number: ");
scanf("%d", &num2);
printf("difference is: %d", num1 - num2);
}
else if (mfunc == DIV) {
printf("enter first number: ");
scanf("%d", &num1);
printf("enter a second number: ");
scanf("%d", &num2);
printf("quotent is: %d\n", num1 / num2);
}
else if (mfunc == MUL) {
printf("enter first number: ");
scanf("%d", &num1);
printf("enter a second number: ");
scanf("%d", &num2);
printf("product is: %d", num1 * num2);
}
else {
printf("type a given mathematical function");
}
return 0;
}
if you really want the user to input string you should be do that:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int num1;
int num2;
char mfunc[256];
printf("type in a mathematical function: add, subtract, divide or multiply \n");
gets(mfunc);
if (strcmp(mfunc, "ADD") == 0||strcmp(mfunc, "add") == 0) {
printf("enter first number: ");
scanf("%d", &num1);
printf("enter a second number: ");
scanf("%d", &num2);
printf("sum of two number is: %d", num1 + num2);
}
else if (strcmp(mfunc, "SUB") == 0||strcmp(mfunc, "sub") == 0) {
printf("enter first number: ");
scanf("%d", &num1);
printf("enter a second number: ");
scanf("%d", &num2);
printf("difference is: %d", num1 - num2);
}
else if ((strcmp(mfunc, "DIV") == 0||strcmp(mfunc, "div") == 0)) {
printf("enter first number: ");
scanf("%d", &num1);
printf("enter a second number: ");
scanf("%d", &num2);
printf("quotent is: %d\n", num1 / num2);
}
else if ((strcmp(mfunc, "MUL") == 0||strcmp(mfunc, "mul") == 0)) {
printf("enter first number: ");
scanf("%d", &num1);
printf("enter a second number: ");
scanf("%d", &num2);
printf("product is: %d", num1 * num2);
}
else {
printf("type a given mathematical function");
}
return 0;
}
I know I used gets(), see paul-kapustin in this Q.
How to read a line from the console in C?

Why is my program printing 0 instead of the intended value? (C) [duplicate]

This question already has answers here:
Reading in double values with scanf in c
(7 answers)
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 2 years ago.
I'm trying to make a calculator program for my class (not fully completed as you can tell) and I'm running into a problem with the Multiplication and Division section. They both print out 0 instead of their intended value, but Addition and Subtraction work fine. Any help?
int choice = 8;
double numberone;
double numbertwo;
while(choice > 7 || choice < 1){
printf("(1) Addition \n(2) Subtraction \n(3) Multiplication \n(4) Division \n(5) Modulus (integers only) \n(6) Test if prime (integers only) \n(7) Exit \nPlease choose an operation: \n");
scanf(" %i", &choice);
if (choice != 5 || choice!= 6){
printf("Enter the first number: ");
scanf(" %d", &numberone);
printf("Enter the second number: ");
scanf(" %d", &numbertwo);
if (choice == 1){
double sum = numberone + numbertwo;
printf("Sum: %d", sum);
}
else if (choice == 2){
double dif = numberone - numbertwo;
printf("Difference: %d", dif);
}
else if (choice == 3){
double pro = numberone * numbertwo;
printf("Product: %d", pro);
}
else if (choice == 4){
double quo = numberone / numbertwo;
printf("Quotient: %d", quo);
}
}
}
}
You use %d in your printf which converts your results to an Integer. Use %lf instead.
// ...
else if (choice == 3){
double pro = numberone * numbertwo;
printf("Product: %lf", pro);
}
else if (choice == 4){
double quo = numberone / numbertwo;
printf("Quotient: %lf", quo);
// ...
EDIT:
The same applies to your scanf. Use %lf instead of %i:
// ...
printf("Enter the first number: ");
scanf(" %lf", &numberone);
printf("Enter the second number: ");
scanf(" %lf", &numbertwo);
// ...

Mathematics 101 using C

Can you help me with the code below? I am trying to make a program where users need to choose the following options:
1 - Multiplication table
2- Test
When the user chooses either one and it's finished running, the program will prompt users to choose the following:
1 - Another multiplication table
2 - Another test
3 - Exit
Problem occurs when I choose the 2nd option. Whenever I try to exit (3rd option), it goes back to 1st option. Everything is normal when I choose the 1st option and then the 3rd option. How do I solve this?
Here's my code:
int c1;
int num;
int multi;
int num1, num2;
int answer;
printf("1 - Multiplication table\n");
printf("2 - Test\n");
printf("Choice: ");
scanf("%d", &c1);
while (c1 != 1 && c1 != 2)
{
printf("\nInvalid selection. Please choose again.\n");
printf("1 - Multiplication table\n");
printf("2 - Test\n");
printf("Choice: ");
scanf("%d", &c1);
}
if (c1 == 1)
{
do
{
printf("\nChoose a number ranging from 1 to 12.\n");
printf("Choice: ");
scanf("%d", &num);
while (num > 12)
{
printf("\nInvalid selection. Please choose again.\n");
printf("Choose a number ranging from 1 to 12.\n");
printf("Choice: ");
scanf("%d", &num);
}
printf("\nMultiply of %d\n", num);
for (multi = 1; multi <= 12; multi++)
{
printf("%d x %d = %d\n", num, multi, num * multi);
}
printf("\nNext program?\n");
printf("1 - Another multiplication table\n");
printf("2 - Test\n");
printf("3 - Exit\n");
printf("Choice: ");
scanf("%d", &c1);
while (c1 != 1 && c1 != 2 && c1 != 3)
{
printf("\nInvalid selection. Please choose again.\n");
printf("1 - Another multiplication table\n");
printf("2 - Test\n");
printf("3 - Exit\n");
printf("Choice: ");
scanf("%d", &c1);
}
} while (c1 == 1);
}
if (c1 == 2)
{
do
{
printf("\nEnter 1st number: ");
scanf("%d", &num1);
printf("Enter 2nd number: ");
scanf("%d", &num2);
printf("Your answer is: ");
scanf("%d", &answer);
if (answer == num1 * num2)
{
printf("\nYou are correct. The answer for %d times %d is %d.", num1, num2, num1 * num2);
}
else if (answer != num1 * num2)
{
printf("\nYou are incorrect. The answer for %d times %d is %d, not %d.", num1, num2,
num1 * num2, answer);
}
printf("\nNext program?\n");
printf("1 - Multiplication table\n");
printf("2 - Test\n");
printf("3 - Exit\n");
printf("Choice: ");
scanf("%d", &c1);
while (c1 != 1 && c1 != 2 && c1 != 3)
{
printf("\nInvalid selection. Please choose again.\n");
printf("1 - Multiplication table\n");
printf("2 - Test\n");
printf("3 - Exit\n");
printf("Choice: ");
scanf("%d", &c1);
}
do
{
printf("\nChoose a number ranging from 1 to 12.\n");
printf("Choice: ");
scanf("%d", &num);
while (num > 12)
{
printf("\nInvalid selection. Please choose again.\n");
printf("Choose a number ranging from 1 to 12.\n");
printf("Choice: ");
scanf("%d", &num);
}
printf("\nMultiply of %d\n", num);
for (multi = 1; multi <= 12; multi++)
{
printf("%d x %d = %d\n", num, multi, num * multi);
}
printf("\nNext program?\n");
printf("1 - Another multiplication table\n");
printf("2 - Test\n");
printf("3 - Exit\n");
printf("Choice: ");
scanf("%d", &c1);
while (c1 != 1 && c1 != 2 && c1 != 3)
{
printf("\nInvalid selection. Please choose again.\n");
printf("1 - Another multiplication table\n");
printf("2 - Test\n");
printf("3 - Exit\n");
printf("Choice: ");
scanf("%d", &c1);
}
} while (c1 == 1);
} while (c1 == 2);
}
The do..while loop in C executes one time before checking. In the second if: "if (c1 == 2)" your code first performs the test then it asks again to choose. Even if your answer is "3" it executes one time the code in the following do..while section which is actually the code for the "multiplication table".
You can use the while loop instead of the do..while. However the code seems overcomplicated and some sections are repeated I'll suggest you to review the logic (also consider to use the switch statement).
Found the solution. Apparently, do while loop does not work well. Had to change to while loop. Here's the code:
int c1;
int num;
int multi;
int num1,num2;
int answer;
printf("1 - Multiplication table\n");
printf("2 - Test\n");
printf("Choice: ");
scanf("%d",&c1);
while(c1!=1 && c1!=2){
printf("\nInvalid selection. Please choose again.\n");
printf("1 - Multiplication table\n");
printf("2 - Test\n");
printf("Choice: ");
scanf("%d",&c1);
}
while(c1==1 || c1==2){
if(c1==1){
printf("\nChoose a number ranging from 1 to 12.\n");
printf("Choice: ");
scanf("%d",&num);
while(num>12){
printf("\nInvalid selection. Please choose again.\n");
printf("Choose a number ranging from 1 to 12.\n");
printf("Choice: ");
scanf("%d",&num);
}
printf("\nMultiply of %d\n",num);
for(multi=1;multi<=12;multi++){
printf("%d x %d = %d\n",num,multi,num*multi);
}
}else if(c1==2){
printf("\nEnter 1st number: ");
scanf("%d",&num1);
printf("Enter 2nd number: ");
scanf("%d",&num2);
printf("%d times %d equals to: ",num1,num2);
scanf("%d",&answer);
if(answer==num1*num2){
printf("Your answer is correct! %d times %d equals to %d.",num1,num2,num1*num2);
}else if(answer!=num1*num2){
printf("Your answer is incorrect! %d times %d equals to %d, not %d.",num1,num2,num1*num2,answer);
}
}
printf("\n\nNext program?\n");
printf("1 - Another multiplication table\n");
printf("2 - Test\n");
printf("3 - Exit\n");
printf("Choice: ");
scanf("%d",&c1);
while(c1!=1 && c1!=2 && c1!=3){
printf("\nInvalid selection. Please choose again.\n");
printf("1 - Another multiplication table\n");
printf("2 - Test\n");
printf("3 - Exit\n");
printf("Choice: ");
scanf("%d",&c1);
}
}
You shouldn't rely on getting the correct input from scanf(), the safer option is to read the input using fgets() and convert the value to an int using atoi().
int getValidNumberInput(void)
{
char buf[BUFSIZ];
int value = 0;
if (fgets(buf, sizeof(buf), stdin) != NULL)
value = atoi(buf);
return value;
}
The atoi() function returns 0 for a non numeric value so it's perfectly suitable for you needs.
As mentioned in the comments, the logic of the program is easily simplified by using a while() loop and a switch() statement.
int main(void)
{
bool hasLooped = false;
bool invalidChoice = false;
int choice;
while (1)
{
/* Display the relevant menu */
displayProgramMenu(hasLooped, invalidChoice);
/* Get the selected menu choice */
choice = getValidNumberInput();
if (choice < 1 || choice > 3)
invalidChoice = true;
else
invalidChoice = false;
/* Perform the chosen action */
switch (choice)
{
case 1:
doFirstOption();
hasLooped = true;
break;
case 2:
doSecondOption();
hasLooped = true;
break;
case 3:
exit(0);
break;
default:
break;
}
}
}
The use of the boolean variables allows the complexity to be moved into functions where it is easy to reason about.
void displayMenu(bool hasLooped, bool showInvalidMsg)
{
if (showInvalidMsg)
printf("\nInvalid selection. Please choose again.\n");
else if (hasLooped)
printf("\nNext program?\n");
if (hasLooped)
printf("1 - Another multiplication table\n");
else
printf("1 - Multiplication table\n");
printf("2 - Test\n");
printf("3 - Exit\n");
printf("Choice: ");
}

y/n loop at the end of a function

good evening,
Here is my code. I am making a little calculator but I'm battling at the end to make the function repeat with a y/n loop. I have looked at others but can't seem to get the right answer.
Thanks.
#include <stdio.h>
int main()
{
int n, num1, num2, result;
char answer;
{
printf("\nWhat operation do you want to perform?\n");
printf("Press 1 for addition.\n");
printf("Press 2 for subtraction.\n");
printf("Press 3 for multiplication.\n");
printf("Press 4 for division.\n");
scanf("%d", &n);
printf("Please enter a number.\n");
scanf("%d", &num1);
printf("Please enter the second number.\n");
scanf("%d", &num2);
switch(n)
{
case 1: result = num1 + num2;
printf("The addition of the two numbers is %d\n", result );
break;
case 2: result = num1 - num2;
printf("The subtraction of the two numbers is %d\n", result );
break;
case 3: result = num1 * num2;
printf("The multiplication of the two numbers is %d\n", result );
break;
case 4: result = num1 / num2;
printf("The division of the two numbers is %d\n", result );
break;
default: printf("Wrong input!!!");
}
printf("\nDo you want to continue, y/n?\n");
scanf("%c", &answer);
while(answer == 'y');
}
return 0;
}
You have this code
char answer;
{
printf("\nWhat operation do you want to perform?\n");
//...
//... more code
//...
printf("\nDo you want to continue, y/n?\n");
scanf("%c", &answer);
while(answer == 'y');
}
Try to change it to:
char answer;
do {
printf("\nWhat operation do you want to perform?\n");
//...
//... more code
//...
printf("\nDo you want to continue, y/n?\n");
scanf("%c", &answer);
} while(answer == 'y');
So the basic form is:
do {
// code to repeat
} while (Boolean-expression);
BTW - You should always check the value returned by scanf
Example:
if (scanf("%c", &answer) != 1)
{
// Add error handling
}
Also notice that you often want a space before %c to remove any white space (including newlines) in the input stream. Like
if (scanf(" %c", &answer) != 1)
{
// Add error handling
}

Calculator in division operation using C [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am making a calculator using C language. But the problem is that I am stuck in division.
I want that when I input 0 divided by 0, it will print undefined and if a number divided by zero will print cannot be divide by zero (which seems I got this).
Here is my code:
#include <stdio.h>
#include <math.h>
main(){
float num1, num2, total;
int a;
char choice;
clrscr();
printf("What operation you want to perform?\n 1. Addition\n 2. Subtraction\n 3. Multiplication\n 4. Division\n");
scanf("%d", &a);
switch(a) {
case 1: //Addition
printf("Addition\n Enter two numbers to be added\n");
scanf("%f%f",&num1, &num2);
total=num1 + num2;
printf("\nThe total is: %f + %f = %f\n", num1, num2, total);
printf("\nDo want to continue? Y or N: ");
scanf("%s", &choice);
if(choice=='y'||choice=='Y')
main();
else
exit(1);
break;
case 2: //Subtraction
printf("Subtraction\n Enter two numbers to be subtract\n");
scanf("%f%f", &num1, &num2);
total=num1 - num2;
printf("\nThe Difference is: %f + %f = %f\n", num1, num2, total);
printf("\nDo you want to continue? Y or N: ");
scanf("%s", &choice);
if(choice=='y'||choice=='Y')
main();
else
exit(1);
break;
case 3: //Multiplication
printf("Multiplication\n Enter two numbers to be multiplied\n");
scanf("%f%f",num1, num2);
total=num1*num2;
printf("\nThe product is: %f * %f = %f\n", num1, num2, total);
printf("\nDo you wish to continue? Y or N: ");
scanf("%s", &choice);
if(choice=='y'||choice=='Y')
main();
else
exit(1);
break;
case 4: //Division
abcd:
printf("Division\n Enter two numbers to be divide\n");
scanf("%f%f",&num1, &num2);
if(num2==0.0)
{ printf("\n %f cannot be divided by zero\n\n", num1); // ;(
goto abcd;}
else if(num1==0.0 && num2==0.0) //This is my problem here.
{printf("\nIts Undefined. 0 / 0\n\n ");
}
total=num1/num2;
printf("\nThe qoutient is %f / %f = %f\n", num1, num2, total);
printf("\nDo you want more? Y or N: ");
scanf("%s", &choice);
if(choice=='y'||choice=='Y')
main();
else
exit(1);
break;
default:
printf("Can you add letters? LOL only numbers");
printf("\nDo you want to continue? Y/N ");
scanf("%s", &choice);
if(choice=='y'||choice=='Y')
main();
else
exit(1);
break;
}
getch();
}
You have:
if(num2==0.0)
// division by zero
else if(num1==0.0 && num2==0.0)
// zero/zero is undefined
The problem is that if both numbers are 0.0, it's caught by the first test, and the second is never executed.
You need to test for both numbers being zero first, and then check whether the second number is zero.
Or you can combine the two tests:
if (num2 == 0.0) {
if (num1 == 0.0) {
// zero / zero
}
else {
// non-zero / zero
}
}
else {
// ok
}
Your if else statements are not ordered correctly.
Always try to put the case that is most specific before the more general cases.
So in your case, the specific case is when num1 == 0.0 && num2 == 0.0
Whereas the general case is num2 == 0.0
Also, your program would crash when both num1 and num2 are zero, seeing as it divides them anyway in that case.

Resources