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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

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