Solved

simple if question

Posted on 1998-05-08
6
186 Views
Last Modified: 2010-05-18
I have a program which asks the user for a number (float) but if the user inputs a char instead of a float it sends my program into a loop which never ends.  I want to know if there is a way to read what the user inputs and if it is a char then it will tell the user they must enter a number and repeats the orginal question.

basic example

 
float x;
cout << "enter number";
cin >> x;

if (x != char) // problem is here

cout << "Number is: " << x;
else
cout << "you must enter a number";
0
Comment
Question by:onestar
  • 4
  • 2
6 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1174817
The way around this is the let the user enter a character string.  Then try to convert the string to a number (integer or real, as needed).  

As I'm not very familia with C I/O, I won't submit code examples, unless no one else can help (unlikely).
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174818
By the way the character string can be converted to an integer with atoi() or to a floating point with atof().
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1174819
What the heck.  I found what I needed.

char InputLine[100];

cin.getline(InputLine,100);
// You can test input line for invald characters here.  (Let me know if you need help on this.)  
float x = atof(InputLine);
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 22

Expert Comment

by:nietod
ID: 1174820
Not that you can test the strign entered by the use for invalid characters, but you don't have to.  the conversion fucntions start at the beginning of the string and stop converting when they reach an invalid character.  So if the user enters "123abc" it will convert to 123.  If the user enters "abc" it will convert to 0.  This may or may not be acceptable to you as is.
0
 
LVL 2

Expert Comment

by:kellyjj
ID: 1174821
dang nietod,  you beat to the punch.  I was trying to answer the question when you did.  Damn it all to hell!!!  hehehe
0
 
LVL 2

Expert Comment

by:kellyjj
ID: 1174822
Do you mean to have an "if" in there?  like "if (x)"?
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question