I am trying to get this program to display an error message when the user inputs a zero for the base of an exponent. However, whenever I compile the program the program does not display an error message when I type in zero. I have looked over this code backwards and forwards but still can't find out what is wrong. The line in question is line sixty-four with the expression:
if((num1 == 0)&&(num2 <=0)); {
printf("That is not an acceptable value!\n");
break; }
Can someone please help me? Thanks :)
Code:#include <stdio.h>
#include <math.h>
int menu(void);
int main(void) {
int selection,num1,num2,ans,fans;
selection = menu();
while(selection != 8) {
switch(selection) {
case 1: printf("Enter 1st number: ");
scanf("%d", &num1);
printf("Enter 2nd number: ");
scanf("%d", &num2);
ans = num1 + num2;
printf("%d + %d = %d\n",num1,num2,ans);
break;
case 2: printf("Enter 1st number: ");
scanf("%d", &num1);
printf("Enter 2nd number: ");
scanf("%d", &num2);
ans = num1 - num2;
printf("%d - %d = %d\n",num1,num2,ans);
break;
case 3: printf("Enter 1st number: ");
scanf("%d", &num1);
printf("Enter 2nd number: ");
scanf("%d", &num2);
ans = num1 * num2;
printf("%d * %d = %d\n",num1,num2,ans);
break;
case 4: printf("Enter 1st number: ");
scanf("%d", &num1);
printf("Enter 2nd number: ");
scanf("%d", &num2);
if(num2 == 0) {
printf("Zero is not an acceptable value!\n");
break; }
fans =(float)num1/num2;
printf("%d / %d = %2d\n",num1,num2,fans);
break;
case 5: printf("Enter 1st number: ");
scanf("%d", &num1);
ans = abs(num1);
printf("|%d| = |%d|\n",num1,ans);
break;
case 6: printf("Enter 1st number: ");
scanf("%d",&num1);
if(num1 < 0){
printf("You cannot enter in a negative value!\n");
break; }
fans = sqrt(num1);
printf("sqrt(%d) = %d\n",num1,fans);
break;
case 7: printf("Enter base: ");
scanf("%d", &num1);
printf("Enter exponent: ");
scanf("%d", &num2);
if((num1 == 0)&&(num2 <= 0)) {
printf("That is not an acceptable value!\n");
break; }
fans = pow(num1, num2);
printf("%d ^ %d = %d\n",num1,num2,fans);
break;
default: printf("%d is not a valid selection\n", selection);
}
selection = menu();
}
printf("Bye!\n");
}
int menu(void) {
int choice;
printf("1 Add\n");
printf("2 Subtract\n");
printf("3 Multiply\n");
printf("4 Divide\n");
printf("5 Absolute Value\n");
printf("6 Square Root\n");
printf("7 Powers\n");
printf("8 Quit\n");
printf("Enter a Selection: ");
scanf("%d", &choice);
return choice;
}