Title: adding variables
666kennedy - February 7, 2007 08:43 PM (GMT)
hey thanks for the help in my other thread, this reply is also there but no1 has noticed that its a new question so i put it here as well.
i have dev- C++ and its fine, works with the 2 programs ive learned so far, im on a new one now however, and i am coping fine with what the instructions from my notes are saying, (bareing in mind that i have the lab on a monday with ppl who are there to help) but i have got to a point where it is asking for a variable declaration, instead of the const ones ive been making so far...
here is the code for the program im editing with the varible stuff...
| CODE |
int main (void) { const float PI = 3.14159265; const float RADIUS = 2.0;
cout << endl << " the are of the circle is " << ( PI * RADIUS * RADIUS);
getch();
return 0;
} |
i know this program isnt very useful, and u have to edit and compile every time u want a new area...
but it then asks me to make it more flexible by modifying the declaration of the float constant RADIUS and replace it with a variable declaration (naming the variable "radius"). then set the radius to 2.0 usign a seperate assigment statement in the body of the program. this is not as far as adding "cin" however. i hope you can help!
Rmstn1580 - February 7, 2007 11:35 PM (GMT)
Ta-da!
| CODE |
#include <iostream> using namespace std;
int main() { float PI = 3.14159265; float RADIUS; RADUIS = 2.0;
cout << "\nThe area of the circle is: " << (PI * (RADIUS * RADIUS)) << endl;
system("PAUSE"); } |
666kennedy - February 8, 2007 12:06 AM (GMT)
is that it? i suppose, thats kinda what i thought to do, but i didnt know if that was the point! anyway how do u make so that the number you enter is the number of radius, or do you put something like
????????
Mephisto - February 8, 2007 12:29 AM (GMT)
Pretty much. All you do is make sure that you let the user know what you want. Be all like.
| CODE |
cout << "ENTER SOME RADIUS SHIT!" << endl; cin >> RADIUS;
cout << "The radius is... " << (PI * (RADIUS * RADIUS)) << endl;
|
Something of that nature. You get the picture, right?
666kennedy - February 8, 2007 12:40 AM (GMT)
ye i kinda got that, you have to float radius first tho eh. well it worked, how would i go about making area a variable and assigning it the equation that i had in the "cout" line. i think its more the assigning a equation to it and then putting it in the "cout" line so it comes up. also how would u make the msdos thing read...
the area is xxxxxx square metres, l
like having text on both sides???
thank you to all your help, i think im starting to get the hang of the basics!
Zaqufant - February 8, 2007 01:39 AM (GMT)
You mean like entering the number in the middle of a line? I don't thik that's possible. But to display it in a middle of a line you would just do...
| CODE |
| cout << "Bla Bla Bla" << variable <<"More bla stuff" << endl; |
You would do somthing like that.
Shonoby - February 8, 2007 02:00 AM (GMT)
Well, is this what u mean:
| CODE |
//Area(?) //Darwin Aguirre #include <iostream>
using std::cout; using std::cin; using std::endl;
int main() { int width = 1; int length = 1;
cout << "Enter width: "; cin >> width; cout << "\nEnter Length: "; cin >> length;
cout << "\nYour area is: " << ( width * length ) << " sq meter.\n";
return 0; }
|
Ohh and in most mathematically equations, everyone does not use 3.14.... but only use the value of 3.14. Because with the whole value it will create great completion as the mathematics increase, so just use 3.14.
kccc - February 9, 2007 12:11 AM (GMT)
or did you mean this:
| CODE |
#include <iostream> using std::cout; using std::endl;
int main() { int width = 0; int length = 0; int area; cout << "Enter length: "; cin >> length; cout << "Enter width: "; cin >> width; // area equation area = (length*width); cout << "The area is " << area << " centimeters squared." << endl; } |