if user give other than double value like string/alphanumeric/alphabets/some illegal values (ex:1;2), it should not accept as double and give an error message.
how to write it in c++.
thanks in advance
You Usually want to do the conversion with strtod. That will set a pointer to the end of the input that it could convert to a double. After it returns, you can check that to see whether it converted none, part, of all of the input, and react accordingly.
Related
Is there a way to prevent the program from executing characters in "Menu" type program where you choose options while running console and enter numbers only to do steps and browse data.
My program is a book catalog where you can review, change, add or delete information.
Things wouldn't be as bad but the the thing is, that when you enter a letter or non-number character the program gets messy and stuck.
I am not adding my code since I think that there should be a universal command to get rid of my problem + it would take some time to translate my code into EN.
The easiest way to do it is use the getline function I think. Instead of using cin >> num; for example, you would use getline(cin,value); where "value" is a string that you first store your input in. Then you can do num = atoi(value.c_str()); to save the string input as an integer value into "num". If "num" is a float, then just use atof instead of atoi. This way if the string is not a number, it just sets the value to 0. You could then use an if statement to display an error message if the value of num == 0. Hope that helps. Good luck!
If your application runs in a console and you are worried about wrong (non-number) input, perhaps you should the input not as a number, but as a text and then parse it to see it is really a number. If not, tell the user the input is incorrect and let them enter it again.
I have an input box in the GUI in VisionWorks. The input is treated as a ValueHolder that holds the string I want to traverse and whatnot, but I can't seem to get to that value. Any ideas?
I guess you mean VisualWorks, and yes: lurker is correct aValueHolder value will do.
However, if your input-field has a converter attached, it may not be a ValueHolder but a TypeConverer, which has already converted the string to a number or whatever.
So what is the class of your value-thingy ("treated like" sounds like it could be something else) ?
I'm having some trouble automating a matlab script which should prompt the user for the variable they are interested in, as well as the date range they want. I then want the script to concatenate their answers within a naming convention for the file they will ultimately load.
variable=input('please input variable of interest');
% temp
start=input('please state the start date in the form yymmdd: ');
%130418
enddate=input('please state the end date in the form yymmdd: ');
%140418
file=sprintf('%s_dailydata_%d_%d.csv',variable,start,enddate);
%so I thought 'file' would look like: temp_dailydata_130418_140418.csv
vardata=load(file);
the two numbers representing the dates are not causing any issues, but the fact that 'variable' is a string is. I know if I put apostrophes before and after 'temp' when I'm promted, it will work, but I have to assume that the end user won't know to do this. I tried putting curly braces around 'please input your variable..', but that didn't help either. Obviously this approach assumes that the date being requested exists in a filename.
Can anyone offer any advice? Perhaps the sprintf function is not the best option here?
Don't use 'end' as a variable name, it is a reserved name and using it could create conflicts with any function or logic block you're defining.
If you know your input is going to be a string: from the documentation for input()
str = input(prompt,'s')
Returns the entered text as a MATLAB string, without evaluating expressions.
As for knowing whether or not the file exists, that's something you'd have to incorporate some error logic for. Either a try/catch block with your load() call or you could use uigetfile() to get the filename.
Does anyone know how to override the ParsePrimitive method in DeserializeType?
Basically I have a of Dictionary<string,object> and whenever a number gets parsed in I want it to always be a decimal. Currently it gets taken down to the smallest numeric type it thinks the number will fit into.
Thanks
Added TryToParseNumericType but defaults to decimal
https://github.com/ServiceStack/ServiceStack.Text/pull/347
I have an EditText object (et_travel) on my screen that's asking for miles traveled. I grab that data like this:
float travel = Float.parseFloat(et_travel.getText().toString());
if(travel > 40000){
I just discover that if someone puts 40000 in the EditText, everything works fine, but if they put 40,000 (adding a comma to the number), I force close on the float travel = ...statement.
How can I evaluate the number without having a problem from the user adding a comma?
Is this in Java? It appears to be, but I'm wondering if I'm mistaken. Regardless, I would suggest you remove all of the characters from the string that are not of a numeric type. A way to do this may be using a regular expression.
A way to do this in Java may be the following:
String input = et_travel.getText().toString();
input = input.replaceAll("[^0-9]", "");
float travel = Float.parseFloat(input);
...
This way, you strip anything that is a non-numeric value from the string first, and then attempt to do your work. Obviously do some error checking before this (like input is not null and such). One change that is needed however is that you may need to maintain the '.' character (if you're given non-integer values). This would require changing the first regex a bit.
Check here: http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#replaceAll(java.lang.String, java.lang.String)
What you need is some validation on the input. Before converting the string into a float parse the string. If there are any ','s then remove them. If there is just junk then reject the input, otherwise someone could put a word or anything else in the input and cause havoc in your program.
Check out
inputType to restrict user input
android:inputType="number"