Hi guys, I'd really appreciate a tip in what's going wrong for me in this code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
float d = a + b;
if (d == 2)
cout << a << '\n' << b;
I first wanted to get factors of numbers using this method or maybe an infinite while method to find all the factors of a number or something but it doesn't even give me the write answers for addition, http://puu.sh/1jw2t ..
I'm probably making some obvious mistake here but I can't seem to find it..
You are forgetting to declare the values of float variables a and b. If they are not declared, then the compiler will have trouble finding the sum of a problem whose factors have no value. Your code should be:
using namespace std;
cout<< a << '\n' <<b;
I actually wanted the computer to find 'a' and 'b' for the user and the user would input 'd', so it would be kinda like a calculator. so I'm trying to ask the compiler if 'd' is 2, what are 'a' and 'b'?
I wanted to use this method for multiplication and division too!
Could you know whether a+b=10 if I don't tell you the values of a and b?
Well, the computer can, sort of. Unassigned variables are left with the value of whatever happened to be in memory when the variable was allocated. This will almost always be garbage. To put it succinctly: garbage+garbage=garbage, and garbage≠10.
if(a = b) should be if(a == b). if(a = b) Sets a = b, so it always returns true. a == b is a conditional statement.
Double symbol (&&, ||, ==) is conditional, while a single (&, |, =) is the binary representation.
Also, you failed to initialize 'a' and 'b', so those two integers could be any number.
That is why your code will not work.
On another note: Createing a program to "find 'x'" is not impossible. You simply have to reverse the equation your self, and give the program that equation to use, and the variables to use it with.
I did this all the time in my trigonometry class, lol. We would get to another subject and I would program my calculator with all the equations to find every variable in the equation. That way no matter what variable I needed to find, I would be able to pick the variable from my calculator, enter the numbers, and
it would give me the right answer every time.
On the other hand, if you want a program that will reverse an equation (x + 1 = 2 + 4 -> 2 + 4 - 1 = x), you will have to write a parser algorithm that will read the equation, and take the necessary steps to reverse it. That is extremely complex though (imo).