Solved

simple if question

Posted on 1998-05-08
6
176 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now