C++ Subtraction calculation error!!

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "stdafx.h"
#include <stdio.h>
#include <math.h>
#define PI 3.14159265

void main()
{
char Continue;

do
{
int Amount, k;
char Choice;
float num, Deg;
float ans=0, ans1=0, ans2=1;
flushall();
printf("\n******************
*******************\n
\t
Menu\n\t
(1) Addition\n\t
(2) Subtraction\n\t
(3) Multiplication\n\t
(4) Division\n\t
(5) Mean\n\t
(6) Sin(x)\n\t
(7) Cos(x)\n\t
(8) Tan(x)\n\t
(q) Quit
\n*************************************\nChoice: ");
		scanf("%c", &Choice);
		if (Choice == 'q' || Choice == 'Q')
		{
		printf("You have ended the program.\n");
		break;
		}

		switch (Choice)
		{
case '1' : printf("How many numbers do you want to add : ");
				   scanf("%d", &Amount);
				   for (k=1; k<=Amount; k++)
				   {
		        printf("Please enter number %d: ", k);
			scanf("%f", &num);
			ans = ans + num;
				   }
				   printf("%.2f\n", ans);
				   break;

case '2' : printf("How many numbers do you want to subtract: ");
			       scanf("%d", &Amount);
				   for (k=1; k<=Amount; k++)
				   {
			printf("Please enter number %d: ", k);
			scanf("%f", &num);
			ans = num-ans && ans-num;
				   }
				   printf("%.2f\n", ans);
				   break;

case '3' : printf("How many numbers do you want to multiply: ");
			       scanf("%d", &Amount);
				   for (k=1; k<=Amount; k++)
				   {
			printf("Please enter number %d: ", k);
			scanf("%f", &num);
			ans = num;
			ans2 = ans2 * ans;
				   }
				   printf("%.2f\n", ans2);
				   break;

case '4' : printf("How many numbers do you want to divide: ");
			       scanf("%d", &Amount);
				   for (k=1; k<=Amount; k++)
				   {
			printf("Please enter number %d: ", k);
		        scanf("%f", &num);
					
				   }
				   printf("%.2f\n", ans2);
				   break;

case '5' : printf("How many number you want to insert: ");
	       scanf("%d", &Amount);
		for (k=1; k<=Amount; k++)
				   {
			printf("Please enter number %d: ", k);
			scanf("%f", &num);
		        ans = ans + num;
				   }
		        ans1 = ans / Amount;
			printf("%.2f\n", ans1);
			break;

case '6' : printf("Please enter x (Deg) : ");
	       scanf("%f", &Deg);
		ans = sin (Deg*PI/180);
		printf("sin(%.1f deg) : %.3f\n", Deg, ans);
		break;

case '7' : printf("Please enter x (Deg) : ");
	        scanf("%f", &Deg);
		ans = cos (Deg*PI/180);
		printf("cos(%.1f deg) : %.3f\n", Deg, ans);
		break;

case '8' : printf("Please enter x (Deg) : ");
		scanf("%f", &Deg);
		ans = tan (Deg*PI/180);
		printf("tan(%.1f deg) : %.3f\n", Deg, ans);
		break;
		
}
		flushall();
		printf("Do you want to continue (y/n) : ");
		scanf("%c", &Continue);
	}
	while (Continue == 'y');
	printf("Thank you for using.\n");
}
Last edited on
ans = num-ans && ans-num;
What do you expect that to do?

Also, when you post a problem, provide an explanation of what you are trying to do and what is happening. When you post code, use [code][/code] tags.
what hamsterman is alluding to is you have not provided the TWO numbers needed to support the subtraction operator.In other words you need a loop as provided for the addition operator.
May i know how can i change it to be able for it to work?
Its because of ans = num-ans && ans-num; thats why its not working.

The numbers can be as many as possible, it is depending on the user since ans=0.
It states saying (Enter the number u want to subtract: ) for e.g 2
Enter number 1: 10
Enter number 2: 2

i have changed ans = num-ans && ans-num to ans = num-ans;
However the results will always be the 2nd number subtracting the 1st number.
Is there any way to do it the other way round?

For example, when 1st number is 10, 2nd number is 2.
The results will be 2-10 which is -8.
Last edited on
closed account (DSLq5Di1)
Given your input and arithmetic above, think about what is happening..
1
2
3
4
5
// 1st loop
ans = 10 - 0

// 2nd loop
ans = 2 - 10

Your first number should be the initial value of ans, and any numbers following are subtracted:-
1
2
3
4
5
6
7
8
9
10
k = 1;
printf("Please enter number %d: ", k++);
scanf("%f", &ans);

for (; k<=Amount; k++)
{
    printf("Please enter number %d: ", k);
    scanf("%f", &num);
    ans = ans-num;
}

You could also check which number is greater than the other, then subtract the larger from the smaller.
Last edited on
Topic archived. No new replies allowed.