Got probs here...

Dear world!

I'm A totally beginner, so don't say i'm A noob. I know i am...

I get worng output when i try to compile this program:
____________________________________________________________________
#include <iostream>
#include "multiply.h"
using namespace std;
int main()
{
int result;
int diameter;
diameter = 2;
cout << area;
cout << "\n";
return area;
}
_____________________________________________________________________

Contents of multiply.h:

_____________________________________________________________________

int radius = diameter / 2;
int area = radius * radius * 3.14;

_____________________________________________________________________

Got this output:
_____________________________________________________________________
0
_____________________________________________________________________

But that´s not right. 1 * 1 * 3.14 = 3.14 not 0.
_____________________________________________________________________

Thanks for any advise!
That can't possibly compile. diameter is not declared when initializing radius.
I don't see how your code can compile. When the preprocessor is done with it, and it is ready for compiling, it looks like this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Lots of code from #include <iostream>
int radius = diameter / 2;
int area = radius * radius * 3.14;

using namespace std;
int main()
{
int result;
int diameter;
diameter = 2;
cout << area;
cout << "\n";
return area;
}


You are trying to use the variable diameter before you define what it is, leading to the compilation error:

multiply.h:1: error: 'diameter' was not declared in this scope

Aside from this, you have set area to be an int. An int cannot represent the value 3.14

If you want a non-integer value of area, you will have to use a non-integer variable.

You never set the value of diameter anywhere. You cannot expect the code to calculate something based on diameter when you never set a value.
Last edited on
Thank you very much.

Works now!

Also i think that i didn't got any errors becourse i've used Microsoft Visual C++.


Thank you very much anyway!!!
Hans
Also i think that i didn't got any errors becourse i've used Microsoft Visual C++.

If Microsoft Visual C++ happily compiled that code, something has gone very wrong. :)
Topic archived. No new replies allowed.