Sets and Gets not displaying default, help!

My program is supposed to ensure for values that fall within the correct parameters, and give a default response otherwise. My correct responses display, but the defaults are not appearing for incorrect responses and I can't figure out why.

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

class DateProfile
{
private:
   char gender;
   int romance;

public:
   static const char GENDER_MALE_UP;
   static const char GENDER_MALE_LOW;
   static const char GENDER_FEMALE_UP;
   static const char GENDER_FEMALE_LOW;
   static const char DEFAULT_CHAR;
   static const int MAX_SCALE;
   static const int MIN_SCALE;
   static const int DEFAULT_SCALE;

   bool setGender(char gen);
   char getGender() { return gender; }
   bool setRomance(int rom);
   int getRomance() { return romance; }

   DateProfile();
   DateProfile(char gen, int rom);
};

char const DateProfile::GENDER_MALE_UP = 'M';
char const DateProfile::GENDER_MALE_LOW = 'm';
char const DateProfile::GENDER_FEMALE_UP = 'F';
char const DateProfile::GENDER_FEMALE_LOW = 'f';
char const DateProfile::DEFAULT_CHAR = 'NULL';
int const DateProfile::MAX_SCALE = 10;
int const DateProfile::MIN_SCALE = 1;
int const DateProfile::DEFAULT_SCALE = 0;

int main()
{
   DateProfile daterM1, daterM2;

   daterM1.setGender('y');
   daterM1.setRomance(44);

   daterM2.setGender('M');
   daterM2.setRomance(7);

   cout << daterM1.getGender() << endl;
   cout << daterM1.getRomance() << endl;

   cout << daterM2.getGender() << endl;
   cout << daterM2.getRomance() << endl;

   return 0;
}

DateProfile::DateProfile()
{
   setGender(DEFAULT_CHAR);
   setRomance(DEFAULT_SCALE);
}

DateProfile::DateProfile(char gen, int rom)
{
   if (!setGender(gen))
      setGender(DEFAULT_CHAR);
   if (!setRomance(rom))
      setRomance(DEFAULT_SCALE);
}

bool DateProfile::setGender(char gen)
{
   if (gen != GENDER_MALE_LOW && gen != GENDER_MALE_UP && gen !=
      GENDER_FEMALE_LOW && gen != GENDER_FEMALE_UP)
      return false;
   gender = gen;
   return true;
}

bool DateProfile::setRomance(int rom)
{
   if (rom > MAX_SCALE || rom < MIN_SCALE)
      return false;
   romance = rom;
   return true;
}

/*            Sample Run                  

//daterM1, set to bad results, should display the defaults
╠
-858993460

//daterM2, set to correct results that pass
M
7
Press any key to continue . . .
*/
The problem is that your default values are not one of your allowed values so when you pass them to the set functions they return false without updating the value.
Last edited on
Wow, thank you so much! That was it exactly. I needed your extra pair of eyes.
Note that 'NULL' is a multicharacter literal. Exactly how this will be treated depends on your compiler, but its type is int so it might be truncated when you store it as a char. If you meant to use the null character you should have written '\0' instead.
Topic archived. No new replies allowed.