If/Else statement

Just trying to work out these kinks in my code. I'm mostly referencing lines 43-46 here. I think my syntax is wrong for my if statement where I wrote (( size[b] = ('h','o','w','s','p')) || (size[b] = ('H','O','W','S','P'))). I want the compiler to know that only those letters are valid, but I think I'm doing it wrong :/. I want the program to continue if the above statement (once corrected) is true, and if not, then it should go on to the else statement. I have the else statement outputting an error message, but how can I have the program loop back up and start the program from a previous point when the user inputs invalid info?? Thanks, and sorry for my newb-ness :(.

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
#include <iostream>
using namespace std;

//Prototype to keep console from closing.
class KeepRunning {
  public:
    ~KeepRunning() {
      system("pause");}};

//Define batting values
#define H  1
#define h  1
#define O  1
#define o  1
#define W  0
#define w  0
#define S  0
#define s  0
#define P  0
#define p  0

int main ()
{
    KeepRunning kr;
    
    int player;                 //Assign player number
    double 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;                      //Assign variable for integer size
    int letters = 0;            //Assing value of 0 to allow compiler to count
    
    cout << "\t\t\tBatting Average Calculator\t\t";
    
    cout << "\n\nEnter the player's number: ";
    cin >> player;
   
    cout << "Enter the player's batting record: ";
    cin >> size;
        {b = 0;
         while (size[b] != 0)
         if (( size[b] = ('h','o','w','s','p')) || (size[b] = ('H','O','W','S','P')))
         { letters++; b++; }
         else ( size[b] != 'h','o','w','s','p') || (size[b] != 'H','O','W','S','P');
         {cout << "\nAcceptable batting record codes are: 'H','O','W','S','P'. Please try again.";}}
        
    //Summate H, h, O, o
    sum = letters;
    
    //Summate 
    sumHits = H + h;
    
    //Calculate batting average
    average = sumHits/sum;
    
    cout << "\nPlayer " << player << "'s batting record: " << size << endl;
    cout << "Player " << player << "'s batting average: " << average << endl;
    
    std::cout << "Press ENTER to continue...";
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );         
    return 0;
}
A few things..
If you are checking for an individual letter you don't need an array...you could just keep doing this if(size == 'h' || 'o' ||....etc)

Also don't forgot that = is the assignment operator. (On a side note if you want to initialize an array you need to do this char foo [5] = { 'h', 'o', 'w', 's', 'p' }, which i think is what you were trying to do...but that won't work the way you've written your code)

As to
how can I have the program loop back up and start the program from a previous point when the user inputs invalid info??


You could just use a while loop. something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool invalid = false;
while(!invalid)
{
    invalid = true;
    
    // some stuff
 
    if(something)
        {
             //do stuff
        }
    else
        {
              //error msg
             invalid = false; 
        }
}


Perhaps read up a bit on flow control?
http://www.cplusplus.com/doc/tutorial/control/

Also you seem to have a stray brace on line 41 before b = 0
Thanks a million. I've updated my code, and it now looks like:

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
#include <iostream>
using namespace std;

//Prototype to keep console from closing.
class KeepRunning {
  public:
    ~KeepRunning() {
      system("pause");}};

//Define batting values
#define H  1
#define h  1
#define O  1
#define o  1
#define W  0
#define w  0
#define S  0
#define s  0
#define P  0
#define p  0

int main ()
{
    KeepRunning kr;
    
    int player;                 //Assign player number
    double 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;                      //Assign variable for integer size
    int letters = 0;            //Assing value of 0 to allow compiler to count
    
    cout << "\t\t\tBatting Average Calculator\t\t";
    
    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'))
     { letters++; b++; } 
    else {
    cout << "\nAcceptable batting record codes are: 'H','O','W','S','P'. Please try again.\n"; }
                
    //Summate H, h, O, o
    sum = H + h + O + o;
    
    //Summate 
    sumHits = H + h;
    
    //Calculate batting average
    average = sumHits/sum;
    
    cout << "\nPlayer " << player << "'s batting record: " << size << endl;
    cout << "Player " << player << "'s batting average: " << average << endl;
    
    std::cout << "Press ENTER to continue...";
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );         
    return 0;
}



But, when I run it, the console is good until I enter the batting record, and Windows gives me an error "baseball.exe has stopped working..."??

This is new, I've never had that happen. Any ideas?
Topic archived. No new replies allowed.