473,386 Members | 1,785 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.

Comparing input as integer or not

saza
4
heyya!

again i have a problem,

im trying to compare if either the int input "choice" is really an integer or not.

enter an integer input (int choice)
if input is an integer
do this
else
do that

how doi construct the if argument?

thanks!
Feb 28 '07 #1
4 3394
AdrianH
1,251 Expert 1GB
heyya!

again i have a problem,

im trying to compare if either the int input "choice" is really an integer or not.

enter an integer input (int choice)
if input is an integer
do this
else
do that

how doi construct the if argument?

thanks!
Show me how you are going to read in the input.


Adrian
Feb 28 '07 #2
saza
4
that's the thing, i dont know how it works.

Expand|Select|Wrap|Line Numbers
  1. int choice
  2.  
  3. scanf("%i", &choice);
  4.  
  5. if (/* choice input is not an integer...... */) <-- what statement should i write in?
Thanks!
Feb 28 '07 #3
AdrianH
1,251 Expert 1GB
that's the thing, i dont know how it works.

int choice

scanf("%i", &choice);

if (chice input is not an integer...... ) <-- what statement should i write in?

Thanks!
Uh, I think you mean "%d" (see http://www.cplusplus.com/reference/c...dio/scanf.html for more details on scanf format specifers and return values.

Anyway, if when reading in the input stream it cannot convert the value to an integer, scanf will return 0 as a result (it actully returns the number of successful scanned items).

Hope this helps.


Adrian
Feb 28 '07 #4
Savage
1,764 Expert 1GB
Well,you can test it on this way:

Expand|Select|Wrap|Line Numbers
  1.     int choice;
  2.     textbackground(0);
  3.     clrscr();
  4.     scanf("%d",&choice);
  5.     if(choice%1==0)/*If choice is declared as integer this condition is always true.*/
  6.     {
  7.         printf("\n\n\t Number is of integer type"):
  8.         getch();
  9.     }else{
  10.         printf("\n\n\t Number is not of int type"):
  11.     }
  12. }
Feb 28 '07 #5

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

Similar topics

2
by: prashanth | last post by:
Hi all, I am unable to write a javascript which will allow me to compare datetime entered by the user with the current datetime.( i.e the usertime should be less than or greater than currenttime)...
5
by: Curtis Gilchrist | last post by:
I am required to read in records from a file and store them in descending order by an customer number, which is a c-style string of length 5. I am storing these records in a linked list. My...
3
by: Robert Dell | last post by:
I have a problem comparing strings in an order form i'm writing. I want to give a running total at the bottom of the page and it appears to be working except it doesn't compare correctly (it...
13
by: Nicholas | last post by:
How can I compare char* with integers and characters contained in the str, where integers can be one digit or more? void Access(char *str) { char *pt = str; while (pt != '0') { if...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
3
by: geforceter | last post by:
I am doing a Java ME project which acquires input such as current location and destination from the use and then display the appropriate information such as bus service to take, travel time and image...
1
by: chiefychf | last post by:
I'm working on a school project and I am having a few issues... The program calls for three arrays a,b,c that have to be sorted, then compared to even or odd and stored in arrays d & e, then merge...
1
by: RN1 | last post by:
Sometimes I find that though I am comparing 2 integers, the result turns out to be unexpected. For e.g. an ASP page encapsulates recordset paging. <% Dim iPage,iPageCounter ...
1
by: dig909 | last post by:
I am trying to compare a number inserted into a jtextfield with a number generated with the program. Unfortunately the numbers never evaluate as equal. Here is an example of the code I'm working...
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:
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.