+ 1

How can i improve my calculator? (c++)

I want to add code to my program that will show you an error if you type a letter in a number. I dont know how to do this. Heres the code: https://code.sololearn.com/cg5czqgMZ59c/?ref=app

24th Apr 2023, 8:38 PM
Maciej Borecki
3 Respuestas
+ 4
Simple error check: cin.clear(); // read your input if(cin.good()) { // your switch-case statement } else { // do error handling here } This idea of error handling using stream methods .clear(), .good(), .fail() and the like can be done more sophisticated. But this simple idea may be what you are looking for. Generally, i deem it a good idea to read the string first, as has been suggested, and use a stringstream to then extract the data. Makes it so much more flexible and robust. However, don't use stdlib.h, if at all use cstdlib, and avoid atoX function family as they do not provide for any conversion error detection.
25th Apr 2023, 3:00 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 2
Easiest way is probably to take the input as a string to start with, and then use one of the functions from stdlib.h to extract the numbers (probably atof here?)
24th Apr 2023, 9:44 PM
Orin Cook
Orin Cook - avatar
+ 1
#include <cmath> Use std::pow() to raise one number to power of another. Use fmod() to calculate remainder from floating point division.
25th Apr 2023, 2:43 AM
Ipang