Once again hi.
I have tried to make a simple program to tell me the area of a circle.
I was able to make it till where I can insert the data but unfortunately I can't seem to get an answer :\
Could someone please take a look at the code and tell me what I might have missed please?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
usingnamespace std;
int main ()
{
int area, pi, radius;
pi= 3,14;
area= pi*(radius*radius);
cout << "Which is the radius of the circle";
cin >> radius;
cout << "The circle has an area of" << area;
system ("Pause");
}
What's even cooler is that you're using a comma as a decimal point in line 8.
Can you imagine how much fun that would be to debug in a larger program...*shudder*
btw, keeping with what helios is talking about, The ONLY way to code things properly is to think of them as a logical progression. As such, you can't make calculations before you have the data.
If I tell you solve for y knowing that x+y =5 and x=3, you have to know that x is equal to 3 before you can say that y+3=5 and blah blah blah farther down the line. The computer doesn't know anything about your calculations and numbers until you tell it what they are, so only give it the next step when it's ready.
And yeah, change those ints to floats or doubles. Otherwise you don't get any decimal values (which are very necessary for things like, idk, PI!).
edit: btw, to make things look nice, start including spaces with your prints, so you can change line 12 to say: cout << "The circle has an area of " << area;
Only thing I changed was the print, but trust me, it makes a difference when writing programs. If you can't read it, doesn't matter if its correct.
Also I don't know if they've taught you about newlines yet, but it's this character '\n' (a backslash and n together). It makes things look so much nicer for prints and will also become very important farther down the line. You should add one of those at the end of prints like this: cout << "The circle has an area of " << area <<"\n";
Sometimes this won't be necessary, but it's a good practice.
Thanks for your help guys.
Just so you know I learned my lesson after this experience:)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
int main ()
{
float area ,pi, radius;
pi= 3.14;
cout << "Which is the radius of the circle? " << "\n";
cin >> radius;
area= pi*(radius*radius);
cout << "The circle has an area of " << area << "\n";
system ("Pause");
}
Edit: Seen your edit, jpeg.
Will do my best to put your advice to use. Thanks once again :)