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.

I think you are on the right track here:

``````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++;
}``````

I would set up a counter of the number of decimal points before the loop starts and initialize it to zero. I would add an else-if statement between the "if" and "else" statement to deal with the possibility of a decimal point. That else-if code would increment the decimal point counter. It would also check to make sure there has not been more than one decimal point so far in the strong. If there has, flag validData as false:

``````validData = true;
len = strlen(myStr);
i = 0;
int numDecPoints = 0;    // decimal point counter
while ((i < len) && (validData == true))
{
if (i == 0)
{
if (((myStr[i] < '0') || (myStr[i] > '9')) && (myStr[i] != '-'))
{
validData = false;
}
}
else if (myStr[i] == '.')
{
// increment numDecPoints.
// if numDecPoints >= 2, flag validData as false.
}
else
{
if ((myStr[i] < '0') || (myStr[i] > '9'))
{
validData = false;
}
}
i++;
}``````

I am assuming that the first character cannot be a decimal point, but the last can be. If this is not true, you would have to change the code a little bit to reflect exactly what is legal data is. However, the logic of the counter remains the same.

I hope this is a C++ project assignment, you are not using c++ types like std::string which could simplify your coding.

I understand the part about if more than 1 decimal point has was put in flag false, but i dont understand what you mean by increment the decimal counter...

``````//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;
int numDecPoints = 0;
while ((i < len) && (validData == true))
{
if (i == 0)
{
if (((myStr[i] < '0') || (myStr[i] > '9')) && (myStr[i] != '-'))
{
validData = false;
}
}
else
{
if (myStr[i] == '.')
{
//dont understand increment counter
}

if (numDecPoints >= 2)
{
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;
}``````

I understand the part about if more than 1 decimal point has was put in flag false, but i dont understand what you mean by increment the decimal counter...

``````//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;
int numDecPoints = 0;
while ((i < len) && (validData == true))
{
if (i == 0)
{
if (((myStr[i] < '0') || (myStr[i] > '9')) && (myStr[i] != '-'))
{
validData = false;
}
}
else
{
if (myStr[i] == '.')
{
//dont understand increment counter
}

if (numDecPoints >= 2)
{
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;
}``````

By "increment numDecPoints", I mean "increase numDecPoints by 1 when you encounter a decimal point". numDecPoints starts with value 0. After you hit the first decimal point, it would increase to 1. Then the next decimal point encountered would increase it to 2, which would make it illegal, so you would flag validData as false (which you have done). By increment, I mean "add one", as in:

``numDecPoints++;``

or

``numDecPoints = numDecPoints + 1;``

The above two statements are equivalent.

Geese i feel like an idiot, that makes sense. Ill give that a shot...thanks for all of the help so far...

Well i tried the decimal point script but when i run my program i still cannot enter a decimal anywhere. And I dont know where to start with the negative instructions...which are
-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.
-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.

I did the output as double the number that was easy but limiting the integer to one decimal point and only a negative sign being at the beginning is where it is difficult....

``````//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;
int numDecPoints = 0;
while ((i < len) && (validData == true))
{
if (i == 0)
{
if (((myStr[i] < '0') || (myStr[i] > '9')) && (myStr[i] != '-'))
{
validData = false;
}
}
else
{
if (myStr[i] == '.')
{
numDecPoints = numDecPoints + 1;
}

if (numDecPoints >= 2)
{
validData = false;
}

}

{
if ((myStr[i] < '0') || (myStr[i] > '9'))
{
validData = false;
}
}
i++;
}

if (validData == true)
{
myInt = atoi(myStr);
cout << "Twice your number is: " << myInt*2 << endl;
}
else
{
cout << "Number is not Valid!!! Try again... \n";
}

} while (validData == false);

return 0;
}``````

Don't try to do it all at once.

First, check for the negative sign. Make sure there is only one and it's first.
Then check for one decimal.
Last, check that there are only digits, decimal and negative signs in the input.

If you space with consistent indentation, these errors will be easier to spot. Look at the three blocks of code and decide which you want to execute under which circumstances.

``````// start of if block
if (i == 0)
{                      // first block of code
if (((myStr[i] < '0') || (myStr[i] > '9')) && (myStr[i] != '-'))
{
validData = false;
}
}
else
{	  // second block of code
if (myStr[i] == '.')
{
numDecPoints = numDecPoints + 1;
}

if (numDecPoints >= 2)
{
validData = false;
}

}

// end of if block

// 3rd block of code.
// the code below ALWAYS executes.  Do you want it to?
{
if ((myStr[i] < '0') || (myStr[i] > '9'))
{
validData = false;
}
}``````

That is the problem i just dont get. Which block do i put the script in for the individual components. Above all of that the decimal code i put does not even work at all...I have honestly no idea how the heck to write the code to make sure there can only be one negative sign and it always has to be first???? Why is my decimal thing not working in the first place???

``````//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;
int numDecPoints = 0;
while ((i < len) && (validData == true))
{
if (i == 0)
{
if (((myStr[i] < '0') || (myStr[i] > '9')) && (myStr[i] != '-'))
{
validData = false;
}
}
else
{//Dont even know how to write script for neg stuff, but should it go here?
if (myStr[i] == '.')//How come this wont execute, i have moved it around , the code makes sense and it does not acknowledge that i have a decimal point in my integer that i input....
{
numDecPoints = numDecPoints + 1;
}

if (numDecPoints >= 2)
{
validData = false;
}

}

{
if ((myStr[i] < '0') || (myStr[i] > '9'))
{
validData = false;
}
}
i++;
}

if (validData == true)
{
myInt = atoi(myStr);
cout << "Twice your number is: " << myInt*2 << endl;
}
else
{
cout << "Number is not Valid!!! Try again... \n";
}

} while (validData == false);

return 0;
}``````

This seems easy but it just does not register to me, I thought i was getting a grip on this stuff. Boy was i wrong....

Maybe you could use strtod() as a 'workhorse' and switch to using a double instead of integer.
See http://www.cplusplus.com/reference/clibrary/cstdlib/strtod.html
For example,

``````char myStr[30] = "";
while(1)
{
cout << "Enter a decimal number: (q to quit)" << endl;
cin.getline(myStr, 30);

if('q' == *myStr)
{
break;
}

char * endptr = 0;
double dd = strtod (myStr, &endptr);

// in this particular setup, *endptr is only allowed to be newline, nothing else,
// i.e. even an extra space trailing an otherwise valid number invalidates the input ...
if('\n' != *endptr)
{
cout << "Invalid input ..." << endl;
}
else
{
cout << dd << endl;
}
}``````
``````//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;
int numDecPoints = 0;
while ((i < len) && (validData == true))
{
if (i == 0)
{
if (((myStr[i] < '0') || (myStr[i] > '9')) && (myStr[i] != '-'))
{
validData = false;
}
}
else
{//Dont even know how to write script for neg stuff, but should it go here?
if (myStr[i] == '.')//How come this wont execute, i have moved it around , the code makes sense and it does not acknowledge that i have a decimal point in my integer that i input....
{
numDecPoints = numDecPoints + 1;
}

if (numDecPoints >= 2)
{
validData = false;
}

}

{
if ((myStr[i] < '0') || (myStr[i] > '9'))
{
validData = false;
}
}
i++;
}

if (validData == true)
{
myInt = atoi(myStr);
cout << "Twice your number is: " << myInt*2 << endl;
}
else
{
cout << "Number is not Valid!!! Try again... \n";
}

} while (validData == false);

return 0;
}``````

This seems easy but it just does not register to me, I thought i was getting a grip on this stuff. Boy was i wrong....

It may seem like your program is full of bugs, but it actually has very few. You are far closer to success than you realize. Please reread my last post and my earlier one too. The blocks of code that you have written are good. You are just not picking which blocks of code to execute properly. If you look back at my last post, you want to execute one and exactly one of the three blocks of code for each trip through the while loop (one trip through while loop per character). The code you thought is not getting executed is in fact being executed. I have added some cout statements to try to help you see what the program is doing.

You want to redesign your "if" - "else" statement into an "if - "else if" - "else" statement. Below is my modified code with the cout statement and a few comments.

``````//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;
int numDecPoints = 0;
while ((i < len) && (validData == true))
{
if (i == 0)
{
if (((myStr[i] < '0') || (myStr[i] > '9')) && (myStr[i] != '-'))
{
validData = false;
}
}
else
{//Dont even know how to write script for neg stuff, but should it go here?
if (myStr[i] == '.')//How come this wont execute, i have moved it around , the code makes sense and it does not acknowledge that i have a decimal point in my integer that i input....
{

cout << "This block of code is executing 1\n";
numDecPoints = numDecPoints + 1;
}

if (numDecPoints >= 2)
{
cout << "This block of code is executing 2\n";
validData = false;
}

}

{    // currently extra bracket
if ((myStr[i] < '0') || (myStr[i] > '9'))
{
cout << "This block of code executing 3\n";
cout << "Flagging validData false\n";
cout << "numDecPoints = " << numDecPoints << endl;
validData = false;
}
}    // currently extra bracket
i++;
}

if (validData == true)
{
myInt = atoi(myStr);
cout << "Twice your number is: " << myInt*2 << endl;
}
else
{
cout << "Number is not Valid!!! Try again... \n";
}

}
while (validData == false);

return 0;
}``````

I am really getting frustrated. I have been trying different things for the last hour and fifteen minutes and now got to the point where i am staring at the screen...I thought at first i got what you were saying about the if statements, because when i went back and looked at my code it was like, if, if , else if , if, if and ended up confusing myself with what was even happening. I went back and read your postings intently and i just am not picking up the lingo you are putting down...I am really putting up a lot of effort on this stuff and it gets really frustrating when i cannot grasp a concept!!!!....

``````//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;
int numDecPoints = 0;
while ((i < len) && (validData == true))
{
if (i == 0)
{
if (((myStr[i] < '0') || (myStr[i] > '9')) && (myStr[i] != '-'))
{
validData = false;//I get that this block validates the script above, this is the
//first thing in the loop

}
}
else //Else these are to be excecuted...

if (myStr[i] == '.') //I dont understand how to change these ifs, to excecute
//everything one by one...You said that you should excecute
//one thing through one trip through a loop, should i
//create others loops for the other criteria??
{
numDecPoints = numDecPoints + 1;
}

if (numDecPoints >= 2)
{
validData = false;
}

if ((myStr[i] < '0') || (myStr[i] > '9'))
{
validData = false;
}

i++;
}

if (validData == true)
{
myInt = atoi(myStr);
cout << "Twice your number is: " << myInt*2 << endl;
}
else
{
cout << "Number is not Valid!!! Try again... \n";
}

} while (validData == false);

return 0;
}``````

I originally had in mind an "if"-"else if" -"else" statement, with another "if" statement within the "else" statement, but upon rethinking, I think it would be better to have it as an "if" - "else if" - "else if" statement, so I changed it slightly from what I had originally, but it's pretty much the same concept. This is what I had in mind. Try running it.

``````//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;
int numDecPoints = 0;
while ((i < len) && (validData == true))
{
if (i == 0)
{   // code block 1
if (((myStr[i] < '0') || (myStr[i] > '9')) && (myStr[i] != '-'))
{
validData = false;
}
}
else if (myStr[i] == '.')
{   // code block 2
numDecPoints = numDecPoints + 1;

if (numDecPoints >= 2)
{
validData = false;
}
}
else if ((myStr[i] < '0') || (myStr[i] > '9'))
{   // code block 3
validData = false;
}

i++;
}

if (validData == true)
{
myInt = atoi(myStr);
cout << "Twice your number is: " << myInt*2 << endl;
}
else
{
cout << "Number is not Valid!!! Try again... \n";
}

} while (validData == false);

return 0;
}``````

This weeds out the illegal input as far as I have tested it, but test it more thoroughly to make sure it weeds out what you want to have weeded out and nothing more. You are still going to have a problem with decimal numbers like 7.4 in the doubling part of your code. That is due to the fact that you are using the atoi function, which returns an integer:

``int atoi ( const char * str );``

http://www.cplusplus.com/reference/clibrary/cstdlib/atoi.html

So 7.4 is going to get truncated to 7 and doubled to 14. The code I posted is only modified slightly from your post, but hopefully this clarifies what I meant a little. Again, I converted my original idea of an "if" - "else if" - "else" statement slightly.

Ok I got the decimal point script to work by rearranging the else if statements like you suggested. I changed the atoi statement to a atof statement that returns a float and not an integer and now the answer reads correctly. The part about the negative sign, like i said, i just dont understand how to start to write the code for it... how do i write code to allow only one negative sign and it has to be at the beginning to be validated??? I get the just of what you said, i just cannot write the actual code for it....its funny its like you are my new teacher!!! ha ha but really thanks so much for your help. I never thought this stuff could be so confusing...

``````//Assignment 7 :Strings and Numeric input Validation
//By: Curtis Davenport 3/02/08

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
float 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;
int numDecPoints = 0;
while ((i < len) && (validData == true))
{
if (i == 0)
{
if (((myStr[i] < '0') || (myStr[i] > '9')) && (myStr[i] != '-'))
{
validData = false;
}
}
else if (myStr[i] == '.')
{
numDecPoints = numDecPoints + 1;

if (numDecPoints >= 2)
{
validData = false;
}

}
else if ((myStr[i] < '0') || (myStr[i] > '9'))
{
validData = false;
}

i++;
}

if (validData == true)
{
myInt = atof(myStr);
cout << "Twice your number is: " << myInt*2 << endl;
}
else
{
cout << "Number is not Valid!!! Try again... \n";
}

} while (validData == false);

return 0;
}``````

Well, just loop through all but the first character, and make sure it is either a digit or a period. if it is neither(even if it is a negative sign), then it is invalid input.

what you already have does this

``````if (i == 0)
{
if (((myStr[i] < '0') || (myStr[i] > '9')) && (myStr[i] != '-'))
{
validData = false;
}
}
else if (myStr[i] == '.')
{
numDecPoints = numDecPoints + 1;

if (numDecPoints >= 2)
{
validData = false;
}

}``````

it checks if it is in after i = 0, and if it is, it is bad data.

The part about the negative sign, like i said, i just dont understand how to start to write the code for it... how do i write code to allow only one negative sign and it has to be at the beginning to be validated???

Isn't it already implemented in lines 27 and 29, and elsewhere? Try running it with some input. I think you are done if I am understanding the task correctly. I put in the following numbers:

``````--4.5
4-5
-4.5``````

The first two were rejected, the third was accepted. What else is there to do?

yea i am an idiot. For some reason i had something wrong and it was not working...but it works and i think i fulfilled the assignment objectives. Thanks so much for your help, you taught me a lot.