Input & Output problem

Nov 12, 2012 at 3:19am
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <iomanip>
using namespace std;

const int SENTINEL = 'Q'; //type to stop the loop

int main()
{
    float A, G, V;
    float length, width, depth;
    char number;
    
    cout << setiosflags(ios::showpoint)
         << setiosflags(ios::fixed)
         << setprecision(2);
    cout << "Enter numbers for Surface Area(A), Girth(G), and Volume(V). To stop the loop, enter Q." << endl;
    
     cin >> number;
     while(number != SENTINEL) //sentinel controlled while loop
     {
     
      float length, width, depth;
     
            switch (number)
            {
      
                   case 'A':
                   A = 2*length*width + 2*width*depth + 2*length*width;
                   cout << "The surface area is " << A << endl;
                   break;
        
                   case 'G':
                   G = 2*length*width + depth;
                   cout << "The girth+depth is " << G << endl;
                   break;
        
                   case 'V':
                   V = length*width*depth;
                   cout << "The volume is " << V << endl;
                   break; 
        
             }  
        
        cin >> number;
        }
    
system("pause");
return 0;

}


At the moment, that is my program I created. I'm supposed to find the surface area, girth, and volume using the formulas given:
Volume = LWD (length X width X Depth)
Surface Area = 2LW plus 2WD plus 2LD
Girth = 2(L plus W) plus D

*input of the program will be a letter. (example: "V" will indicate volume, "A" for surface area, and "G" for girth) & Q is the sentinel (to stop loop).
*Second part of the input will be a three (float) numbers representing length, width, and depth respectively.
For example: A 2.0 3.0 4.0 means "Find the surface area of box with length = 2.0, width = 3.0, and depth = 4.0.

I'm using a switch structure to control the actions.
And the inputs are:
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

Enter numbers for Surface Area(A), Girth(G), and Volume(V). To stop the loop, en
ter Q.
A 1.0 2.0 3.0
The surface area is 0.00
G 5.0 4.0 3.0
The girth+depth is 0.00
V 2.3 4.9 6.7
The volume is 0.00
A 3.7 4.3 9.6
The surface area is 0.00
Q
Press any key to continue . . .


^^ that is my output. So the problem is for me is that it comes out as "0.00" for all outputs.
Is there anyway to fix that?
Nov 12, 2012 at 3:33am
Use char variable for a,v,g,q. After you get input for the char variable , you didn't ask the user to input for length,width,depth . So put cin statements for above after while condition
Nov 12, 2012 at 4:58am
Okay, so i changed the variable for a,v,g from "float" to "char".
1
2
3
char A, G, V;
float length, width, depth;
char number;


and i put the cin statement "cin >> length, width, depth;" above while condition
1
2
3
4
5
6
cout << "Enter numbers for Surface Area(A), Girth(G), and Volume(V). To stop the loop, enter Q." << endl;
    
     cin >> number;
     cin >> length, width, depth;
     while(number != SENTINEL) //sentinel controlled while loop
     {


but my output is this:
Enter numbers for Surface Area(A), Girth(G), and Volume(V). To stop the loop, en
ter Q.
A 1.0 2.0 3.0
The surface area is
G 5.0 4.0 3.0
The girth+depth is
Q
Press any key to continue . . .


Giving me a blank answer. Not sure if i put the cin statement right.
Last edited on Nov 12, 2012 at 4:58am
Nov 12, 2012 at 5:47am
Hmmm - perhaps number is not a good name for a variable that is a char !!

Variables A, V, G should be float or double because that's what holds the answer to you calculations

Here is a better way to quit from something:

1
2
3
4
5
6
7
8
9
bool Quit = false;

while (!Quit) {
//your code here

//user wants to quit
Quit = true;
}


Use the toupper function, so you don't have to test the input twice.
Topic archived. No new replies allowed.