Class Object Value to String
Feb 24, 2016 at 7:01am UTC
Hi all, super noobish question here. My instructions say I have to take the values of the objects below and convert them to a string in the method "string toString()", that I can then display via method "display()".
I'm having a really hard time transferring either the values or a string outside of the class to be accessed by main, ultimately.
Sorry for the poorly worded question, I've been coding for 6 weeks now. Thanks in advance for any advice!
I'm sorry if this is too much code. I tried to cut a lot out... The last two methods are where my question is.
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 134 135 136 137 138 139 140 141 142 143 144 145 146
#include <iostream>
#include <string>
#include <cmath>
#include <sstream>
using namespace std;
class iTune
{
private :
string name;
string artist;
int bitrate;
int total_time;
public :
// class constants
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;
string theString;
// accessors
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();
// constructors
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 tune1;
tune1.setName("Headup" );
tune1.setArtist("Deftones" );
tune1.setBitrate(192);
tune1.setTime(313000);
cout << "These are the most played tracks in my collection!\n\n" ;
tune1.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()
{
string temp1, temp2;
stringstream convert;
convert << bitrate;
temp1 = convert.str();
convert << total_time;
temp2 = convert.str();
theString = name + " , " + artist + " , " + temp1 + " , " = temp2;
return theString;
}
void iTune::display()
{
cout << theString;
}
Last edited on Feb 24, 2016 at 7:08am UTC
Feb 24, 2016 at 8:22am UTC
You never call toString() anywhere so theString never gets set. I don' think you want theString to be member variable of the class. Why not make theString a local variable in the toString function instead and then you can call toString() in the display function to get the string representation of the object?
Feb 24, 2016 at 8:51am UTC
Yes,peter said it right. You can put theString variable as a local variable of toString() function.
Try this :
1 2 3 4 5 6 7 8
string iTune::toString()
{ string temp1,theString;
stringstream convert;
convert << bitrate<<"," << total_time;
temp1 = convert.str();
theString = name + " , " + artist + " , " + temp1;
return theString;
}
I didnt include the temp2 variable for my own assumptions,but you can try this to see it it works. 😃
Also,there will be a '+' sign before temp2 in line138.remember tht.
As for this line,
I'm having a really hard time transferring either the values or a string outside of the class to be accessed by main, ultimately
You can do this
1 2 3 4
Itune i(string,string,int ,int );
string s;
s=i.toString();
//....(other stuff goes here)
I dont kno if the function name is appropriate,but pls reply if you found this helpful.
Feb 24, 2016 at 3:51pm UTC
That's incredibly helpful, Peter87 and MaBunny, and I thank you both! I was able to get it to display something because of the two of you! It's not displaying what I want yet, and I don't know why, but I'm so much closer!
After changing toString() and display() to:
string iTune::toString()
{
string temp1, temp2, theString;
stringstream convert;
convert << bitrate;
temp1 = convert.str();
convert << total_time;
temp2 = convert.str();
theString = name + " , " + artist + " , " + temp1 + " , " = temp2;
return theString;
}
void iTune::display()
{
iTune display;
string temp;
temp = display.toString();
cout << temp << endl;
}
Any idea why my output is:
These are the most played tracks in my collection!
64-858993460
Press any key to continue . . .
Feb 24, 2016 at 5:29pm UTC
1 2
theString = name + " , " + artist + " , " + temp1 + " , " = temp2;
// ^ should be +, not =
temp2 (total_time) replaces the entire theString.
You can simplify to_string() considerably:
1 2 3 4 5 6
string iTune::toString()
{ stringstream ss;
ss << name << " , " << artist << " , " << bitrate <<" , " << total_time;
return ss.str();
}
Please use CODE tags, not quote tags when posting code.
Last edited on Feb 24, 2016 at 5:31pm UTC
Feb 25, 2016 at 12:52am UTC
You all are amazing!Thank you so much! It's displaying now :)
Now I'm having a problem with my set's not setting my private values, so it's displaying my defaults, but I'll start a new thread for that.
Thanks again!
Topic archived. No new replies allowed.