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 ..
http://www.daniweb.com/software-development/cpp/threads/238276
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
// Header files
#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;