Calculator

A simple calculator that's my second code.What now?
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
75
76
77
78
79
80
81
82
#include <iostream>
#include <string>
using namespace std;

int main()
{
long int number;
long int times;
string ans;
int cat = 0;
do
{
cout<<"multiplication,addition,subtraction or division?"<<"\n";
cin>> ans;
{
if (ans == "multiplication"||ans == "multi"||ans == "m")
{
cat=1;
cout<<"Please enter a number: ";
cin>>number;
cin.ignore();
cout<<"Multiply by:";
cin>>times;
cin.ignore();
cout<<number<<"x"<<times<<"="<<number*times<<"\n";
cout<<"Kazam's calculator.";
cin.get();
cat=0;
}
if (ans == "addition"||ans == "add"||ans == "a")
{
cat=1;
cout<<"Please enter a number: ";
cin>>number;
cin.ignore();
cout<<"Add:";
cin>>times;
cin.ignore();
cout<<number<<"+"<<times<<"="<<number+times<<"\n";
cout<<"Kazam's calculator.";
cin.get();
cat=0;
}
if (ans == "subtraction"||ans == "sub"||ans == "s")
{
cat=1;
cout<<"Please enter a number: ";
cin>>number;
cin.ignore();
cout<<"Subtract:";
cin>>times;
cin.ignore();
cout<<number<<"-"<<times<<"="<<number-times<<"\n";
cout<<"Kazam's calculator.";
cin.get();
cat=0;
}
if (ans == "division"||ans == "div"||ans == "d")
{
cat=1;
cout<<"Please enter a number: ";
cin>>number;
cin.ignore();
cout<<"Divide by:";
cin>>times;
cin.ignore();
if (times <=0)
{
    cout<<"Can't divide by zero or less than zero."<<"\n";
}
else
{
cout<<number<<"/"<<times<<"="<<number/times<<"\n";
cout<<"Kazam's calculator.";
cin.get();
}
cat=0;
}
}
}
while(cat == 0);
}
Last edited on
Your calculator doesn't handle the case where I want to divide a number by zero. You should fix this first of all...

Apart from that, are you asking what program you should write next? There's a bunch of threads talking about various challenges that one can attempt...

http://cplusplus.com/forum/beginner/11118/
http://www.cplusplus.com/forum/articles/12974/

Hope that helps...
you can't divide a number by zero on the windows calculator either.
thanks for the links.
A tip as sammy said handle the case :)

like

1
2
3
4
5
6
7
8
if(times != 0)
{
   //Calculate
}
else
{
  std::cout<<"Can't divide by zero"<<std::endl;
}
Fixed division
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
75
76
77
78
79
80
81
82
#include <iostream>
#include <string>
using namespace std;

int main()
{
long int number;
long int times;
string ans;
int cat = 0;
do
{
cout<<"multiplication,addition,subtraction or division?"<<"\n";
cin>> ans;
{
if (ans == "multiplication"||ans == "multi"||ans == "m")
{
cat=1;
cout<<"Please enter a number: ";
cin>>number;
cin.ignore();
cout<<"Multiply by:";
cin>>times;
cin.ignore();
cout<<number<<"x"<<times<<"="<<number*times<<"\n";
cout<<"Kazam's calculator.";
cin.get();
cat=0;
}
if (ans == "addition"||ans == "add"||ans == "a")
{
cat=1;
cout<<"Please enter a number: ";
cin>>number;
cin.ignore();
cout<<"Add:";
cin>>times;
cin.ignore();
cout<<number<<"+"<<times<<"="<<number+times<<"\n";
cout<<"Kazam's calculator.";
cin.get();
cat=0;
}
if (ans == "subtraction"||ans == "sub"||ans == "s")
{
cat=1;
cout<<"Please enter a number: ";
cin>>number;
cin.ignore();
cout<<"Subtract:";
cin>>times;
cin.ignore();
cout<<number<<"-"<<times<<"="<<number-times<<"\n";
cout<<"Kazam's calculator.";
cin.get();
cat=0;
}
if (ans == "division"||ans == "div"||ans == "d")
{
cat=1;
cout<<"Please enter a number: ";
cin>>number;
cin.ignore();
cout<<"Divide by:";
cin>>times;
cin.ignore();
if (times <=0)
{
    cout<<"Can't divide by zero or less than zero."<<"\n";
}
else
{
cout<<number<<"/"<<times<<"="<<number/times<<"\n";
cout<<"Kazam's calculator.";
cin.get();
}
cat=0;
}
}
}
while(cat == 0);
}
Why can't I divide by less than zero? :(. My windows calculator says 'cannot divide by zero' when I try...a reasonable message.
Last edited on
Topic archived. No new replies allowed.