Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How do I prevent the following C++ code from taking in negative values? Any zero or positive integer or decimal I enter as a user

How do I prevent the following C++ code from taking in negative values? Any zero or positive integer or decimal I enter as a user produces correct results, but when I enter a negative value I want the program to tell the user that what they've entered is not a positive integer or decimal, and to re-attempt their entry.

main driver file:

#include

#include

#include

#include "convert.h"

using namespace std;

int main()

{

string str;

cout <<

"Please enter a string representing a positive integer or decimal: ";

cin >> str;

convertToNumeric ctn(str);

long int integerValue = ctn.getIntValue();

double doubleValue = ctn.getDecValue();

double integral;

/* If the string represents an integer value, adding decimal portion to

* the decimal value.

*/

if (std::modf(doubleValue, &integral) == 0)

{

cout << "Integer Conversion: " << integerValue << endl;

cout << "Decimal Conversion: " << std::fixed << std::setprecision(1)

<< integral <

}

/*

* Else, display the values normally.

*/

else

{

cout << "Integer Conversion: " << integerValue << endl;

cout << "Decimal Conversion: " << doubleValue << endl;

}

/*

* Demonstrating that the converted values are useable with

* long int-specific and double-specific operations.

*/

cout << endl;

cout << "Demonstration of long int: " << integerValue << " + 100 = " <<

(integerValue + 100) << endl;

cout << "Demonstration of decimal: " << doubleValue << " + 100.12 = " <<

(doubleValue + 100.12) << endl;

return 0;

}

convert.cpp file:

#include

#include "convert.h"

using namespace std;

convertToNumeric::convertToNumeric()

{

str = "123";

}

/*

* This accessor function is the overloaded constructor that takes a string

* and calls the convert method to convert into respective integer and

* decimal value. There is not return value. The input is s, which is the

* string to be converted.

*/

convertToNumeric::convertToNumeric(string s)

{

str = s;

if(convert(str))

{

cout << "Conversion Successful!" << endl;

}

else

{

cout << "Conversion failed." <

}

}

/*

* This mutator function serves the purpose of converting a string into its

* respective integer and decimal values. The input is s, which is the

* string to be converted. The function returns a boolean value indicating

* whether conversion is successful or not.

*/

bool convertToNumeric::convert(string s)

{

bool success = false;

bool dotFound = false;

/*

* Here, we are looping through each character in the string and storing

* the integer portion in cstk1 and the decimal portion in cstk2.

* Ex: If s = 123.45, cstk1 = 321 and cstk2 = 54

* (since the stack follows LIFO)

*/

for(unsigned int i = 0; i < s.size(); i++)

{

if(s[i] == '.') dotFound = true;

else if(dotFound)

{

cstk2.push(s[i]);

}

else

{

cstk1.push(s[i]);

}

}

/*

* Here, we are popping out each value from stack cstk1 and generating

* the integer value.

* Ex: s = 123.45 and cstk1 = 321, num = 0

* Iteration 1: num = 0 + (3 * 1) = 3

* Iteration 2: num = 3 + (2 * 10) = 23

* Iteration 3: num = 23 + (1 * 100) = 123

* Now intValue = 123

*/

long int num = 0; int multiplyValue = 1;

char val = '0';

while(cstk1.pop(val))

{

int valInInt = val - '0';

num += (valInInt * multiplyValue);

multiplyValue *= 10;

}

intValue = num;

/*

* Here, we are popping out each value from stack cstk2 and generating

* the decimal value.

* Ex: s = 123.45 and cstk2 = 54, num = 0, doubleValue = 123

* (equated to integer value)

* Iteration 1: num = 0 + (5 * 1) = 5

* Iteration 2: num = 5 + (4 * 10) = 45

* Iteration 3: num = 45 / 100 = 0.45

* Now doubleValue = 123 + 0.45 = 123.45

*/

doubleValue = double(num);

num = 0; multiplyValue = 1;

while(cstk2.pop(val))

{

int valInInt = val - '0';

num += (valInInt * multiplyValue);

multiplyValue *= 10;

}

doubleValue += (double(num) / multiplyValue);

success = true;

return success;

}

/*

* The accessor function below is returning the integer value of the string

* that has been converted.

*/

long int convertToNumeric::getIntValue()

{

return intValue;

}

/*

* The accessor function below is returning the double value of the string

* that has been converted.

*/

double convertToNumeric::getDecValue()

{

return doubleValue;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning ASP.NET 4.5 Databases

Authors: Sandeep Chanda, Damien Foggon

3rd Edition

1430243805, 978-1430243809

More Books

Students also viewed these Databases questions

Question

Why are dummy activities used in a CPM/PERT network?

Answered: 1 week ago