rejlive
New Coder
Hi all
This is my first post and I want to thank you for having a great site.
I am trying to make improvements to my code for a basic calculator but I am not sure how to implement a FILE I/O into it for deleting buffer. The code is below with a FILE I/O statement but it is not properly executed within the source. I am not a programmer I am trying to analyze this for an assignment. The course is closed but I really would like to get the buffer to delete for overflow prevention. This program does not really need this it is just for course requirements.
This is my first post and I want to thank you for having a great site.
I am trying to make improvements to my code for a basic calculator but I am not sure how to implement a FILE I/O into it for deleting buffer. The code is below with a FILE I/O statement but it is not properly executed within the source. I am not a programmer I am trying to analyze this for an assignment. The course is closed but I really would like to get the buffer to delete for overflow prevention. This program does not really need this it is just for course requirements.
C:
#include<stdio.h>
#include<stdlib.h>
// Calculator Using a Union
union Data {
int n;
int m;
};
int main()
{
int a,b;
/* The union assigns a and b to memory*/
union Data data;
data.n = a;
data.m = b;
/* Choosing an operator and create a redundant FILE I/O
for the purpose of the assignment requirements. Since this is a simple calculator
I did not create a write to file so there is no text file */
// Here is the FILE I/O that helps with buffer overflow
int fclose( FILE *fp );
// Choosing an operator
char ch;
printf("Choose Operator\n");
printf("+ for Adding\n");
printf("- for Subtracting\n");
printf("* for Multiplying\n");
printf("/ for Dividing\n");
scanf("%c",&ch);
// Choosing two numbers
printf("Enter any two numbers\n");
scanf("%d%d",&a,&b);
// Switch case is used depending on the operator chosen
switch(ch)
{
case '+':
printf("%d + %d = %d",a,b,(a+b));
break;
case '-':
printf("%d - %d = %d",a,b,(a-b));
break;
case '*':
printf("%d * %d = %d",a,b,(a*b));
break;
case '/':
if(b==0)
/*If 0 is entered for the divisor p2 this error will display*/
{
printf("Divisor cannot be 0");
return 0;
}
printf("%d / %d = %0.2f",a,b,(a/(float)b));
break;
default:
/*If a character other than an operator is entered this
error will display*/
printf("Error! operator is not correct");
}
return 0;
}
Last edited by a moderator: