sentinel/continue to loop program

Hi. I have this code, which is working well, but when I prompt the user to input either sentinel value or any key to continue, they have to press enter for the compiler to do anything. Can I make it so they don't have to press enter, or no??

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <limits> 
#include <cstring>
using namespace std;

//Prototype to keep console from closing.
class KeepRunning {
  public:
    ~KeepRunning() {
      cin.get();}};

int main ()
{
    KeepRunning kr;
    
    const char H = 1;           //Define batting values
    const char h = 1;                    
    const char O = 1;                            
    const char o = 1;                            
    const char W = 0;                            
    const char w = 0;                            
    const char S = 0;                            
    const char s = 0;                            
    const char P = 0;                            
    const char p = 0;                                    
                                          
    int player;                 //Assign player number
    int sum;                    //Assign variable for sum of H,h and O,o
    double sumHits;             //Assign variable for sum of only H and h
    double average;             //Assign variable for average of H and O
    char size[100];             //Allows compiler to view user input as array
    int b=0;                    //Assign variable for integer size
    int lettersH = 0;           //Assign value of 0 to allow compiler to count hits
    int lettersO = 0;           //Assign value of 0 to allow compiler to count outs
    int noValue = 0;            //Assign value of 0 FOREVER
    int exit = 0;           //Assign value of SENTINEL to give user option to exit
     
    cout << "\t\t\tBatting Average Calculator\t\t";
    
    do
    {
    do
    {
         cout << "\n\nEnter the player's number: ";
         cin >> player;
    
         cout << "Enter the player's batting record: ";
         cin >> size;

         if (size[b] != 'H' && size[b] != 'h' &&
             size[b] != 'O' && size[b] != 'o' &&
             size[b] != 'W' && size[b] != 'w' &&
             size[b] != 'S' && size[b] != 's' &&
             size[b] != 'P' && size[b] != 'p' || cin.fail()) 
          {
            cout << "\nAcceptable batting record codes are: 'H','O','W','S','P'.Please try again.\n";
            cin.clear();
            cin.ignore(); 
          }            
    } while (size[b] != 'H' && size[b] != 'h' &&
             size[b] != 'O' && size[b] != 'o' &&
             size[b] != 'W' && size[b] != 'w' &&
             size[b] != 'S' && size[b] != 's' &&
             size[b] != 'P' && size[b] != 'p' || cin.fail()); 
             
          
    sumHits = 0.0;
    for(b=0;b<strlen(size);b++) {
        switch(size[b]) {
           case 'H': 
           case 'h':
              sumHits +=1;
              lettersH++;
              break;
           case 'O': 
           case 'o':
              lettersO++;
              break;
           case 'W':
           case 'w':
           case 'S':            
           case 's':            
           case 'P':            
           case 'p':
              noValue =+0; 
              break;             
              default:
              cin.clear();
              cin.ignore();
              lettersH = 0; 
              lettersO = 0; 
              }
          }
    //Summate H, h, O, o
    sum = ((lettersH) + (lettersO) + (noValue));
       
    //Calculate batting average
    average = sumHits/sum;
    
    cout << "\nPlayer " << player << "'s batting record: " << size << endl;
    cout << "Player " << player << "'s batting average: " << average << endl;
    
    cout << "\n\nPress 0 to exit. Press any key to continue.\n";
    cin >> exit;
    }while(exit != 0);
    
    //std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );    
    return 0;  
}
You can't, unless you get into the platform specific libraries, like the WinAPI. In general, just don't worry about it. If you REALLY want something, though, I'm happy to work out something (assuming Windows).
Oh, well darn the luck then. I'm not sure what happened, but the program was working fine and now any key closes the console. Any idea how to make sure it keeps open if the value isn't 0?
Try inputting into a string rather than an int, and then compare the string for if it isn't 0. Otherwise, if you enter a letter cin simply won't change the value of exit, and it will quit because the default value of exit is 0 (the sentinel value)
Thanks. Now that's fixed, but I have one more rookie question. Sorry:/. I changed the code so that 1 is to exit and 0 is remain in the program. The program prints from the beginning,
Enter the players number.
. But after inputting requested data, I'm given the error message. Why isn't the program continuing beyond this point the second time around?
Nevermind, I got it! Thanks for your help :)
Topic archived. No new replies allowed.