I have an assignment due in Programming 1 and I need a little bit of help. I have already created the program but now I have one last thing to do. However, I don't quite understand what my professor wants me to do. Here are the instructions she gave me:
"Use a single subprogram to prompt and read user's input for the numbers to be used in the calculations.
Note: It must be used to get all input for calculations."
I am not sure what that means.
Here is the code:
Code:
#include <stdio.h>
#include <math.h>
/*
Stephen Haroldson
11:00am
*/
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);
if((num1 == 0)||(num2 <= 0)) {
printf("That is not an acceptable value!\n");
break; }
printf("Enter exponent: ");
scanf("%d", &num2);
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;
}
If anyone can help me I would appreciate it a lot