I have an assignment to do and i have a good portion of it already complete...The only thing i need is to validate that the number can only have one decimal point and the output of the number that has been validated is multiplied by two.....See the assignment question i posted to give a little more insight to what i have to get done.....here is what i have so far...
//Assignment 7 :Strings and Numeric input Validation
//By: Curtis Davenport 3/02/08
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int myInt;
char myStr[30];
int i,len;
bool validData;
do
{
cout << "Enter a number: ";
cin.getline(myStr,30);
validData = true;
len = strlen(myStr);
i = 0;
while ((i < len) && (validData == true))
{
if (i == 0)
{
if (((myStr[i] < '0') || (myStr[i] > '9')) && (myStr[i] != '-'))
{
validData = false;
}
}
else
{
if ((myStr[i] < '0') || (myStr[i] > '9'))
{
validData = false;
}
}
i++;
}
if (validData == true)
{
myInt = atoi(myStr);
cout << "Your number is: " << myInt << endl;
}
else
{
cout << "Number is not Valid!!! Try again... \n";
}
} while (validData == false);
return 0;
}
-Write a program that asks the user to enter a single real (float) number (positive or negative) and outputs twice the number the user entered.
-The program must validate the input of the number by first storing it into a character string and then verifying that it only contains digits, decimal point, and negative sign.
-Limit the size of the array to 30 characters.
-Verify that the maximum number of decimal points in the string is one (1).
-Verify that the negative sign (if any) can only be at the first position of the string.
-If any of the validation conditions fail, notify the user and request the number again until it is correct.
-Once the string is validated, convert the string to float and output the number multiplied by two.
-There is no requirement for this program to contain additional functions (other than main) or to request the user if he/she would want to enter a number again after validated.