Sep 22, 2012 at 4:50am UTC
I am still new to C++
I am writing a code that will find the area of a pyramid
Could you please help me understand why this program is not working?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
//User inputs Pyramid's area.
cout <<" input pyramid's area." << endl;
cin >> a;
//User inputs Pyramid's height.
cout <<"input pyramid's height.";
cin >> h;
//declares a, h.
float int = a
float int = h
float int = v
//calculates the Pyramid's volume.
cout << fixed << setprecision (2) << v = (a*h)/3 << endl;
//output to screen
cout<< "The volume of the cone is" << v << endl;
system("PAUSE");
return 0;
}
Last edited on Sep 23, 2012 at 12:58am UTC
Sep 22, 2012 at 5:08am UTC
Hi, it seems that you're confused over variable types and/or declaration. See further comments in the source below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <iostream>
//#include <string> You aren't using this header
#include <iomanip> // You'll need this header for setprecision
using namespace std;
int main ()
{
// Declare your variables before using them
// Also, granted them more meaningful names
float area;
float height;
float volume;
cout << "input pyramid's area: " << endl;
cin >> area;
cout << "input pyramid's height: " << endl;
cin >> height;
volume = (area * height) / 3;
cout << "The volume of the cone is " << fixed << setprecision (2) << volume << endl;
return 0;
}
Last edited on Sep 22, 2012 at 5:10am UTC
Sep 22, 2012 at 5:11am UTC
Yeah i kind of am. tried to get help from some friends but most dont know c++
Sep 22, 2012 at 5:28am UTC
so in language like bash you can have lots of flexability eith variables but in c++ you have to declare all of your variables with their data types (there are six)
Sep 22, 2012 at 5:35am UTC
Thanks so much guys :)
very much appreciate your input
Sep 22, 2012 at 9:51am UTC
so i retried the code again its not working now
Am I missing something else?
Sep 22, 2012 at 10:01am UTC
Show us the code you have now.
Sep 22, 2012 at 4:03pm UTC
remember to use more meaningfull names for variables declaration.
Sep 23, 2012 at 12:19am UTC
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
float area;
float height;
float volume;
cout << "input pyramid's area: " << endl;
cin >> area;
cout << "input pyramid's height: " << endl;
cin >> height;
volume = (area * height) / 3;
cout << "The volume of the cone is " << fixed << setprecision (2) << volume << endl;
return 0;
}
This is my code that doesnt work
Sep 23, 2012 at 6:13pm UTC
its cout.fixed abnd cout.setprecision(2) : they are data members of the ostream class and traditionally accessed with cout but you can use cerr or wcerr or make your own