473,386 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to take only Int Input and show error message when another type of input is given

Hi all
i am making a program to add two numbers but there is also such condition that only integers to be processed except integer input should be treated as 'Invalid Input'
please help!
Jul 18 '15 #1
2 1512
Answer is in your question: IF it's not int then something, something.
Just think about how would you define int in programming terms and what do you want to happen if that condition has not been met.
Good luck!
Jul 18 '15 #2
weaknessforcats
9,208 Expert Mod 8TB
This is harder than it looks.

If the user enters 123.45, the 123 is an int. In this case you would need to scan for the decimal and then avoid using 123 and 45 as the two ints.

Since you have no idea what the user will enter most likely you will need to fetch data a character at a time to determine whether it is an int. You would fetch the 1 and see that could be an int. Then fetch the 2 and see that 12 could be an int. Then fetch the 3 and see that 123 could be an int. Then fetch the decimal point and see that it is not part of an int so you ditch 123 and the decimal point. Then fetch characters until you get whitespace which allows ditching the 45. etc.etc.

Then build in logic that restricts the user to 2 ints.

Parsing input is so difficult that I tell students to not do it at all and just provide the input the program expects.

If you do write a parser, be sure to put the code in a library form so you can use it on all of your programs forever.

This is especially sad since most OS have features that do this if you write using the OS system calls.
Jul 18 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: aleksander.helgaker | last post by:
I've completely rewritten a calculator I wrote to help me learn Python. After someone told me about the def command I reliesed that I could make the program much better, but there is a very anoying...
2
by: JR | last post by:
I am attempting to display two list boxes. In the first list box is a listing of all entities. When a entity is selected and it has a subentity the second list box displays with a list of the...
2
by: Joakim Olesen | last post by:
Hello When validating xml against a schema, the most frequent error I get is something like "The 'FOO' attribute has an invalid value according to its data type. An error occurred at , (25,...
8
by: pmud | last post by:
Hi, I am using a compare validator in asp.net application(c# code). This Custom validator is used for comparing a value enterd by the user against the primary key in the SQL database. IF the...
2
by: ad | last post by:
I have a customErrors section in Web.config. All error except error 403 and 404, will be redirect to GenericErrorPage.aspx. How can I show the e.Message or some explicit statement according...
3
by: Richard Finnigan | last post by:
Hi I was wondering if anyone could help me with this problem. I've been using VWD and my webhost ( a shared hosting package) have told me that the MSQL express files wont work on thier server so...
8
by: sabby0805 | last post by:
Hi ! New to the boards here. I am in wk 2 of a java programming class. We had to submit an assignment and this is what I did: // checkpoint 2.2: WeeklyPay1.java // Printing name of employee and...
2
by: mirianCalin | last post by:
hi.. im doing a site.. i have this registration form where the user must enter some information like name, phone_number, age, etc.. an error message should appear if #1: there is an empty...
1
by: aaronsah | last post by:
My previous question was here: http://bytes.com/topic/access/answers/955125-attempting-use-asp-sql-update-mdb-via-webpage-running-into-80040e07-da That got rid of the MS error, but now I am...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.