Calculator
Aug 28, 2014 at 6:28pm UTC
Hi there, I made this code up but I don't know why the result didn't come up with decimal its just an integer number even I made the data type float / double?
I also wanted from the code to start again if it gives the "Error"
look at line 68 if you didn't get what I mean
*note*
This code is just a start for me..
Thanks.
regards..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#include "stdafx.h"
#include <iostream>
using namespace std;
int addNums(float x, float y){
float answer = x + y;
return answer;
}
int addNums1(float a, float b){
float answer = a - b;
return answer;
}
int addNums2(float c, float d){
float answer = c / d;
return answer;
}
int addNums3(float e, float f){
float answer = e * f;
return answer;
}
int main()
{
float n1, n2;
int num;
cout << "Please enter a number: " ;
cin >> n1;
cout << endl;
cout << "Please enter a number: " ;
cin >> n2;
cout << endl;
cout << "Please choose your mathematical operation: " << endl;
cout << "Press \n 1 for addition \n 2 for subtraction \n 3 for division \n 4 for multiplication \n" ;
cin >> num;
if (num == 1){
cout << "The result is: " ;
cout << addNums(n1, n2);
cout << endl;
}
else if (num == 2){
cout << "The result is: " ;
cout << addNums1(n1, n2);
cout << endl;
}
else if (num == 3){
cout << "The result is: " ;
cout << addNums2(n1, n2);
cout << endl;
}
else if (num == 4){
cout << "The result is: " ;
cout << addNums3(n1, n2);
cout << endl;
}
else
cout << "error" ;
system("pause" );
return 0;
}
Aug 28, 2014 at 6:38pm UTC
Because you accidentally left the return types as int
.
By the way, prefer using double
instead of float
- only use float
if you need to store a lot of them and you don't mind losing precision.
Aug 28, 2014 at 7:07pm UTC
you mean in lines 6, 11, 16, 21??
Aug 28, 2014 at 7:08pm UTC
okay solved.. thank you, Sir
Topic archived. No new replies allowed.