473,387 Members | 1,650 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,387 software developers and data experts.

How to take command-line input and verify that it is an int?

crabpot8
Hi all,

I have a small part of my program that I would like to revise. The problem is that if someone enters text, it just gets converted to 1 as an int. I would like to print an error message and ask them for an int again.

Thanks in advance,
crab

Expand|Select|Wrap|Line Numbers
  1. {    
  2.      int in;
  3.      cout << endl << "Please input an integer: ";
  4.      cin >> in;
  5.  
  6.      cout << "The int entered was : " << in; 
  7. }
Sep 9 '08 #1
2 1761
boxfish
469 Expert 256MB
If you cin to a variable of type int, and the user does not enter an integer, cin's fail flag will be raised, and your program will not accept any more input after that. The cin.fail() function returns true if the fail flag is raised, and the cin.clear() function lowers it again. After the line of code that prompts the user for input, check whether the fail flag is raised with the cin.fail() function, and if it is, lower it with the cin.clear() function and ask the user for input again. It would probably be best to put all this into a do {...} while (...); loop:
Expand|Select|Wrap|Line Numbers
  1. do {
  2.     Reset the fail flag
  3.     Ask the user for input
  4. } while (The input is still not a number);
  5.  
Hope this helps.
Sep 9 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
Also, don't forget to eat the data that caused the error in the first place. Clearing the failbit will not remove the cause of the error. That is, if text cause the error then the text has to be removed in addition to clearing the failbit.
Sep 10 '08 #3

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

Similar topics

21
by: Rabbit63 | last post by:
Hi: I want to show a set of records in the database table on the clicnt browser. I have two ways to do this (writen in JScript): 1.The first way is: <% var sql = "select firstname from...
2
by: Keith | last post by:
I think we have a corrupt IIS metabase on our web cluster. The controller is replicating the problem to the members. What steps can we take to confirm our IIS metabase is intact? I would...
0
by: Henrik | last post by:
I whant to take info from my VB program and put it in a textbox on a internet hompage. Can someone tell me what's the command for that?
2
by: micahstrasser | last post by:
I have been trying for days to send a command to the command prompt through the shell() function in vb.net. For some reason it is not working. Here is the code: Private Sub Button1_Click(ByVal...
0
by: Rob Dob | last post by:
How do I create a insert command that will take a datatable or datarow as a parameter instead of me having to supply it with parameters for each field value. I heard something about using the v2.0...
0
by: sajijkumar | last post by:
How can I take backup of the database to flash drive. I used a Dir and Drive list box controls to select the perticular dir, and Wrote the code, Shell "command.com /c copy " & App.Path &...
0
by: czerwww | last post by:
Can someone please help me? I have class for database connection and I need set command.commandTimeout. How can I do that? Code: Imports System.Data.SqlClient Imports System.Data Public Class...
2
by: durai1004 | last post by:
Hi all, I want to take the Backup of my database from command prompt. I tried the following command. mysql>mysqldump -u username -p password dbname > dbbackup.sql; But its showing error....
3
by: creative1 | last post by:
Here is how you create a complex data report that involves parent and child commands and you can update information at runtime. Its pretty straight forward to work with simple queries; however,...
1
by: Mark A | last post by:
When upgrading DB2 LUW from 8.2 to 9.1 the following command is issued as part of the upgrade: db2 migrate database <db-name> Does the migrate command make changes to every data and index...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.