Where am i going wrong????

i have just created this program for a basic calculator. If you run it, you will see what is wrong. When user enters 0 it is supposed to finish.

#include<iostream>
#include<cmath>
#include<cstdlib>
void multiply();
void divide();
void add();
void subtract();
using namespace std;
int main()
{
cout<<"rules: choose function, enter numbers, 5 max, 0 to terminate"<<endl;
int mathFunction;
cout<<"Choose function, 1 multiply 2 divide 3 add 4 subtract"<<endl;
cin>>mathFunction;
switch (mathFunction)
{
case 1:
multiply();
case 2:
divide();
case 3:
add();
case 4:
subtract();
}
cin.get();
return 0;
}
void multiply()
{
int accumulator=1;
for(int a=0;a<5;a++)
{
int value;
cout<<"Enter number"<<endl;
cin>>value;
if (value<=0)
{
break;
}
accumulator*=value;
}
cout<<"answer is "<<accumulator<<endl;
}
void divide()
{
int accumulator=1;
for(int a=0;a<5;a++)
{
int value;
cout<<"Enter number"<<endl;
cin>>value;
if (value<=0)
{
break;
}
accumulator/=value;
}
cout<<"answer is "<<accumulator<<endl;
}
void add()
{
int accumulator=0;
for(int a=0;a<5;a++)
{
int value;
cout<<"Enter number"<<endl;
cin>>value;
if (value<=0)
{
break;
}
accumulator+=value;
}
cout<<"answer is "<<accumulator<<endl;
}
void subtract()
{
int accumulator=0;
for(int a=0;a<5;a++)
{
int value;
cout<<"Enter number"<<endl;
cin>>value;
if (value<=0)
{
break;
}
accumulator-=value;
}
cout<<"answer is "<<accumulator<<endl;
}


Thanks guys, i really appreciate this
Anthony
also, if there are any simpler versions, please let me know. I am reading the c++ for dummies, and am up to the arrays section, but dont quite understand arrays.


Also, to save me posting another thread, i am trying to create a zorg like program, but i have no idea how to get a user to input y or n or whatever and the program makes a choice based on that. Help?? :(
First, you should rap code in a code block, using[.code][./code] without the dots.

The first thing i noticed is that you dont have break statements in your switch. at the end of each case you need break; or the computer will read all the cases. When the user enters 1 all four math functions are called. Also, you aren't handleing case 0 to terminate.

fixed version
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
switch (mathFunction)
{
case 0:
    return 0;
case 1:
    multiply();
    break;
case 2:
    divide();
    break;
case 3:
    add();
    break;
case 4:
    subtract();
    break;
}
Run this, I'm sure you'll see the problem.

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;

void multiply();
void divide();
void add();
void subtract();

int main()
{
cout<<"rules: choose function, enter numbers, 5 max, 0 to terminate"<<endl;
int mathFunction;
cout<<"Choose function, 1 multiply 2 divide 3 add 4 subtract"<<endl;
cin>>mathFunction;
switch (mathFunction)
       {
       case 1:
       multiply();
       case 2:
       divide();
       case 3:
       add();
       case 4:
       subtract();
       }
       cin.get();
return 0;
}

void multiply()
     {
     cout << "Ok lets multiply" << endl;// Test
     int accumulator=0;
     for(int a=0;a<5;a++)
     {
     int value=0;
     cout<<"Enter number"<<endl;
     cin>>value;
     cout << " " << value << endl;  // Test
     if (value<=0)
     {
     break;
     }
     accumulator*=value;
     }
     cout<<"answer is "<<accumulator<<endl;
     }

void divide()
     {
     cout << "Ok lets divide" << endl;// Test
     int accumulator=1;
     for(int a=0;a<5;a++)
     {
     int value;
     cout<<"Enter number"<<endl;
     cin>>value;
     if (value<=0)
     {
     break;
     }
     accumulator/=value;
     }
     cout<<"answer is "<<accumulator<<endl;
     }
     
     
void add()
     {
     cout << "Ok lets add" << endl;// Test
     int accumulator=0;
     for(int a=0;a<5;a++)
     {
     int value;
     cout<<"Enter number"<<endl;
     cin>>value;
     if (value<=0)
     {
     break;
     }
     accumulator+=value;
     }
     cout<<"answer is "<<accumulator<<endl;
     }
     
     
void subtract()
     {
     cout << "Ok lets subtract" << endl;// Test
     int accumulator=0;
     for(int a=0;a<5;a++)
     {
     int value;
     cout<<"Enter number"<<endl;
     cin>>value;
     if (value<=0)
     {
     break;
     }
     accumulator-=value;
     }
     cout<<"answer is "<<accumulator<<endl;
     }


sorry... still not seeing it :(

i have corrected the break points in the switch statement. the subtract function is outputting negative numbers, so its something with the accumulator-=value bit, but i dont know how to fix that.....

and divide always returns 0
Last edited on
any help would be appreciated...
Of course it is. You initialize value to 0, then on line 102 you are subtracting value from accumulator, or 0. Why wouldn't this give you a negative number?

And for divide you have accumulator initialized to 1, and of type int. So you divide 1 by value and it's going to be a fractional answer. Since the answer is stored in an int it must be a whole value, and the next value less then 1 that's of type int is 0.
Last edited on
thank you :) so how do i set the accumulator to the first value entered then perform the operations 'on-the-fly' then append the result to the accumulator ready for the next value? this would solve both problems
Topic archived. No new replies allowed.