Mathematics 101 using C - 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: ");
}

Related

Yes/No loop using while loop in C with a math operation program

First of all, I am a total beginner to both C (and any programming) and Stack Overflow, so sorry if something like this has been asked before.
So I've been having trouble with my math operations code in the loop part. When I type in N or Y, the program acts as if I typed in a different option and says I have to type in Y or N.
Here's the code. Sorry if it's a jumbled mess, this is all I know so far.
#include <stdio.h>
int main() {
while (1) {
int choice1, choice2, num1, num2;
printf("\n [1] Addition\n [2] Subtraction\n [3] Multiplication\n [4] Division\n");
printf("\n Pick a choice: ");
scanf("%d", &choice1);
printf("\n Give a number: ");
scanf("%d", &num1);
printf("\n Give another number: ");
scanf("%d", &num2);
switch (choice1) {
case 1:
printf("Sum is %d", num1+num2);
break;
case 2:
printf("Difference is %d", num1-num2);
break;
case 3:
printf("Product is %d", num1*num2);
break;
case 4:
printf("Quotient is %d", num1/num2);
break;
default:
printf("Please select a valid operation");
}
printf("\n Try another operation [y/n]: ");
scanf("%d", &choice2);
if (choice2 == 'y' || choice2 == 'Y') {
printf("Retrying...");
break; }
else if (choice2 == 'n' || choice2 == 'N') {
printf("Exiting...");
break; }
else {
printf("Pick y or n only");
break;
}
}
return 0;
}
the issue here is that in the if statement you are using break which breaks the flow, instead you should be using continue
major changes: (I've added comment line to make the changes more obvious for you)
int check = 1; //added a check variable
while (check) { //used check variable for while loop
printf("\n Try another operation [y/n]: ");
scanf("%c", &choice2); //changed %d to %c as the input is a char
if (choice2 == 'y' || choice2 == 'Y') {
printf("Retrying...");
continue; } //changed break with continue
else if (choice2 == 'n' || choice2 == 'N') {
printf("Exiting...");
check= 0; } //updated check so next iteration fails and terminates the loop
else {
printf("Error wrong input");
check= 0; //there cannot be a 2nd try using if else statement
}
there will be few more changes which I'll mark in the code below
#include <stdio.h>
int main() {
int check = 1; //added a check variable
while (check) { //used check variable for while loop
int choice1, choice2, num1, num2;
printf("\n [1] Addition\n [2] Subtraction\n [3] Multiplication\n [4] Division\n");
printf("\n Pick a choice: ");
scanf("%d", &choice1);
printf("\n Give a number: ");
scanf("%d", &num1);
printf("\n Give another number: ");
scanf("%d", &num2);
switch (choice1) {
case 1:
printf("Sum is %d", num1+num2);
break;
case 2:
printf("Difference is %d", num1-num2);
break;
case 3:
printf("Product is %d", num1*num2);
break;
case 4:
printf("Quotient is %d", num1/num2);
break;
default:
printf("Please select a valid operation");
}
printf("\n Try another operation [y/n]: ");
scanf("%c", &choice2); //changed %d to %c as the input is a char
if (choice2 == 'y' || choice2 == 'Y') {
printf("Retrying...");
continue; } //changed break with continue
else if (choice2 == 'n' || choice2 == 'N') {
printf("Exiting...");
check= 0; } //updated check so next iteration fails and terminates the loop
else {
printf("Error wrong input");
check= 0; //there cannot be a 2nd try using if else statement
}
}
return 0;
}
for more detail, you can read the docs
I think you will have to do some major changes as shown below
#include <stdio.h>
int main() {
while (1) {
int choice1, num1, num2;
char choice2;
printf("\n [1] Addition\n [2] Subtraction\n [3] Multiplication\n [4] Division\n");
printf("\n Pick a choice: ");
scanf("%d", &choice1);
printf("\n Give a number: ");
scanf("%d", &num1);
printf("\n Give another number: ");
scanf("%d", &num2);
switch (choice1) {
case 1:
printf("Sum is %d", num1+num2);
break;
case 2:
printf("Difference is %d", num1-num2);
break;
case 3:
printf("Product is %d", num1*num2);
break;
case 4:
printf("Quotient is %d", num1/num2);
break;
default:
printf("Please select a valid operation");
}
printf("\n Try another operation [y/n]: ");
scanf("%s", choice2);
if (choice2 == 'y' || choice2 == 'Y')
{
scanf("%d", &choice1);
}
else;
{
printf("Exiting...");
}
}
return 0;
}

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?

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
}

LINK error 2019

i was writing a program in c and i used the function flushall_
that is used in visual 2013 express and it kept giving me the error :
Error 1 error LNK2019: unresolved external symbol _flushall_ referenced in function _main C:\Users\Naya123\Desktop\proj1\proj1\q1.obj proj1
i also tryed copying the code into a new project and also i delete the function flushall_ from the code but still the same
the code (its only the main ) :
#include<stdio.h>
#include<math.h>
#include<string.h>
void main()
{
char option; int num1, num2, num3,count=0,num,base;
menue();
scanf_s("%c",&option);
while (option != 'Y' || option != 'y')
{
switch (option)
{
case 'a' :
case 'A':
printf("insert first number : "); scanf_s("%d", &num1);
printf("insert seconde number : "); scanf_s("%d", &num2);
printf("insert third number : "); scanf_s("%d", &num3);
printf("the max between these three is %d \n ", max(num1, num2, num3));
break;
case 'B' :
case 'b':
printf("insert first number : "); scanf_s("%d", &num1);
printf("insert seconde number : "); scanf_s("%d", &num2);
printf("insert third number : "); scanf_s("%d", &num3);
printf("the min \n");
//printf("the min between these three is %d \n ", min(num1, num2, num3));
break;
case 'C':
case 'c':
printf("insert number : "); scanf_s("%d", &num);
if (is_positive(num) == ERROR)
{
printf("Error back to menue \n");
break;
}
else {
printf("the oposite of the number is %d \n ", oposite(num));
break;
}
case 'D':
case 'd':
printf("insert number : "); scanf_s("%d", &num);
if (is_positive(num) == ERROR)
{
printf("Error back to menue \n");
break;
}
else {
printf("the oposite of the number is %d \n ", (is_polendrom(num)== 0) ? "the number isn't a polindrom \n" : "the number is a polindrom \n");
break;
}
case 'E':
case 'e':
printf("insert number : "); scanf_s("%d", &num);
if (is_positive(num) == ERROR)
{
printf("Error back to menue \n");
break;
}
else {
printf("insert the base: "); scanf_s("%d", &base);
printf("the number in a detcimal base is %d \n ", numByBase10(num,base));
break;
}
case 'F':
case 'f':
printf("insert number : "); scanf_s("%d", &num);
if (num < 0)
num = (make_bolindrom(-1 * num))*-1;
else num = make_bolindrom(num);
break;
case 'G':
case 'g' :
printf("DoYou Really want to quit? y/n \n");
option=getchar(); enter=flushall_();
if (option == 'Y' || option == 'y')
printf("thx for using the system ! byee \n");
else
menue();
break;
}
if (option != 'Y' || option != 'y')
{
menue();
scanf_s("%c",&option);
}
}
}
The function is named _flushall, not flushall_.
Besides, you should enable a higher warning level to get warnings if you call functions that are not declared. This would have helped you detect the cause of the problem.

if statement not working properly? [closed]

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);
}

Resources