Printing variables with member function does not give correct values

58 Views Asked by At

Running the program will take user input and validate, but does not print the expected information. I can simply enter 5 for each dimension and 'blue' for color but then receive:

The length is: 2.97299e-41
The width is: 2.99205e-41
The height is: 2.99205e-41
The color is: 
The volume is: 0

Could someone please explain why the input info does not carry to the print stage? Here is the code:

#include <iostream>
#include <string>
using namespace std;
// User is prompted for cube dimensions and color, which is validated. Create class Cube,          calculate volume,
// then display the stored information from Cube.
string length;  // declare variables
string width;
string height;
string volume;
string color;

int main() {
    int flag = 0;                       // flag, duh
    cout << "Enter the cube length: ";  // user input dimensions & color
    cin >> length;
    for (int i = 0; i < length.size(); i++)  // input validation for integers
    {
        if (!isdigit(length[i])) {
            flag = 1;
            break;
        }
    }
    if (flag == 1) cout << "Enter a valid number." << endl;
    cout << "Enter the cube length: ";
    cin >> length;

    cout << "Enter the cube width: ";
    cin >> width;
    for (int i = 0; i < width.size(); i++) {
        if (!isdigit(width[i])) {
            flag = 1;
            break;
        }
    }
    if (flag == 1) cout << "Enter a valid number." << endl;
    cout << "Enter the cube width: ";
    cin >> width;

    cout << "Enter the cube height: ";
    cin >> height;
    for (int i = 0; i < height.size(); i++) {
        if (!isdigit(height[i])) {
            flag = 1;
            break;
        }
    }
    if (flag == 1) cout << "Enter a valid number." << endl;
    cout << "Enter the cube height: ";
    cin >> height;

    cout << "Enter the cube color: ";
    cin >> color;
    for (int i = 0; i < color.size(); i++)  // input validation for string
    {
        if (flag == 1) cout << "Enter a valid color." << endl;
        cout << "Enter the cube color: ";
        cin >> color;

        if (!isdigit(color[i])) {
            flag = 1;
            break;
        }
    }
    stof(length);  // string values convert to integer
    stof(width);
    stof(height);

    Cube access;       // create object
    access.display();  // call member function
}
class Cube {
public:
    float length;
    float width;
    float height;
    float volume = length * width * height;  // calculate volume
    string color;
    void display() {  // display Cube information
        cout << "The length is: " << length << endl;
        cout << "The width is: " << width << endl;
        cout << "The height is: " << height << endl;
        cout << "The color is: " << color << endl;
        cout << "The volume is: " << volume << endl;
    }
};

I tried providing different values, but none go through. Using the input example mentioned above code, I expect:

The length is: 5
The width is: 5
The height is: 5
The color is: blue
The volume is: 125
1

There are 1 best solutions below

0
Pablo Salazar On

I solve your question adding a function to calculate the volume and using the variables inside the object.

#include <iostream>
#include <string>
using namespace std;

string length;
string width;
string height;
string volume;
string color;

class Cube
{
public:
    float length;
    float width;
    float height;
    float volume;
    string color;
    void display()
    {
        cout << "The length is: " << length << endl;
        cout << "The width is: " << width << endl;
        cout << "The height is: " << height << endl;
        cout << "The color is: " << color << endl;
        cout << "The volume is: " << volume << endl;
    }
    void calculateVolume()
    {
        volume = length * width * height;
    }
};

int main()
{
    int flag = 0;
    cout << "Enter the cube length: ";
    cin >> length;
    for (int i = 0; i < length.size(); i++)
    {
        if (!isdigit(length[i]))
        {
            flag = 1;
            break;
        }
    }
    if (flag == 1)
        cout << "Enter a valid number." << endl;

    cout << "Enter the cube width: ";
    cin >> width;
    for (int i = 0; i < width.size(); i++)
    {
        if (!isdigit(width[i]))
        {
            flag = 1;
            break;
        }
    }
    if (flag == 1)
        cout << "Enter a valid number." << endl;

    cout << "Enter the cube height: ";
    cin >> height;
    for (int i = 0; i < height.size(); i++)
    {
        if (!isdigit(height[i]))
        {
            flag = 1;
            break;
        }
    }
    if (flag == 1)
        cout << "Enter a valid number." << endl;

    cout << "Enter the cube color: ";
    cin >> color;
    for (int i = 0; i < color.size(); i++)
    {
        if (flag == 1)
            cout << "Enter a valid color." << endl;

        if (!isdigit(color[i]))
        {
            flag = 1;
            break;
        }
    }

    Cube access;
    access.length = stof(length);
    access.width = stof(width);
    access.height = stof(height);
    access.color = color;
    access.calculateVolume();
    access.display();
}