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

AI Thread Summary
The discussion revolves around handling user input in a C++ calculator program, particularly when the input is invalid, such as entering a character instead of a number. The user is learning programming and seeks advice on making their code more stable. They mention using `typeid` to detect data types and express confusion about its application when a character is inputted into a variable declared as a double. Another participant suggests using the `isalpha` function to check if the input is an alphabet character. They clarify that the issue may arise during the input process with `cin`, and recommend using the `getline` function to read input as a string. This allows for more robust validation checks, such as ensuring the input is not empty and conforms to the expected numerical format. Overall, the conversation emphasizes the importance of input validation to prevent program crashes and improve stability.
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);
cout<<"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.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...

Similar threads

Replies
14
Views
34K
Replies
54
Views
5K
Replies
34
Views
4K
Replies
86
Views
12K
Replies
9
Views
2K
Replies
9
Views
2K
Replies
17
Views
4K
Back
Top