I wanted to see how I could enhance the efficiency of this program, in terms of conciseness and security.
1. How can I ensure that the user will not break the program by typing a character or a special symbol when I have asked for a number?
2. Are there any functions in C that can recognize if the data that the user has input is a digit or a character?
I am new to programming. Any help would be appreciated. Thank You!
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
#include <math.h>
int main()
{
//Declaring Variables
float num1, num2;
int op;
char ans;
//Getting the user to choose an option
Again:
printf("\n\t\tMENU FOR OPERATIONS:\n ");
printf("\t\t____________________\n\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Modulus\n");
printf("6. Power Function\n\n");
printf("Enter the MENU option: ");
scanf("%d", &op);
while ( op != 1 && op != 2 && op != 3 && op != 4 && op != 5 && op != 6)
{
printf("\nYou entered an invalid MENU option!\n");
printf("Kindly try again.\n\n");
printf("Enter the MENU option: ");
scanf("%d", &op);
}
//Getting the numbers from user
printf("Enter the first number: ");
scanf("%f", &num1);
printf("Enter the second number: ");
scanf("%f", &num2);
//Implementing a switch statement for processing the input
switch(op)
{
//Case for Addition function
case 1:
printf("Result = %.1f\n", num1 num2 );
break;
//Case for Subtraction function
case 2:
printf("Result = %.1f\n", num1 - num2);
break;
//Case for Multiplication function
case 3:
printf("Result = %.1f\n", num1 * num2);
break;
//Case for Division function
case 4:
printf("Result = %.1f\n", num1 / num2 );
break;
//Case for Modulus function
case 5:
printf("Result: %d\n",(int)num1 % (int)num2);
break;
//Case for Power Function
case 6:
printf("Result: %.1f\n", pow(num1, num2));
break;
default:
printf("You entered an invalid operator!\n\n");
break;
}
//Asking the user if he wants to run the program again
printf("\nDo you want to continue (Y/N)? ");
ans = getche();
ans = toupper(ans);
//Implementing a while loop to get the user to enter a valid character
while((ans != 'Y' ) && (ans != 'N'))
{
printf("\n\nYou must type a Y or an N!\n");
printf("Do you want to continue (Y/N)? ");
ans = getche();
ans = toupper(ans);
}
//Implementing an if statement to ascertain whether the user wants to continue or quit
if( ans == 'Y')
{
system("cls");
goto Again; //links the program to the start in case of a YES
}
else
{
exit(1); //Successfully exits the program in case of a NO
}
}
CodePudding user response:
Stop using
scanf()
as a user interface function. It is not designed to be used as such, and you can easily found plenty of reasons why. I personally like this. Use combination offgets()
andsscanf
, if necessary.Yes, in the
ctypes.h
, there are functions likeisalpha
andisdigit
etc.
Additionally, do not use goto
as a flow control element, ever, as it leads to hard to debug, hard to read, hard to follow through spaghetti code. If your program logic is implemented by goto
, that almost always means you didn't think enough on it.
As a side note, by convention most C functions return 0 in case of success, and a non-zero value indicating some sort of error. Here, i,t is not much of a problem, but if you find yourself writing a library or such, you will annoy your users.