CIT020 Index > Midterm Practice > Corrections

CIT020 - Midterm Practice - Corrections

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main( )
{
    // height should be a double, not an integer
    int double height = 0.0;
    double radius = 0.0;
    
    double volume = 0.0; // result variable was not declared

    // declaration of PI is wrong.
    PI = double const 3.1415926536;
    const double PI = 3.1415926536;

    cout << "Enter the height in centimeters: ";
    cin >> height;

    cout << "Enter the radius in centimeters: ";
    cin >> radius; // missing semicolon

    // You can't express a condition this way. C++ won't give a syntax
    // error, but it won't do what you want.
    if (height && radius > 0)
    if (height > 0 && radius > 0)
    {
        // PI not capitalized correctly, length used instead of height
        volume = pi * length * radius * radius;
        volume = PI * height * radius * radius;
        cout << "The volume is " << volume
           << " cubic centimeters." << endl; // output not properly labelled
    }
    else
    {
        cout << "Both radius and height must be greater than zero." //missing quote marks
           << endl;
    } // missing closing curly brace

    system("PAUSE");
    return 0;
}