Ask your own question, for FREE!
Computer Science 28 Online
OpenStudy (adunb8):

Anyone know what i am doing wrong here? Im trying to find the base and the area of pyramid but i get a 1e0.02 something like this answer help!!

OpenStudy (adunb8):

#include <iostream> #include <iomanip> using namespace std; int main() { float length, area, base, VP, height ; cout << "The volume of the square base pyramid." << endl; cout << "Enter the length of the pyramid's base :" << endl; cin >> length ; cout << "Enter the height of the pryamid :" << endl; cin >> height ; area = length*length ; VP = area*height ; cout <<setprecision (2) << "The area of the base is : " << area << endl;; cout <<setprecision(2) << "The height of the pyramid is : " << height<< endl ; cout << setprecision (2) <<"The volume of the pyramid is : " << VP << endl;

OpenStudy (anonymous):

Are you getting that for the base or the area? aka, are you getting the base to come out correctly?

OpenStudy (adunb8):

no that is the problem... when i input for length 10.0 and height 5.0 i get an area of something like 1e002 or something and for volume i get 2e002 or something like that

OpenStudy (anonymous):

for debugging purposes (and to teach) try putting cout << area right after the area calculation and run that. If you're still getting the wrong answer, you know your error is in the top half of your code, otherwise the second half. Make sense?

OpenStudy (anonymous):

Please post what you get once you run that. I'd like to see what you get.

OpenStudy (adunb8):

The volume of the square base pyramid. Enter the length of the pyramid's base : 10.0 Enter the height of the pryamid : 5.0 The area of the base is : 1e+002 The height of the pyramid is : 5 The volume of the pyramid is : 1.7e+002 Press any key to continue . . . this is what i got on my c-command prompt... =(

OpenStudy (adunb8):

#include <iostream> #include <iomanip> using namespace std; int main() { float length, area , base, height, VP ; cout << "The volume of the square base pyramid." << endl; cout << "Enter the length of the pyramid's base :" << endl; cin >> length ; cout << "Enter the height of the pryamid :" << endl; cin >> height ; VP = (area*height)/3 ; area = length*length ; cout <<setprecision (2) << "The area of the base is : " << area << endl;; cout <<setprecision(2) << "The height of the pyramid is : " << height<< endl ; cout << setprecision (2) <<"The volume of the pyramid is : " << VP << endl;

OpenStudy (anonymous):

After area = length*length put in a line that says cout << area; for me please and post the output once again

OpenStudy (adunb8):

#include <string> #include <iomanip> using namespace std; int main() { float length, area, base, height, VP ; cout << "The volume of the square base pyramid." << endl; cout << "Enter the length of the pyramid's base :" << endl; cin >> length ; cout << "Enter the height of the pryamid :" << endl; cin >> height ; VP = (area * height) / 3 ; area = sqrt(length) ; cout << area << endl; cout <<setprecision (2) << "The area of the base is : " << area << endl;; cout <<setprecision(2) << "The height of the pyramid is : " << height<< endl ; cout << setprecision (2) <<"The volume of the pyramid is : " << VP << endl;

OpenStudy (adunb8):

i got an error.

OpenStudy (anonymous):

what's the error say?

OpenStudy (adunb8):

i think im doing the area wrong.... how do i multiply itself twice?

OpenStudy (anonymous):

area = length * length;

OpenStudy (anonymous):

also, you should use double instead of float.

OpenStudy (adunb8):

that area = length * length doesnt work when i do that the answer becomes 1e+002 or something

OpenStudy (anonymous):

your order is wrong. Area should be calculated first. otherwise it will calculate based on a pointer to garbage. (your variable) switch the area=length*length to be before your Vp = line

OpenStudy (adunb8):

i dont think that matters. #include <iostream> #include <string> #include <iomanip> using namespace std; int main() { float length, base, height, VP ; double area ; cout << "The volume of the square base pyramid." << endl; cout << "Enter the length of the pyramid's base :" << endl; cin >> length ; cout << "Enter the height of the pryamid :" << endl; cin >> height ; area = length * length ; VP = (area * height) / 3 ; cout <<setprecision (2) << "The area of the base is : " << area << endl;; cout <<setprecision(2) << "The height of the pyramid is : " << height<< endl ; cout << setprecision (2) <<"The volume of the pyramid is : " << VP << endl;

OpenStudy (adunb8):

still get same 1e+002

OpenStudy (anonymous):

ok, I copied your code and tried it in my system. I also included stdio.h in my header. you might try that as well

OpenStudy (anonymous):

Did it work?

OpenStudy (adunb8):

i dont think i am able to use that because i did not learn that in my class.

OpenStudy (anonymous):

just ran this without stdio and it works just fine. attached is the code

OpenStudy (anonymous):

did that work for you?

OpenStudy (adunb8):

let me check

OpenStudy (adunb8):

did not work =(

OpenStudy (anonymous):

I just compiled and ran @charpede's temp.cpp and it seems to work fine: The volume of the square base pyramid. Enter the length of the pyramid's base : 3 Enter the height of the pryamid : 5 The area of the base is : 9 The height of the pyramid is : 5 The volume of the pyramid is : 15 Can you paste your full output?

OpenStudy (adunb8):

ok #include <iostream> #include <iomanip> using namespace std; int main() { float length, area , base, height, VP ; cout << "The volume of the square base pyramid." << endl; cout << "Enter the length of the pyramid's base :" << endl; cin >> length ; cout << "Enter the height of the pryamid :" << endl; cin >> height ; area = length*length ; VP = (area*height)/3 ; cout <<setprecision(2) << "The area of the base is : " << area << endl;; cout <<setprecision(2) << "The height of the pyramid is : " << height<< endl; cout << setprecision(2) <<"The volume of the pyramid is : " << VP << endl; return 0 ; }

OpenStudy (anonymous):

The reason you're getting results that look something like `1.4e+02` sometimes is that it's outputting in scientific notation. This is because you're limiting the precision to 2 significant digits and you're inputting values that result in answers what are normally written as more than two digits in decimal form. `1.4e+02 = 140` If you want to output the answers up to two `decimal places`, just add `cout.setf(ios_base::fixed);` before you output.

OpenStudy (adunb8):

i dont know what that is i think im suppose to use the basic stuff we learned. i have no idea what that you wrote is though..

OpenStudy (anonymous):

What I was saying is: To get rid of your problem, either don't use `setprecision()`, or do this: #include <iostream> #include <iomanip> using namespace std; int main() { float length, area , base, height, VP ; cout << "The volume of the square base pyramid." << endl; cout << "Enter the length of the pyramid's base :" << endl; cin >> length ; cout << "Enter the height of the pryamid :" << endl; cin >> height ; area = length*length ; VP = (area*height)/3 ; cout.setf(ios_base::fixed); cout <<setprecision(2) << "The area of the base is : " << area << endl; cout <<setprecision(2) << "The height of the pyramid is : " << height<< endl; cout << setprecision(2) <<"The volume of the pyramid is : " << VP << endl; return 0 ; }

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!