Simple maths problem helpp!

Hi guys, I'd really appreciate a tip in what's going wrong for me in this code.

#include <iostream>

using namespace std;

int main()

    float a;
    float b;
    float d = a + b;

     if (d == 2)
        cout << a << '\n' << b;

    return 0;

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, ..

I'm probably making some obvious mistake here but I can't seem to find it..
Last edited on
What are the values of 'a' and 'b'? Does the user input them someplace? As it stands now, there is nothing for your program to add!
closed account (EAUX92yv)
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:

#include "iostream"
using namespace std;
int main()
float a=1;
float b=1;
float d=a+b;
if (d=2)
cout<< a << '\n' <<b;

I hope this helps you out!
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!
Last edited on
closed account (EAUX92yv)
There is a random number code in C++. Look here for a tutorial:
Line 12 is an assignment, not a comparison. So whatever a + b is, after line 12 d is always equal to 2. Try if (d == 2) instead.

Finding the factors of a number requires some modulo/remainder arithmetic, which you really can't do with floating point values (it works on integers).

Good luck!
Ah nice spot, oops ^_^..

Well I'm not getting any errors but I'm not getting any output either now lol

here's an image of what happens

I think my compiler and i both share a dislike for maths..
Last edited on
can you post the updated code you are using, maybe i can see where your going wrong
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.
Last edited on
So that means creating a simple programme that finds missing numbers of an equation is impossible? or is there a way to clean up the garbage of where unassigned variables are allocated?

I'll try maybe assigning 'a' and 'b' values of 0 and then working from there or adding some for loop or something :D .
So that means creating a simple programme that finds missing numbers of an equation is impossible?
It's impossible this way. There are symbolic computation methods to solve certain classes of equations, and there's also numerical algorithms to approximate said solutions.

But in any case, the C++ language is imperative, not declarative like mathematics. You can't just do
int a=20, b;
and expect b to hold 10 at the end.
Despite their name, statements don't actually state anything about the variables involved. a=10+b is a transformation of the state ( ) of the computer; more specifically of the value of a.

or is there a way to clean up the garbage of where unassigned variables are allocated?
Garbage is indistinguishable from non-garbage.
Well, for starters:

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).
Last edited on
Topic archived. No new replies allowed.