So I tried making a simple calculator that can input 2 floats and do basic operations (addition, subtraction, division, multiplication). This is the first time I'm writing a code without copying the entire thing from a tutorial. I did copy some bug fixes and other things but mostly I did it myself. I want criticism and I want to know what I could've done better.
#include <iostream>
#include <ctype.h>
#include <string>
using namespace std;
char start;
float num1, num2, result;
char op;
int menu(){
cout<<"\nstart the program? \ny or n:\t";
cin>>start;
return 0;
}
int input_number(){
cout<<"\nenter the first number: ";
cin >> num1;
while (!cin){
cout<<"Error. Number of elements must be numerical. Try again: " << endl;
cin.clear();
cin.ignore(256, '\n');
cin >> num1;
}
cout<<"\nenter the second number ";
cin >> num2;
while (!cin){
cout<<"Error. Number of elements must be numerical. Try again: " << endl;
cin.clear();
cin.ignore(256, '\n');
cin >> num2;
}
return 0;
}
int input_op(){
cout<<"\nenter which operation you want to execute (+,-,/,*): ";
cin>>op;
switch (op)
{
case '+': result= num1+num2; break;
case '-': result= num1-num2; break;
case '*': result= num1*num2; break;
case '/': result= num1/num2; break;
default: cout<<"wrong input";
break;
}
return 0;
}
int main(){
cout<<"-----Calculator X-----";
menu();
if (start=='y' || start== 'Y'){
input_number();
input_op();
cout<<"the result is : "<<result<<'\n';
menu();
}
else{
cout<<"exiting program";
return 0;}
}