|
-
November 7th, 2004, 07:18 AM
#3
Member
Well, No one has yet to reply. Im not sure if that is because everyone that has looked at my post sofar has no idea what I am talking about, or dont know how to fix it, but yeah. Anywayz, So far so good. I have fixed all of the problems sofar.
Code:
//Lyrics
//Class Music
#include <iostream>
#include <string>
using namespace std;
struct songs
{
string songName;
string lyrics;
};
struct cds
{
string title;
songs song[25];
};
class m
{
public:
void SetGroup(string);
string GetGroup();
void SetTitle(string, int);
string GetTitle(int);
private:
string group;
cds cd[5];
};
void m::SetGroup(string xgroup)
{
group = xgroup;
}
string m::GetGroup()
{
return group;
}
void m::SetTitle(string xtitle, int cnum)
{
cd[cnum].title = xtitle;
}
string m::GetTitle(int cnum)
{
return cd[cnum].title;
}
int main()
{
m music[3];
char hey[256];
cin.get(hey,256);
music[0].SetGroup(hey);
cin.ignore(1,'\n');
cin.get(hey,256);
music[0].SetTitle(hey, 0);
cout<<music[0].GetGroup()<<'\n'<<music[0].GetTitle(0)<<endl;
system("PAUSE");
return 0;
}
Just incase anyone was following the code and wanted to know why it wasnt working, I will explain it to the best of my knowledge. My "friend" had the GetGroup() functions returning a char pointer. Well, I re-read a chapter on pointers, which didnt help at all, and then I did some research on teh internet.
First, when you declare a char pointer, it is a little different then normal pointers.
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word = "Hey";
char* pChar = "Rah";
cout<<&word<<'\n'<<pChar<<'\n'<<word<<'\n'<<*pChar<<endl;
int num = 0;
int* pInt;
*pInt = 1;
cout<<&num<<'\n'<<pInt<<'\n'<<num<<'\n'<<*pInt<<endl;
system("PAUSE");
return 0;
}
Ouput:
0x22ff58
Rah
Hey
R
0x22ff40
0x22ffe0
0
1
Press any key to continue . . .
With the int pointer, it contains the address which points to a value. With the char pointer, (to my knowledge) it also contains an address which points to a value. But like it says, it is a char, which only holds one character. So when I assigned char* pChar = "Rah"; it gave the first address of the pointer R. If you want to access the a, you would have to do something like *(pChar+1). I know this is confusing, because I am still a little confused myself. If all char pointers are const, and you cant access the full string which the char pointer points to, what good are they. I dont know and hope someone answers this question.
Anywayz, I hope I helped someone out with my code, and I hope someone helps me out with my above question. Thanks for following along.
-Ep
01001001001000000100110001101111011101100110010100100000010000100110010101110100011101000111100100100001
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|