could somebody help me and give me a few tips?

Problem:

Write a c++ program to calcualte the surface area, volume, or "girth + depth" of a box:
the formulas are as follows;
volume = LWD (lengthxwidthxdepth)
surface area= 2lw plus 2wd plus 2ld
girth + depth = 2(l+w) plus d

2. the input of the program will be a letter. V will indicate volume. A is surface area. G is girth plus depth, and q to stop the loop (q is the sentinel) the second part of the input will be a three (float) numbers representing the length, width, and depth respectively.
3. for each input, just calculate what is called for, not all three ansers. Use NESTED if-elses, or a switch structure to control your actions. THe output should have appropriate labels. Use format to control the appearance of your answers.
4. use sentinel controlled while loops to read the data. The Q for the operation will be the sentinel. THe program should work for any data set, not just the data set below.
5. libraries: iostream, iomanip

input:
A 1.0 2.0 3.0
G 5.0 4.0 3.0
V 2.3 4.9 6.7
A 3.7 4.3 9.6
Q

Output: as specified above


So that's the project and this is what I have so far


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

int main ()
{
int V, A, G, Q;
const int SENTINEL = Q; // Cease Loop
float length,width,depth;

V = length*width*depth;
A = 2*(length*width) + 2*(width*depth) + 2*(length*depth);
G = 2*(length*width) + depth;



system ("Pause");
return 0;

}
What is your question?

PLEASE USE CODE TAGS (the <> formatting button) when posting code. It makes it easier to help you.
Last edited on
1
2
3
V = length*width*depth;
A = 2*(length*width) + 2*(width*depth) + 2*(length*depth);
G = 2*(length*width) + depth;


You haven't assigned length, width of depth values. So they probably are storing some garbage value. This is the same for your SENTINEL value. You need to first read in the users number before making the calculations as well as assign q a value. You can do this by using the "cin >> length; " >> is the extraction operator and reads what the user types. This is an overloaded version of the bit wise shift operator and is included in the <iostream> header file you have included. It also says you should use a sentinel loop to read in this data and display appropriate prompts to user explain what is needed of them.

It asks that you only calculate what the user wants you to not all three answers like you are doing. As said you can do this with a switch statement. I would also split your calculations into functions to make the code more modular if you know how to do that.

EDIT:The basic outline would look like follows:

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
27
28
29
30
31
32
33
34
static bool SENTINEL = false;

while (!Sentinel)
{
    cout << ask for dimensions << endl;
    cin >> length >> width >> depth; //read in dimensions
    cout << ask what they want to calculate giving some indication of what to type for each such as (V)olume << endl;
    char input; //char to store input
    cin >> input;

    switch (input)
    {
        case 'v':
        case 'V':
            cout << CalculateVolume(length,width,depth) << endl;
        case 's':
        case 'S':
            cout << CalculateSurfaceArea(length,width,depth) << endl;;
        case 'g':
        case 'G':
            cout << CalculateGirthDepth(length,width,depth) << endl;
    }

    cout << Ask if they want to continue (y/n) << endl;
    cin << input;

    switch (input)
    {
        case 'y':
            SENTINEL = false;
        case 'n':
            SENTINEL = true;
    }
}
Last edited on
Topic archived. No new replies allowed.