simple calculator
Feb 2, 2014 at 6:17am UTC
I am currently taking a class and one of the problems is to make a simple calculator. I have been able to make the code work but the out put isn't what the teacher is looking for. the input should be three float numbers like
25000
625
1
form that the output should be
25000.000000 + 625.0000000 = 25625.0
as you can see he wants six zeros added to some of the out put and one zero added to the last number. I don't know how to do that. Any help would be great.
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
#include <iostream>
#include <iomanip>
//cout<<fixed<<setprecision(2); this is for my notes olny
using namespace std;
void simple_calculator();
int main()
{
simple_calculator();
return 0;
}
void simple_calculator()
{
int menu_option;
float frist_number, second_number;
cout<<"Enter first number:\n" ;
cin>>frist_number;
cout<<"Enter second number:\n" ;
cin>>second_number;
cout<<"Enter a menu option:\n" ;
cout<<"[1] Add:\n" ;
cout<<"[2] Subtract:\n" ;
cout<<"[3] Multiply:\n" ;
cout<<"[4] Divide:\n" ;
cin>>menu_option;
switch (menu_option)
{
case 1:
cout<<frist_number<<" + " <<second_number<<" = " <<frist_number + second_number;
break ;
case 2:
cout<<setprecision(6)<<frist_number<<" - " <<setprecision(6)<<second_number<<" = " <<setprecision(1)<<frist_number - second_number;
break ;
case 3:
cout<<setprecision(6)<<frist_number<<" * " <<setprecision(6)<<second_number<<" = " <<setprecision(1)<<frist_number * second_number;
break ;
case 4:
cout<<setprecision(6)<<frist_number<<" / " <<setprecision(6)<<second_number<<" = " <<setprecision(1)<<frist_number / second_number;
break ;
}
}
Feb 2, 2014 at 6:29am UTC
You forgot the setprecision flags for the first case of switch. Also, if the precision remains constant, you don't need to respecify.
Feb 2, 2014 at 11:05am UTC
You forgot to put cout<<fixed;
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
#include <iostream>
#include <iomanip>
//cout<<fixed<<setprecision(2); this is for my notes olny
using namespace std;
void simple_calculator();
int main()
{
simple_calculator();
return 0;
}
void simple_calculator()
{
int menu_option;
float frist_number, second_number;
cout<<"Enter first number:\n" ;
cin>>frist_number;
cout<<"Enter second number:\n" ;
cin>>second_number;
cout<<"Enter a menu option:\n" ;
cout<<"[1] Add:\n" ;
cout<<"[2] Subtract:\n" ;
cout<<"[3] Multiply:\n" ;
cout<<"[4] Divide:\n" ;
cin>>menu_option;
cout<<fixed; // <------ put this over here
switch (menu_option)
{
case 1:
cout<<setprecision(6)<<frist_number<<" + " <<setprecision(6)<<second_number<<" = " <<setprecision(1)<<frist_number + second_number;
break ;
case 2:
cout<<setprecision(6)<<frist_number<<" - " <<setprecision(6)<<second_number<<" = " <<setprecision(1)<<frist_number - second_number;
break ;
case 3:
cout<<setprecision(6)<<frist_number<<" * " <<setprecision(6)<<second_number<<" = " <<setprecision(1)<<frist_number * second_number;
break ;
case 4:
cout<<setprecision(6)<<frist_number<<" / " <<setprecision(6)<<second_number<<" = " <<setprecision(1)<<frist_number / second_number;
break ;
}
}
Topic archived. No new replies allowed.