Why isn't my set setting?

I can't figure out why my set() isn't setting. There is no input, but my instructions were to "Always check for invalid client input in set() methods." This is just one value of one object to keep the code short. Thanks in advance!

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
class iTune
{
private:
   string name;

public:
   static const int MIN_STR_LENGTH;
   static const int MAX_STR_LENGTH;
   static const string DEFAULT_STRING;

   bool setName(string tempName);
   string getName() { return name; }

   iTune();
   iTune(string tempName);
};

int const iTune::MIN_STR_LENGTH = 1;
int const iTune::MAX_STR_LENGTH = 80;
string const iTune::DEFAULT_STRING = " (undefined) ";

int main()
{
   iTune tune1;

   tune1.setName("Headup");

   return 0;
}

iTune::iTune()
{
   setName(DEFAULT_STRING);
}

iTune::iTune(string tempName)
{
   if (!setName(tempName))
      setName(DEFAULT_STRING);
}

bool iTune::setName(string tempName)
{
   int tempInt;
   tempInt = tempName.length();
   if (tempInt > MAX_STR_LENGTH || tempInt < MIN_STR_LENGTH)
      return false;
   name = tempName;
   return true;
}
> I can't figure out why my set() isn't setting.
¿how are you checking that?
Sorry, wasn't sure what would be relevant. I've got a display method being called from main:

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
class iTune
{
private:
   string name;

public:
   static const int MIN_STR_LENGTH;
   static const int MAX_STR_LENGTH;
   static const string DEFAULT_STRING;

   bool setName(string tempName);
   string getName() { return name; }

   iTune();
   iTune(string tempName);
};

int const iTune::MIN_STR_LENGTH = 1;
int const iTune::MAX_STR_LENGTH = 80;
string const iTune::DEFAULT_STRING = " (undefined) ";

int main()
{
   iTune tune1;

   tune1.setName("Headup");

   tune1.display();

   return 0;
}

iTune::iTune()
{
   setName(DEFAULT_STRING);
}

iTune::iTune(string tempName)
{
   if (!setName(tempName))
      setName(DEFAULT_STRING);
}

bool iTune::setName(string tempName)
{
   int tempInt;
   tempInt = tempName.length();
   if (tempInt > MAX_STR_LENGTH || tempInt < MIN_STR_LENGTH)
      return false;
   name = tempName;
   return true;
}
string iTune::toString()
{
   stringstream ss;

   ss << name << " , " << artist << " , " << bitrate << " , " << total_time;
   return ss.str();
}

void iTune::display()
{
   iTune display;
   string temp;
   temp = display.toString();

   cout << temp;
}
1
2
3
4
5
6
7
8
void iTune::display()
{
   iTune display;
   string temp;
   temp = display.toString();

   cout << temp;
}


Is... not right.

1
2
3
4
void iTune::display()
{
    cout << toString();
}
What an amazing and simple fix! Thanks cire, you helped me discover a further issue :)

Now that my values are displaying, my example with bad values meant to display the default values is not showing the default for one value, the total_time. I've set tune5 to exceed 3600000 ms, so it should display "0", but it's not:

Sorry for the wall of code! When I pull too much out, I break it more...

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
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <sstream>
using namespace std;

class iTune
{
private:
   string name;
   string artist;
   int bitrate;
   int total_time;

public:
   static const int MIN_BITRATE;
   static const int MAX_BITRATE;
   static const int DEFAULT_BITRATE;
   static const int MIN_STR_LENGTH;
   static const int MAX_STR_LENGTH;
   static const string DEFAULT_STRING;
   static const int MIN_PLAY_TIME;
   static const int MAX_PLAY_TIME;
   static const int DEFAULT_PLAY_TIME;

   bool setName(string tempName);
   string getName() { return name; }
   bool setArtist(string tempArtist);
   string getArtist() { return artist; }
   bool setBitrate(int tempRate);
   int getBitrate() { return bitrate; }
   bool setTime(int tempTime);
   int getTime() { return total_time; }
   string toString();
   void display();

   iTune();
   iTune(string tempName, string tempArtist, int tempRate, int tempTime);
};

int const iTune::MIN_BITRATE = 64;
int const iTune::MAX_BITRATE = 705;
int const iTune::DEFAULT_BITRATE = 64;
int const iTune::MIN_PLAY_TIME = 1;
int const iTune::MAX_PLAY_TIME = 1000 * 60 * 60;
int const iTune::DEFAULT_PLAY_TIME = 0;
int const iTune::MIN_STR_LENGTH = 1;
int const iTune::MAX_STR_LENGTH = 80;
string const iTune::DEFAULT_STRING = " (undefined) ";

int main()
{
   iTune tune5;

   tune5.setName("");
   tune5.setArtist("");
   tune5.setBitrate(1);
   tune5.setTime(99999999);

   cout << "These are the most played tracks in my collection:\n\n";

   tune5.display();

   return 0;
}

iTune::iTune()
{
   setName(DEFAULT_STRING);
   setArtist(DEFAULT_STRING);
   setBitrate(DEFAULT_BITRATE);
   setTime(DEFAULT_PLAY_TIME);
}

iTune::iTune(string tempName, string tempArtist, int tempRate, int tempTime)
{
   if (!setName(tempName))
      setName(DEFAULT_STRING);
   if (!setArtist(tempArtist))
      setArtist(DEFAULT_STRING);
   if (!setBitrate(tempRate))
      setBitrate(DEFAULT_BITRATE);
   if (!setTime(tempTime))
      setTime(DEFAULT_PLAY_TIME);
}

bool iTune::setName(string tempName)
{
   int tempInt;
   tempInt = tempName.length();
   if (tempInt > MAX_STR_LENGTH || tempInt < MIN_STR_LENGTH)
      return false;
   name = tempName;
   return true;
}

bool iTune::setArtist(string tempArtist)
{
   int tempInt;
   tempInt = tempArtist.length();
   if (tempInt > MAX_STR_LENGTH || tempInt < MIN_STR_LENGTH)
      return false;
   artist = tempArtist;
   return true;
}

bool iTune::setBitrate(int tempRate)
{
   if (tempRate > MAX_BITRATE || tempRate < MIN_BITRATE)
      return false;
   bitrate = tempRate;
   return true;
}

bool iTune::setTime(int tempTime)
{
   if (tempTime > MAX_PLAY_TIME || tempTime < MIN_BITRATE)
      return false;
   total_time = tempTime;
   return true;
}

string iTune::toString()
{
   stringstream ss;

   ss << name << ", by " << artist << ", at a bitrate of " << bitrate <<
      ", at " << total_time << " milliseconds\n\n";
   return ss.str();
}

void iTune::display()
{
   cout << toString();
}
Last edited on
In your default constructor you make this call:

setTime(DEFAULT_PLAY_TIME);

But, if we take a look at that method we see that since DEFAULT_PLAY_TIME is less than MIN_BIT_RATE the call fails. The method indicates this by returning the value false, but since you only check the return values of your functions sporadically you don't detect it.
Wow, thanks! I've been working on this too long and needed the second eyes. I corrected that line to:

 
if (tempTime > MAX_PLAY_TIME || tempTime < MIN_PLAY_TIME)


but it's still giving me the same result:


(undefined) , by (undefined) , at a bitrate of 64, at -858993460 milliseconds

Press any key to continue . . .

Again, DEFAULT_PLAY_TIME is less than MIN_PLAY_TIME, so any call to setTime with an argument of DEFAULT_PLAY_TIME will fail.

Why do I get the feeling you can't read your own code? ;)
You're right! I've been coding for 7 weeks now and can't read my own without great attention! My instructor specified those particular values, though I hadn't noticed the disparity until you pointed it out. Having seen it, I wondered if it was something else I'd have to work around, but found an update on our site where she corrected the values to make sense.

cire, I thank you profusely!
Topic archived. No new replies allowed.