-
Reading errors in floats
The goal is to write program asking for the user for a float, and to put in safety nets against in-proper entries.
The following code allows the user to enter both positive and negative integers and floats, but the program won't give the error message for certain types of input errors.
92.64.47 will be displayed as 92.64
864hj will be displayed as 864.
Whenever the code reaches an error in having the input be an integer or float, it doesn't give an error message, it just stops the string right there.
Suggestions on where/what my problem is and how I might fix it?
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
float myFloat;
char myStr[30];
bool validData;
int i, len /* length of string */;
do
{
cout << "Enter a float number: ";
cin.getline(myStr,30);
validData = true;
len = strlen(myStr);
i=0;
while ((i < len) && (validData == true))
{
if ((i == 0) && ((myStr[i] < '0') || (myStr[i] > '9')) && (myStr[i] != '-') && (myStr[i] != '.')) //allows for +- integers and floats
{
cout << "if loop checking\n";
validData = false;
cout << "if loop checked\n";
}
i++;
}
if (validData == true)
{
myFloat = atof(myStr);
cout << "Your float is: " << myFloat << "\n";
}
else
{
cout << "Incorrect Data. Try again!!!\n";
}
} while (validData == false);
return 0;
}
-
This is C++ not C. ...and your problem is, that's how cin works. It starts from the beginning and gets all the pertainent data it can. Then it stops. If it doesn't get anything, it fails.
Read in as a string then use string streams to convert to whatever you want. Voila.
-
By bad on the wrong forum, duly noted for any future problems.
Thanks for the quick reply.
-
http://www.hmug.org/man/3/strtod.php
Code:
cin.getline(myStr,30);
char *end;
double d = strtod ( myStr, &end );
if ( end == myStr ) {
// nothing converted
} else
if ( errno == ERANGE ) {
// over/under flow
} else
if ( *end != '\0' && !isspace(*end) ) {
// some garbage at end of string
}
-
Code:
float myFloat;
cout << "Enter a float number: ";
while (!(cin >> myFloat) || cin.get() != '\n')
{
cin.clear();
cin.ignore(1000, '\n');
cout << "Incorrect Data. Try again!!!\nEnter a float number: ";
}
cout << "Your float is: " << myFloat << "\n";
Search through some of my recent posts for explanations on why it is that simple.
-
That does work, thank you. However, this is a homework assignment that requires the use of strings so I don't think I can use yours.
-
I've been going through my book (unfortunatly the lectures have a habbit of not covering everything you need for the assignments, I guess that's one way to challenge you), and one example shows the program using
Code:
getline( cin, string)
Yet when I go from
Code:
cin.getline(myStr,30)
to
Code:
getline( cin, myStr)
I get an error.
note: <string> library is now included
-
More stuff. I'm trying to just expand my if statement to enclude everything.
Code:
if (((myStr[i] < '0') || (myStr[i] > '9')) //allows only digits 0-9 in the string
&& ((myStr[i] != '-') && (myStr[0] = '-')) //allows -'s in the string
&& (myStr[i] != '.'))//allows decimal points in the string
There is a problem in second and third line (tho I haven't even begun working the third line). What I want it to be read as is "- signs are okay, but only in the first spot, otherwise, bad code."
-
cin.getline(myStr,30) is for use with C style strings like myStr in your original code. getline( cin, myStr) is for C++ strings. You'd have to declare myStr as string myStr; instead of as a character array. You should prefer C++ strings in C++ programs, but unfortunately some classes require you to use C style strings instead. Whichever type of string you use determines which type of getline you use.
>> (myStr[0] = '-')
That is wrong because youuse = instead of ==. But a better way to do it would be to check if i does not equal 0 before checking whether myStr[i] is '-'.