Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Basic calculator Including a FILE I/O

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.

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:
I'm not sure why you need to "delete a buffer" (which buffer?) and why you would need File I/O for that. This statement
C:
// Here is the FILE I/O that helps with buffer overflow
int fclose( FILE *fp );
does not actually do anything (let alone help). It is just a declaration of this function, in case you might want to call it, which is quite superfluous as this declaration is already in stdio.h.
Did this code ever compile ? It seems to me this statement isn't valid inside a function.

In a real life program you would certainly need to protect the use input buffer against overflow. I have never used scanf for input so I don't know how it behaves if you maliciously type a thousand characters there. Personally I'd use something like for user input
C:
#define MAXLEN 80
char str[MAXLEN];
fgets(str, MAXLEN, stdin);
sscanf(str, ......)
I guess that is what is meant by File I/O here. Hope this helps !
 
I'm not sure why you need to "delete a buffer" (which buffer?) and why you would need File I/O for that. This statement
C:
// Here is the FILE I/O that helps with buffer overflow
int fclose( FILE *fp );
does not actually do anything (let alone help). It is just a declaration of this function, in case you might want to call it, which is quite superfluous as this declaration is already in stdio.h.
Did this code ever compile ? It seems to me this statement isn't valid inside a function.

In a real life program you would certainly need to protect the use input buffer against overflow. I have never used scanf for input so I don't know how it behaves if you maliciously type a thousand characters there. Personally I'd use something like for user input
C:
#define MAXLEN 80
char str[MAXLEN];
fgets(str, MAXLEN, stdin);
sscanf(str, ......)
I guess that is what is meant by File I/O here. Hope this helps !
Thank you this is just an evaluation of code and I appreciate your help. The code you gave was very helpful in helping me understand that there are numerous ways to prevent overflow.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom