trouble with my char values

Hi all. I am taking my very first programming course this semester, so I am very new to all of this. I apologize if this is a simple fix.

The purpose of this program is to provide some basic information on the attendees of a theater. I was able to successfully get the program to count up the number of people in each age group, but the next step is asking for the gender, and then counting how many males and females are in attendance. The problem I am having is that when I enter an m, M, f, or F for the gender, I get my error message that I put for if the user enters anything except for those four values. I would greatly appreciate any advice to get this program back on the right track.

I see that on most threads, the full code is requested so here it is.

[code]


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

int age;
int group_0_to_18 = 0;
int group_19_to_30 = 0;
int group_31_to_40 = 0;
int group_41_to_50 = 0;
int group_51_to_60 = 0;
int group_60_up = 0;
int average_age = 0;
int total_age = 0;
int high_age = 0;
int low_age = 0;
int iterations = 0;
int num_male = 0;
int num_female = 0;
char gender;
char M;
char F;
char m;
char f;


int main() {


do {
cout << "Enter age of attendee (-1 to quit): ";
cin >> age;


if (age == -1) { // Sentinel value
break;
}


if (age < 0) {
cout << "Please enter a positive value: ";
cin >> age;
}

cout << "Enter gender (M or F): ";
cin >> gender;

if (gender != toupper(m) && gender != toupper(f)) {
cout << "Please enter either an 'M' or an 'F': ";
cin >> gender;
}



if (iterations == 0) { // Next segment to find low and high ages
high_age = iterations;
low_age = iterations;
}

else {
if (age > high_age) { // If the age is greater than the
// current highest/lowest
high_age = age; // Set highest/lowest equal to
// input value
}
else if (age < low_age) {
low_age = age;
}
}



if (age >= 0 && age <= 18) { // 0-18
++group_0_to_18;
}

if (age >= 19 && age <= 30) { // 19-30
++group_19_to_30;
}

if (age >= 31 && age <= 40) { // 31-40
++group_31_to_40;
}

if (age >= 41 && age <= 50) { // 41-50
++group_41_to_50;
}

if (age >= 51 && age <= 60) { // 51-60
++group_51_to_60;
}
if (age >= 60) { // 60+
++group_60_up;
}





++iterations;

total_age += age;


}
while (age >= 0);


cout << "Age 0 to 18: " << group_0_to_18 << endl;
cout << "age 19 to 30: " << group_19_to_30 << endl;
cout << "age 31 to 40: " << group_31_to_40 << endl;
cout << "age 41 to 50: " << group_41_to_50 << endl;
cout << "age 51 to 60: " << group_51_to_60 << endl;
cout << "Over 60: " << group_60_up << endl;

average_age = total_age / iterations;

cout << "Average age of attendees: ";
cout << average_age;
cout << endl;

cout << "Males: ";
cout << num_male;



return 0;
}
Last edited on
closed account (E0p9LyTq)
You forgot to use the close tag. :)

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
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
#include <string>
#include <cctype>

int main()
{
   int age = 0;
   int group_0_to_18 = 0;
   int group_19_to_30 = 0;
   int group_31_to_40 = 0;
   int group_41_to_50 = 0;
   int group_51_to_60 = 0;
   int group_60_up = 0;
   int average_age = 0;
   int total_age = 0;
   int high_age = 0;
   int low_age = 1000;
   int iterations = 0;
   int num_male = 0;
   int num_female = 0;
   char gender = ' ';

   do
   {
      std::cout << "Enter age of attendee (-1 to quit): ";
      std::cin >> age;

      if (age == -1)
      { // Sentinel value
         break;
      }

      if (age < 0)
      {
         std::cout << "Please enter a positive value: ";
         std::cin >> age;
      }

      while (true)
      {
         std::cout << "Enter gender (M or F): ";
         std::cin >> gender;

         gender = toupper(gender);
         if ('M' == gender || 'F' == gender)
         {
            break;
         }
      }

      if ('M' == gender)
      {
         ++num_male;
      }
      else
      {
         ++num_female;
      }

      if (age > high_age)
      { // If the age is greater than the
        // current highest/lowest
         high_age = age; // Set highest/lowest equal to
                         // input value
      }
      else if (age < low_age)
      {
         low_age = age;
      }

      if (age >= 0 && age <= 18)
      { // 0-18
         ++group_0_to_18;
      }

      if (age >= 19 && age <= 30)
      { // 19-30
         ++group_19_to_30;
      }

      if (age >= 31 && age <= 40)
      { // 31-40
         ++group_31_to_40;
      }

      if (age >= 41 && age <= 50)
      { // 41-50
         ++group_41_to_50;
      }

      if (age >= 51 && age <= 60)
      { // 51-60
         ++group_51_to_60;
      }

      if (age >= 60)
      { // 60+
         ++group_60_up;
      }

      ++iterations;

      total_age += age;
   } while (age >= 0);

   std::cout << "\nAge 0 to 18:  " << group_0_to_18 << '\n';
   std::cout << "age 19 to 30: " << group_19_to_30 << '\n';
   std::cout << "age 31 to 40: " << group_31_to_40 << '\n';
   std::cout << "age 41 to 50: " << group_41_to_50 << '\n';
   std::cout << "age 51 to 60: " << group_51_to_60 << '\n';
   std::cout << "Over 60:      " << group_60_up << '\n';

   std::cout << "\nMales:  " << num_male << '\n';
   std::cout << "Female: " << num_female << '\n';

   average_age = total_age / iterations;

   std::cout << "\nAverage age of attendees:   " << average_age << '\n';
   std::cout << "The oldest attendee was:    " << high_age << " years old.\n";
   std::cout << "The youngest attendee was:  " << low_age << " years old.\n";
}

Enter age of attendee (-1 to quit): 25
Enter gender (M or F): m
Enter age of attendee (-1 to quit): 30
Enter gender (M or F): M
Enter age of attendee (-1 to quit): 25
Enter gender (M or F): g
Enter gender (M or F): f
Enter age of attendee (-1 to quit): 30
Enter gender (M or F): M
Enter age of attendee (-1 to quit): 15
Enter gender (M or F): f
Enter age of attendee (-1 to quit): 80
Enter gender (M or F): M
Enter age of attendee (-1 to quit): -1

Age 0 to 18:  1
age 19 to 30: 4
age 31 to 40: 0
age 41 to 50: 0
age 51 to 60: 0
Over 60:      1

Males:  4
Female: 2

Average age of attendees:   34
The oldest attendee was:    80 years old.
The youngest attendee was:  15 years old.
Last edited on
awesome, everything seems to be working perfectly now! Thank you, I appreciate it.
Hi

trying changing this:

if (gender != toupper(m) && gender != toupper(f)) {

to:

1
2
char genderAsUpperCase = toupper(gender);
if (genderAsUpperCase != 'M' && genderAsUpperCase != 'F') {


The average should be of double type, you can delay the declaration of it until you have a value to assign to it:

double average_age = static_cast<double>total_age / iterations; // the cast promotes the calculation to be of type double

Don't forget to get rid of the first declaration of average_age at the top of the file.

Btw, please always use code tags http://www.cplusplus.com/articles/z13hAqkS/

When using a do loop, put the while part on the same line as the closing brace, so it doesn't look like a while on it's own.

I am not a fan of do loops like this, just because it always executes at least once, IMO is not a reason on it's own to choose it over an ordinary while loop. One example of where it is needed or at least convenient, is where something must inserted, even if it only loops once. An example for C programming (from Kernighan & Ritchie ) is the function itoa where at least 1 char must be inserted into a char array, even if the number of chars is 0.

All 3 types of loops (while, for , do) can be converted from one to another.

Try to avoid having global variables (those declared before main) - it's a good idea to restrict the scope of variables as much as feasible. For now you can put them all into the main function. It may not seems to make much difference now, but once you learn functions you will get to see how different it will be.

Good Luck !!
Topic archived. No new replies allowed.