Solved

simple if question

Posted on 1998-05-08
6
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

729 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