Sentinel controlled while loops (need help)

Hey everyone,

I'm writing a program here that should calculate the surface are, volume, cross sectional area of sphere. For each input, it should calculate what is called for, not all three answers. I should use sentinel controlled "while" loops to read the data. So here's my code, it has some issue with the SENTINEL, it doesn't compile, please help me out.

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
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

const char SENTINEL = Q;

int main ()
{
   
    char A, X, V, input;
    const double PI=3.14159;
    float r, total_A, total_X, total_V;
    
    cout<<"Enter character A to find Surface Area, character X to find the Cross Section Area,\n";
    cout<<"character V to find Volume or Q to quit\n"<<endl;
    cin>>input>>endl;
    while (input!=SENTINEL)
    {
          if (input==A)
          cout<<"Enter radius\n";
          cin>>r>>endl;
          total_A=4.0*PI*(r*r);
          cout<<"The surface are is"<<setw(5)<<showpoint<<fixed<<setprecision(2)<<total_A<<endl;
          
          if (input==X)
          cout<<"Enter radius\n";
          cin>>r>>endl;
          total_X=PI*(r*r);
          cout<<"The cross sectional area is"<<setw(5)<<showpoint<<fixed<<setprecision(2)<<total_X<<endl;
          
          if (input==V)
          cout<<"Enter radius\n";
          cin>>r>>endl;
          total_V=4.0/(3.0*PI*(r*r*r));
          cout<<"The volume is"<<setw(5)<<showpoint<<fixed<<setprecision(2)<<total_V<<endl;
          }
          system ("pause");
          return 0;
          }
it doesn't compile

You should post the compiler's message.

On the first glance, at least this line cannot be compiled:
cin>>input>>endl;
because the output manipulator std::endl cannot be called by an input stream.
On line 7: const char SENTINEL = Q; -> const char SENTINEL = 'Q'; // Note: ''
OK, changed it to this, now it compiles, but when I run the program it doesn't recognize the inputs, such as A,V,X


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
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

const char SENTINEL = 'Q';

int main ()
{
   
    char A, X, V, input_char;
    const double PI=3.14159;
    float r, total_A, total_X, total_V;
    
    cout<<"Enter character A to find Surface Area, character X to find the Cross Section Area,\n";
    cout<<"character V to find Volume or Q to quit\n"<<endl;
    cin>>input_char;
    while (input_char!=SENTINEL)
    {
          if (input_char==A)
          cout<<"Enter radius\n";
          cin>>r;
          total_A=4.0*PI*(r*r);
          cout<<"The surface are is"<<setw(5)<<showpoint<<fixed<<setprecision(2)<<total_A<<endl;
          
          if (input_char==X)
          cout<<"Enter radius\n";
          cin>>r;
          total_X=PI*(r*r);
          cout<<"The cross sectional area is"<<setw(5)<<showpoint<<fixed<<setprecision(2)<<total_X<<endl;
          
          if (input_char==V)
          cout<<"Enter radius\n";
          cin>>r;
          total_V=4.0/(3.0*PI*(r*r*r));
          cout<<"The volume is"<<setw(5)<<showpoint<<fixed<<setprecision(2)<<total_V<<endl;
          }
          system ("pause");
          return 0;
          }
1: I would just be repeating what coder777 pointed out
2: What if user doesn't have caps lock on
3: Try to illuminate redundancy
4: You don't need a variable for each result
5: const (I think) should be declared outside block so whole application can see it.

This code is unconventional, so see if you can modify it without using goto and it does have a problem, but the point is to answer your question and maybe give you a hint to a better coding method and style.

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
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

  const double PI = 3.14159;
  
int main () {
  char  Mode;
  double Result, r;
  
Next:  
  cout << "Enter mode & raduis: ";
  cin >> Mode;
  Mode &= 0x5f;

  if ( Mode != 'Q' ) {
    cin >> r;
    
    switch ( Mode ) {
      case 'V':
        cout << "Surface area is: ";
        Result = 4.0 * PI * (r * r);
        break;
      
      case 'A':
        cout << "Volume is: ";
        Result = 4.0 / (3.0 * PI * (r * r * r));
        break;
        
      default:
        cout << "Not a valid mode " << endl;
        goto Next;   
      }
    
    cout << Result << endl;  
    goto Next;
    }
  
  return 0;
  }
if (input_char==A)

You probably wanted

if (input_char=='A')
I understand that my style (if I have any) and format isn't good enough. Still I'm new to it and trying to learn. On my code: It's compiling !! :) However, when I run the program and trying to type V or X (or any other letter except Q) it will only calculate for A - which is the surface area ? Can you point me out what am I doing wrong ?

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
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

const char SENTINEL = 'Q';

int main ()
{
   
    char A, X, V, input_char;
    const double PI=3.14159;
    float r, total_A, total_X, total_V;
    
    cout<<"Enter character A to find Surface Area, character X to find the Cross Section Area,\n";
    cout<<"character V to find Volume or Q to quit\n"<<endl;
    cin>>input_char;
    while (input_char!=SENTINEL)
    {
          if (input_char=='A');
          cout<<"Enter radius for Surface Area\n";
          cin>>r;
          total_A=4.0*PI*(r*r);
          cout<<"The surface are is"<<setw(5)<<showpoint<<fixed<<setprecision(2)<<total_A<<endl;
                              
          if (input_char=='X');
          cout<<"Enter radius for Cross Section Area\n";
          cin>>r;
          total_X=PI*(r*r);
          cout<<"The cross sectional area is"<<setw(5)<<showpoint<<fixed<<setprecision(2)<<total_X<<endl;
                              
          if (input_char=='V');
          cout<<"Enter radius for Volume\n";
          cin>>r;
          total_V=4.0/(3.0*PI*(r*r*r));
          cout<<"The volume is"<<setw(5)<<showpoint<<fixed<<setprecision(2)<<total_V<<endl;
                   
    }
          system ("pause");
          return 0;
          }
The following line does nothing:
if (input_char=='A');
and neither do the other ifs.

I think you meant to write
1
2
3
4
5
6
if (input_char=='A') {
          cout<<"Enter radius for Surface Area\n";
          cin>>r;
          total_A=4.0*PI*(r*r);
          cout<<"The surface are is"<<setw(5)<<showpoint<<fixed<<setprecision(2)<<total_A<<endl;
}


and likewise for the others.
Guys you are MAGICIANS !!!!!! Thanks a lot
Topic archived. No new replies allowed.