Hey Guys,

Was reading a few posts on here and looks like a cool place to come and try to broaden my knowledge and try and teach myself C++.

I've only been reading a few days and I decided to try and create a simple calculator. I wanted to see if anyone else had done this and when I consulted google this site was one of the first results with the following post ..

So I decided to try and modify his code to make it loop and return after a calculation was complete. The trouble with my code is if a non numeric key is entered when prompted it goes into a weird loop. I was wondering if someone could take a look and let me know where I'm going wrong ?

Here,s my code ..

// Zarrens C++ Simple Calculator
// I decided to put everything I've learnt in the past few days to
// create this little simple calculator so I can practice my skills
// as I improve, I will improve my calculator

#include <cstdlib>
#include <cstdio>
#include <iostream>

using namespace std;

void Welcome_screen() {
cout << "Weclome to Zarren's C++ Simple Calculator.\n";
cout << "This is a little project that I will be improving as I improve ;)\n\n";
}

float number_cruncher(float num1, float num2, char myoperator) {
float result;
switch (myoperator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
cout << "You fail";
break;
}
return result;
}

int main() {
char controller;
float num1=0;
float num2=0;
char myoperator;
do {
Welcome_screen();
cout << "Enter your first number: ";
cin >> num1;
cout << "\nEnter the operator: ";
cin >> myoperator;
cout << "\nEnter second number: ";
cin >> num2;
float result = number_cruncher(num1, num2, myoperator);
cout << '\n' << result << "\n";
cout << "Type any key to contunue or Q to quit ...:";
cin >> controller;
} while(controller != 'Q');
cout << "See ya !!!";
return 0;

## All 5 Replies

You want something like

if(!isdigit(num1 || num2)){
cout<<"You entered a character that is not a digit\n";
Welcomscreen();
}

And for C++.Net you do:

if(!Char::IsDigit(e->KeyChar) && e->KeyChar != 0x08)//The 0x08 is the backspace key.. you might wanna add 0x0D which is the Enter button.
{
e->Handled = true;
}

Thanks for your reply but unfortunately this didnt help as it goes into a loop if I put a char in num1 so it never reaches the test ?

Normal cin sucks so I midified it and this is are 2 functions I use instead of cin:

int kcin(int i) {
int variable;
while(1) {
if (cin >> variable) {
break;
}
if(cin.fail()) {
cout << "Input Again: ";
cin.clear();
cin.ignore( 1024, '\n' );  //spend a lot of time on this!
}
}
return variable;
}

char kcin(char a) {
char variable;
while(1) {
if (cin >> variable) {
break;
}
if(cin.fail()) {
cout << "Input Again: ";
cin.clear();
cin.ignore( 1024, '\n' );  //spend a lot of time on this!
}
}
return variable;
}

One is to enter a character and the othera variable.
It kinda sucks because you do not send an adress of a variable but a copy of it so you have to return it. I wrote this functions a long time ago and still use them and I don't feel like changing 'em.
You use them like this
int variable=kcin(variable);

char character=kcin(character);
The arguments you pass to the function overload it and because there is 2 different functions if you pass an int to it it will call kcin(int) and if a character kcin(char).

commented: Thanks for the help bud !! +0

Thanks Sergent that's exactly what I needed. I'm not worried about passing the variable by reference as I've only been studying for about a week lol :D

here is my modified code to include your cin function.

// Zarrens C++ Simple Calculator
// I decided to put everything I've learnt in the past few days to
// create this little simple calculator so I can practice my skills
// as I improve, I will improve my calculator

#include <cstdlib>
#include <cstdio>
#include <iostream>

using namespace std;

void Welcome_screen() {
cout << "Weclome to Zarren's C++ Simple Calculator.\n";
cout << "This is a little project that I will be improving as I improve ;)\n\n";
}

float input_check() {
float num;
while(1) {
if (cin >> num) {
break;
}
if(cin.fail()) {
cout << "Numerical value needed !! Try again: ";
cin.clear();
cin.ignore( 1024, '\n' ); //spend a lot of time on this!
}
}
return num;
}

float Plus    (float num1, float num2) { return num1+num2; }
float Minus   (float num1, float num2) { return num1-num2; }
float Multiply(float num1, float num2) { return num1*num2; }
float Divide  (float num1, float num2) { return num1/num2; }

int main() {
char controller;
float num1=0;
float num2=0;
char myoperator;
float result=0;
do {
Welcome_screen();
cout << "Enter your first number: ";
num1 = input_check();
cout << "\nEnter the operator: ";
cin >> myoperator;
cout << "\nEnter second number: ";
num2 = input_check();
switch(myoperator) {
case '+' : result = Plus     (num1, num2); break;
case '-' : result = Minus    (num1, num2); break;
case '*' : result = Multiply (num1, num2); break;
case '/' : result = Divide   (num1, num2); break;
default : cout << "Sorry, this is a BASIC calculator ;)\n";
}
cout << '\n' << result << "\n";
cout << "Type any key and ENTER to contunue or Q ENTER to quit ...:";
cin >> controller;
system("clear");
} while(controller != 'Q');
cout << "Thanks for using my basic calculator !! \n Goodbye :D\n";
return 0;
}

Please let me know if there is anything I can do to improve this. Now onto arrays / pointers :D

Hi it looks good I'm a bigginer in programming and I need to make a simple calculator with an interface. how can I put this code with an interface in Visual C++ 2008 Express and display the result in a dilog box.
Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.