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

In summary, if you are using a double as the input to your program and a character is entered, your code may crash. You can use isalpha to check for the character and if it is entered, handle the situation accordingly.
  • #1
tbarker5
10
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
  • #2


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.
 
  • #3


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 '
 
  • #4


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)
 
  • #5
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.
 

1. Why is my program crashing when the user inputs the wrong variable type?

When the user inputs the wrong variable type, the program may crash because the data is not compatible with the variable type you have declared. This can cause unexpected behavior or errors in your program.

2. How can I prevent the user from inputting the wrong variable type?

To prevent the user from inputting the wrong variable type, you can use validation techniques such as checking the data type before converting it or using error handling to catch any incorrect input.

3. What is the best way to handle user input errors in C++?

The best way to handle user input errors in C++ is to use error handling techniques such as try-catch blocks or using functions like cin.fail() to check for errors before converting the input to the desired variable type.

4. Can I convert user input to a different variable type?

Yes, you can convert user input to a different variable type using type casting. However, it is important to validate the input and handle any potential errors that may occur during the conversion process.

5. How can I make my program more user-friendly when handling variable type errors?

To make your program more user-friendly when handling variable type errors, you can provide clear instructions or prompts for the user to input the correct data type. You can also use loops to continuously prompt the user for input until the correct type is entered.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
2
Replies
54
Views
3K
  • Programming and Computer Science
Replies
8
Views
878
  • Programming and Computer Science
Replies
14
Views
31K
  • Programming and Computer Science
Replies
19
Views
3K
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
758
  • Programming and Computer Science
3
Replies
86
Views
10K
  • Programming and Computer Science
Replies
18
Views
2K
Back
Top