lichess.org
Donate

c++ programmers! I need help!

Hi. I'm a beginner at c++ and I am making a calculator program. It is supposed to catch if the user enters invalid input but at the end of each calculation, it displays the error message. How do I make it not do that and how would I make it so that it would catch strings when it's a float?

#include <iostream>
#include <math.h>
std::string operation;
int i = 0;
double x;
double y;
void get_x_and_y();
int main() {
std::cout<<"This is a basic two number calculator\nx and y are real numbers ONLY.\nEntering a string will crash the program\n"
<<"Enter the operation\n1. x+y\n2. x-y\n3. xy\n4. x/y\n5. x^y\n6. sqrt(x)\nOperation(To add, type 1): ";
while(i<1){
getline(std::cin, operation);

if (operation=="1" || operation=="2"|| operation=="3" ||operation=="4" || operation=="5" || operation=="6"){

if (operation == "1"){
get_x_and_y();
std::cout<<x<<" + "<<y<<" = "<<x+y<<std::endl;
}

if(operation == "2"){
get_x_and_y();
std::cout<<x<<" - "<<y<<" = "<<x-y<<std::endl;
}
if(operation == "3"){
get_x_and_y();
std::cout<<x<<" * "<<y<<" = "<<x*y<<std::endl;
}
if(operation == "4"){
get_x_and_y();
std::cout<<x<<"/"<<y<<" = "<<x/y<<std::endl;
}
if(operation == "5"){
get_x_and_y();
std::cout<<x<<"^"<<y<<" = "<<pow(x,y)<<std::endl;
}
if(operation == "6"){
std::cout<<"Enter x: ";
std::cin>>x;
std::cout<<"squrt(x)"<<" = "<<sqrt(x)<<std::endl;
}}
else{
std::cout<<"INVALID INPUT";
}
}}

void get_x_and_y(){
std::cout<<"Enter x: ";
std::cin>>x;
std::cout<<"Enter y: ";
std::cin>>y;
}
I'm no expert coder, and have zero experience in c++, but one way of doing this would be to have a 'keydown event' that catches when a key is pressed on the keyboard. Here you could ignore any key but the 0-9 keys.
However you would probably still need to convert this into a double, since typing 1 would give you a string like this '1' instead of an int or whatever you want it to be. Perhaps you are getting exceptions like: 'this string is not a float'.

You may want to ask this on stackexchange instead :)

It's been a long time, but I think you have to use single quotes (') instead of double (").

I suggest that you make the operation input an integer instead of creating a headache by comparing a string input. Also you are double checking the value of "operation". An if...if else...else block would work better, and a switch statement would work best.

There are many calculator source codes available that actually operate like a real calculator.
follow what number 4 says, and don't use getline. (Single quotes are wrong though)
Remove the 'while(i<1){ }' line as it's not needed.
Also redundant is this line: 'if (operation=="1" || operation=="2"|| operation=="3" ||operation=="4" || operation=="5" || operation=="6"){}'

And make sure to change the 'if..else' statements to this:
if (
else if (
else if (
else if (
else if (
else if (
else {
Thanks! Now how would I make it so the program raises an exception when the user enters a string for double x?

This topic has been archived and can no longer be replied to.