How to Reject the Data Which Overflow the Dimensions

104 28
    • 1). Load the C++ IDE by clicking on its program icon. When it opens, select "File/New/Project" and choose "C++ Project" to create a new C++ project. A source code file appears in the text editor portion of the IDE. This file contains an automatically generated "main" function, which serves as an entry point for your program. You will place all your code within the curly brackets that appear immediately below the "main" function.

    • 2). Write the following variable declarations between the curly brackets of the "main" function. The first two variables are used to capture input from the keyboard. The variables' "length" and "width" hold dimensions in a data type called "short," which can only store values up to 32,767 before overflowing. The final variable is used to store the area of the dimensions, and is of type "long." This data type can store values up to 2,147,483,647, which is twice the maximum value possible when multiplying two "shorts" together.

      char *inputString;

      char buffer[256];

      short length = 0;

      short width = 5;

      long area = 0;

    • 3). Write a statement that prints directions to the user to enter the length dimension, like this:

      printf("Enter the length dimension:\n");

    • 4). Write a statement that captures the user input from the keyboard using the variables "inputString" and "buffer," like this:

      inputString = fgets(buffer, 256, stdin);

    • 5). Write an "if" statement that tests to see if the value the user input is larger than what a short is capable of handling. The maximum value of a "short" is stored in the constant "SHRT_MAX." Check to see if the value is positive, since an overflowed value can become negative and dimensions are typically only positive values. The following "if" statement checks for overflow and prints out a message if overflow occurs:

      if(atoi(inputString) > SHRT_MAX && atoi(inputString) > 0)

      { printf("Data Rejected for overflowing dimension\n");}

    • 6). Write an "else" statement that only runs if the preceding "if" statement evaluates as false. This occurs when the value input is within the right range of values for "short" data types.

      else

      {}

    • 7). Write two statements that assigns the "inputString" to the "length" variable and multiplies it with the width variable. Place the following two statements in-between the curly brackets that immediately follow the "else" statement:

      length = atoi(inputString);

      area = length * width;

    • 8). Write a final statement that prints out the area. Place this below the statements written in the last step and in-between the curly brackets of the "else" statement:

      printf("Area: %d", area);

    • 9). Press the green "Play" button to execute the program. A command prompt appears with a line of text asking you to enter a number. Enter in 32,768, which is one higher than "SHRT_MAX," and the following text is output:

      Data Rejected for overflowing dimension

Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.