[C++] Basic programming problem, user inputting wrong variable type

Click For Summary

Discussion Overview

The discussion revolves around handling user input in a C++ program, specifically addressing the issue of incorrect variable types being entered by users in a calculator application. Participants explore methods to ensure the program remains stable when faced with unexpected input types, such as characters instead of numbers.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant expresses concern about program stability when users input characters instead of numbers, leading to crashes.
  • Another participant suggests using the typeid function to detect the data type entered and proposes using if statements to handle different cases, though they express uncertainty about its effectiveness with character input.
  • A participant questions how to implement typeid in their code example, specifically when a character is entered instead of a double.
  • Another participant introduces the isalpha function as a way to check if a variable is an alphabet character and inquires about when the error occurs during input.
  • A later reply suggests using the getline function to read input as a string, allowing for checks on the string format and ensuring it is not empty before converting to a number.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best method to handle incorrect input types, with multiple approaches being discussed and no definitive solution agreed upon.

Contextual Notes

Some limitations include the potential for misunderstanding how typeid and isalpha functions work in conjunction with user input, as well as the need for further clarification on handling null values and input formatting.

Who May Find This Useful

This discussion may be useful for beginner programmers learning C++, particularly those interested in input validation and error handling in user interfaces.

tbarker5
Messages
10
Reaction score
0
I'm fairly new to programming. I'm doing an undergrad ENG degree and one of my first year mandatory courses is programming. It's my first time ever doing it so I don't know much yet. I'm currently learning C++. I'm in the middle of working on a problem that pretty much involves me making a calculator. A lot of my variable types are either int or double. If I have some while loops set up so if the user puts in an incorrect number it tells them to reenter, ie putting a a value in for sin^-1 that is greater than 1. However if I put a character in my program crashes.
In the question it isn't mandatory that my calculator can handle this but I feel like my code is very unstable.
How do I deal with this problem?
 
Technology news on Phys.org


you use typeid to detect what data type is entered, then use if statements to handle the different cases. Not sure how that works out if you declare double but then enter a char there, though.
 


Pythagorean said:
you use typeid to detect what data type is entered, then use if statements to handle the different cases. Not sure how that works out if you declare double but then enter a char there, though.

I'm not sure I understand. for example if this was my code:
//other code
double anynumber(0);
count<<"Please enter any number: ";
cin>>anynumber;

How would I use this typeid (function?) , if the user entered for example the character ' a '
 


It looks like 'isalpha' is another function (that tells you whether a variable is an alphabet character).

You would use isalpha on the variable anynumber. Does it error on the cin>>anynumber line itself or later on when you try to implement anynumber?

(C++ is not my native language by the way)
 
Hey tbarker5 and welcome to the forums.

Since you have provided the standard input with a double, stdin should do all the formatting for you and the only thing left is to check for what is called a null value.

But cin doesn't do this so you will have to look at the string itself and you can get the string by using getline function:

http://www.cplusplus.com/reference/string/getline/

You can do checks from as simple as the string not being empty to things like making sure the whole format is correct (digits 0-9, one decimal place, etc) but cin should do the formatting for you.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 14 ·
Replies
14
Views
35K
  • · Replies 54 ·
2
Replies
54
Views
5K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 86 ·
3
Replies
86
Views
13K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 17 ·
Replies
17
Views
5K