Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a personal library of data entry functions. Specification Implement the f

ID: 3765683 • Letter: C

Question

Create a personal library of data entry functions.

Specification

Implement the following functions:

bool isValidInt( string str )
Returns true if str can be parsed into a valid int, false otherwise.

int getInt()
Returns a valid integer entered from the keyboard.

bool isValidReal( string str )
Returns true if str can be parsed into a valid double, false otherwise.

double getReal()
Returns a valid real (double) entered from the keyboard.

Each "get" function should do the following:

Prompt the user to enter the appropriate value.

Store the characters entered as a string.

Convert the string to the appropriate numeric value.

If the conversion fails, throw an exception and allow the user to re-enter the value.

Store these functions in namespace "dataChecks" stored in dataChecksNamespace.cpp:

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

namespace dataChecks
{
/*
* parameter numerator
* parameter denominator
* returns the double quotient of integer division
* throws an exception if denominator == 0
*/
double quotient( int numerator, int denominator)
{
if(denominator == 0)
throw new string("divide by zero!");
return static_cast<double>(numerator)/denominator;
}//end quotient()


/*
* parameter str - the string to evaluate
* returns true if the string can be parsed
* into a valid int
*/
bool isvalidInt(string str)
{
int start = 0;
int i; //index of one character in the string
bool valid = true; // assume a valid string
bool sign = false; // assume no sign

// if the length of the string is 0
    //set valid to false
    //return valid

       // if the first character is '-' or '+'
          //set sign to true and start to 1

// if there is not at least one character after the sign
    //set valid to false
    //return valid

        // check if each character in the string is a digit - isdigit(str.at(i))
           // if a character is not a digit, set valid to false

       // return valid;
}

/*
* gets a string from the keyboard
* throws an expection if the string cannot
* be parsed into a valid int
* otherwise returns the int represented by the string
*/

int getAnInt()
{
bool notanint = true;
string svalue;

while (notanint)
{
try
{
// get a string from the keyboard
// if it is not a valid int throw an excepetion
}
catch (string e)
{
// print an error messagecout << "Invalid integer [ " << e << " ] - Please reenter: ";
// send control back to the while statement
}
notanint = false;
}
return atoi(svalue.c_str()); // convert to an integer
}

}//end of dataChecks namespace

Explanation / Answer

namespace dataChecks
{
bool isValidInt( string str )
{
int start = 0;
int i;
bool valid = true;
bool sign = false;
if (int(str.length()) == 0) valid = false;
if (str.at(0) == '-' || str.at(0) == '+')
{
sign = true;
start = 1;
}
if (sign && int(str.length()) == 1) valid = false;
i = start;
while (valid && i < int(str.length()))
{
if (!isdigit(str.at(i))) valid = false;
i++;
}
return valid;
}
int getanInt()
{
bool isvalidInt(string);
bool notanint = true;
string svalue;
while (notanint)
{
try
{
cin >> svalue;
if (!isvalidInt(svalue)) throw svalue;
}
catch (string e)
{
cout << "Invalid integer - Please reenter: ";
continue;
}
notanint = false;
}
return atoi(svalue.c_str());
}
bool isValidReal( string str )
{
int start = 0;
int i;
bool valid = true;
bool sign = false;
if (int(str.length()) == 0) valid = false;
if (str.at(0) == '-' || str.at(0) == '+')
{
sign = true;
start = 1;
}
if (sign && int(str.length()) == 1) valid = false;
i = start;
while (valid && i < int(str.length()))
{
if (!isdigit(str.at(i)) && str.at(i) == '.') valid = true;
i++;
}
return valid;
}
double getaReal()
{
bool isvalidReal(string);
bool notadouble = true;
string svalue;
while (notadouble)
{
try
{
cin >> svalue;
if (!isvalidReal(svalue)) throw svalue;
}
catch (string e)
{
cout << "Invalid Double - Please reenter: ";
continue;
}
notadouble = false;
}
return atof(svalue.c_str());
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote