I was assigned to create a program prompts a user to input the height and radius of a fuel tank. The program will then calculate the time it takes for the tank to drain and print out a table of results. I need to validate the input of height and radius, and data must be of type double, using a for loop can someone help?
///////////////////////////////////////////////////////////////////////////////
// Header Files...
///////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
///////////////////////////////////////////////////////////////////////////////
// Namespaces used....
///////////////////////////////////////////////////////////////////////////////
using namespace std;
///////////////////////////////////////////////////////////////////////////////
// Type Definitions...
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Function prototypes...
///////////////////////////////////////////////////////////////////////////////
string inputNumericString( double max_length);
// inputs a numeric string from the user
string inputNumericString2( double max_length);
// inputs a numeric string from the user
///////////////////////////////////////////////////////////////////////////////
//
// FUNCTION NAME: main
// PARAMETERS: None
// RETURN TYPE: int
// PURPOSE: Entry point for the application.
//
///////////////////////////////////////////////////////////////////////////////
int main()
{
// perform input of the height...
string numeric_string = inputNumericString(3);
//perform input of tank radius...
string numeric_string2 = inputNumericString2(2);
// exit the program with success (0 == success)
return 0;
}
///////////////////////////////////////////////////////////////////////////////
//
// FUNCTION NAME: input_height_of_tank
// PARAMETERS: height
// RETURN TYPE: double
// PURPOSE: Validates user input of variable height
//
///////////////////////////////////////////////////////////////////////////////
string inputNumericString( double max_length )
{
string input_string = ""; // stores the user data
int count = 0; // counter variable
bool user_input_is_invalid = true; // flag variable
while ( user_input_is_invalid )
{
// prompt the user..
cout << "Enter a height, no more than "
<< max_length << " digits long: ";
// input the string...
cin >> input_string;
// Validation check:
// (1) No more than "max_length" characters?
// (2) Each character is a digit?
if ( input_string.length() > max_length )
{
// inform the user...
cout << "ERROR! Your height must be " << max_length
<< " characters or less. Please try again." << endl;
// raise the red flag...
user_input_is_invalid = true;
}
else
{
// Here we want to check to see if the string contains only
// digits. Remember that a string stores ASCII CHARACTERS,
// not numbers; therefore, checking the string involves
// looking at each character and seeing if the character is
// one of the ASCII values for the 10 digits.
// count the number of characters that are in the range [0-9]. This
// uses built-in C++ functions to make it happen...
count =
(int) count_if (input_string.begin(), input_string.end(), isdigit);
// the count of digits should equal the length of the string...
if ( count != input_string.length() )
{
// inform the user...
cout << "ERROR! Your string can only contain digits [0-9]."
<< " Please try again." << endl;
// raise the red flag...
user_input_is_invalid = true;
}
else
{
// everything is OK...
user_input_is_invalid = false;
}
}
}
// return the string to the calling function...
return input_string;
}
///////////////////////////////////////////////////////////////////////////////
//
// FUNCTION NAME: input_tank_radius
// PARAMETERS: raidus
// RETURN TYPE: double
// PURPOSE: Validates user input of variable height
//
///////////////////////////////////////////////////////////////////////////////
string inputNumericString2( double max_length )
{
string input_string = ""; // stores the user data
int count = 0; // counter variable
bool user_input_is_invalid = true; // flag variable
while ( user_input_is_invalid )
{
// prompt the user..
cout << "Enter a radius, no more than "
<< max_length << " digits long: ";
// input the string...
cin >> input_string;
// Validation check:
// (1) No more than "max_length" characters?
// (2) Each character is a digit?
if ( input_string.length() > max_length )
{
// inform the user...
cout << "ERROR! Your radius must be " << max_length
<< " characters or less. Please try again." << endl;
// raise the red flag...
user_input_is_invalid = true;
}
else
{
// Here we want to check to see if the string contains only
// digits. Remember that a string stores ASCII CHARACTERS,
// not numbers; therefore, checking the string involves
// looking at each character and seeing if the character is
// one of the ASCII values for the 10 digits.
// count the number of characters that are in the range [0-9]. This
// uses built-in C++ functions to make it happen...
count =
(int) count_if (input_string.begin(), input_string.end(), isdigit);
// the count of digits should equal the length of the string...
if ( count != input_string.length() )
{
// inform the user...
cout << "ERROR! Your string can only contain digits [0-9]."
<< " Please try again." << endl;
// raise the red flag...
user_input_is_invalid = true;
}
else
{
// everything is OK...
user_input_is_invalid = false;
}
}
}
// return the string to the calling function...
return input_string;
}
First I want to commend you on your excellent use of comments.
Now, in my opinion you have some major code-duplication here. You should endeavour to turn your two utility methods into one, as they're essentially doing the same thing (except for a length parameter and descriptive information like 'radius' etc).
Also consider what happens when the user enters zero for both fields. Is this behaviour that you want?
Apart from that, I don't see much wrong with your code.