Adding to a variable

I have been trying to solve this problem but always encountered problems with trying to add the past value of a number with a new number to make it a new value.
Can anyone help?

Basically num1 should be equal to temp+ num1 everytime the loop runs.

1
2
3
4
5
6
7
8
9
10
	
for (int p=0; p<b; p++)
	{
		float h= b-p-1;
		float i =pow(10, h);
		temp= x[p] * i ;
		num1= temp+num1;
		
	}	
Last edited on
Do you have b, temp, x and num1 declared somewhere else?
yes the full code is
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
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main ()

{ 
	char x [100];
	cin >> x;
	double a= strlen(x)-1;
	int e= a-1;
	int counter=0, num1=0, num2=0,num3=0, result, temp=0, temp2=0, temp3=0;
	int b,c,d;
	bool plus=false;
	bool minus=false;

	
	
	for (int n=0; n<a; n++ )
	{
		if (x[n] == '+') 
		{
			b=n;
			plus=true;

		}
		if (x[n] == '-') 
		{
			b=n;
			minus=true; 
		}
		if (x[n] == '=')
		{ 
			c=n;
			d= c-1;

		}
	}
	
	
	for (int p=0; p<b; p++)
	{
		float h= b-p-1;
		float i =pow(10, h);
		temp= x[p] * i ;
		num1= temp+num1;
		
	}
	for (int q=b+1; q<c; q++)
	{
		float f= c-q-1;
		float j= pow(10, f);
		temp2= x[q] * j;
		num2= temp2+num2;

	}
	
	for (int r=c+1; r<a+1; r++)
	{
		float g= a-r;
		float k=  pow(10, g);
		temp3= x[r] *k;
		num3= temp3+ num3;
	}
	
	if (plus== true && num1 + num2 == num3)
	{
	
				counter= counter+1;
	}
	
	if (minus== true && num1-num2==num3)
	{
				counter= counter+1;

	}
	else 
	{
		counter = counter;
	}
	
	cout << counter << "\n" << num1<< "\n" << num2 << "\n"<< num3 << "\n"<< "\n";
	
return 0;
	
	
}

at a quick glance instead of:
num1 = temp+num1;

you might want:
num1+ = temp+num1;
I have tried that but no luck

The program should check if the entered equation is true.
the equation has no spaces

then it will print 1 if yes and 0 if no
not sure what you're trying to do, but I i enter "2+2=4", then it only goes through every for loop only once. in other words, it aint looping.

what equation are you using for testing?
Whether or not the equation is correct.

Supposedly if someone inputs "3+3=6" or any true equation. It prints "1"
If the equation is false it prints "0"
closed account (2b5z8vqX)
You have a logical error in your source code that is the cause of the unexpected output. In the last three for loop you assign a variable to the result of an element of the character array 'x' times some other variable. The value returned by the subscript operator is that character's internal representation (which is a decimal integer), not its external representation (what is seen when that character is wrote to the standard output stream).

Subtracting the character literal '0' from the value returned by the subscript operator will give you the integer that the character represents.

http://en.cppreference.com/w/cpp/language/ascii

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
#include <iostream>
#include <string>
#include <cmath>
#include <cstring>

using namespace std;

int main (void) { 
	char x [100];
	// ...
	for (int p=0; p<b; p++)
	{
		float h= b-p-1;
		float i =pow(10, h);
		temp= (x[p] - '0') * i ;
		num1= temp+num1;
		
	}
	for (int q=b+1; q<c; q++)
	{
		float f= c-q-1;
		float j= pow(10, f);
		temp2= (x[q] - '0') * j;
		num2= temp2+num2;

	}
	
	for (int r=c+1; r<a+1; r++)
	{
		float g= a-r;
		float k=  pow(10, g);
		temp3= (x[r] - '0') * k;
		num3= temp3+ num3;
	}
	// ...
}
okay, so similar to what i was using (2+2=4).

so you're issue is more that none of the loops are being iterated more than once, rather than
trying to add the past value of a number with a new number to make it a new value


i think you stick a breakpoint on line 43, and check the values for p,b and h are what you expect.

we can see that if you use "2+2=4" or anything of that form, where the operator is always in the second position, it is obvious to see why you loops aren't working. Because:
(int p=0; p<b; p++)

will evaluate to:
(int p=0; p<1; p++)

because b will always be 1.
closed account (2b5z8vqX)
so you're issue is more that none of the loops are being iterated more than once, rather than
...
because b will always be 1.
The amount of times the for loops iterate depends on the input. The variable 'b' will be 3 if the input is "101+20=121".
Last edited on
indeed. that's why i said:
we can see that if you use "2+2=4" or anything of that form
.
The loop should run more than once if the number is bigger say
"300+300=600"
Topic archived. No new replies allowed.