As a C self-learning 'C' project, I'm trying to create a simple 4-function calculator. I've succeeded, but only using integer numbers. Using 'FLOAT" to include the fractional part has me stumped. I really don't know just where to make my variables FLOAT type ?????
Please help. My sketch follows below;
float A;//1st number
float B;//2nd number
float ans;//answer
int OP;//operation choice add, subtract, multiply or divide
String msgA = "Enter A, then hit Enter.";
String msgB = "Enter B, then hit Enter.";
String msgOP = "Enter 1 for add, 2 for subtract, 3 for multiply, 4 for divide";
String msgEnd = "Your answer is:";
void setup() {
// put your setup code here, to run once:
String msgA = "Enter A, then hit Enter.";
String msgB = "Enter B, then hit Enter.";
String msgOP = "Enter 1 for add, 2 for subtract, 3 for multiply, 4 for divide";
String msgEnd = "Your answer is:";
Serial.begin(9600);
Serial.println("4-Function, two number calculator using function calls.");
Serial.println();
}
void loop() {
Serial.println(msgA);
while (Serial.available() == 0) { //waiting for 1st number A
}
A = Serial.parseInt(); //get A
Serial.println(A);
Serial.println(msgB);
while (Serial.available() == 0) { //waiting for 2nd number B
}
B = Serial.parseInt(); //get B
Serial.println(B);
Serial.println(msgOP);
while (Serial.available() == 0) { //waiting for OP (function) choice
}
OP = Serial.parseInt(); //get OP (function) selection
//function select
switch (OP) {
case 1:
ADD (A, B);
break;
case 2:
SUB (A, B);
break;
case 3:
MUL (A, B);
break;
case 4:
DIV (A, B);
break;
//Serial.println(ans);
}
}
// Functions are below. New functions e.g. ADD,SUB, MUL amd DIV MUST BE OUTSiDE Setup AND Loop !!!!!
void ADD( float A, float B) {
ans = A + B;
Serial.println(msgEnd);
Serial.println(ans);
Serial.println();
}
void SUB(float A, float B) {
ans = A - B;
Serial.println(msgEnd);
Serial.println(ans);
Serial.println();
}
void MUL(float A, float B) {
ans = A * B;
Serial.println(msgEnd);
Serial.println(ans);
Serial.println();
}
void DIV(float A, float B) {
ans = A / B;
Serial.println(msgEnd);
Serial.println(ans);
Serial.println();
}