User-input on Pointers
Nov 28, 2016 at 10:16pm UTC
I am having a little bit of trouble trying to get the user-input to work with using pointers. Here is what I wrote.
1 2 3 4 5 6 7 8 9
//GetID, GetSongName and the rest are all functions
cout << "Enter song ID: " << endl;
cin >> userInfo->GetID;
cout << "Enter song name: " << endl;
cin >> userInfo->GetSongName;
cout << "Enter artist name: " << endl;
cin >> userInfo->GetArtistName;
cout << "Enter song length: " << endl;
cin >> userInfo->GetSongLength;
Here are the function definitions.
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
PlaylistNode::PlaylistNode() {
this ->uniqueID = 'none' ;
this ->songName = 'none' ;
this ->artistName = 'none' ;
this ->songLength = 0;
PlaylistNode* nextNodePtr= 0;
}
PlaylistNode::PlaylistNode(string initID, string initSongName, string initArtistName, int initSongLength, PlaylistNode* nextLoc = 0) {
this ->uniqueID;
this ->songName;
this ->artistName;
this ->songLength = 0;
PlaylistNode* nextNodePtr = nextLoc;
string PlaylistNode::GetID() const {
return this ->uniqueID;
}
string PlaylistNode::GetSongName() const {
return this ->songName;
}
string PlaylistNode::GetArtistName() const {
return this ->artistName;
}
int PlaylistNode::GetSongLength() const {
return this ->songLength;
}
I am trying to get better at understanding pointers.
Edit: I forgot to add the constructors.
Last edited on Nov 28, 2016 at 10:41pm UTC
Nov 29, 2016 at 5:18am UTC
You want to cin into the data member of the class object either directly (if it's public) or through a setter (if its private) but your code is indirecting the pointer to the function pointers of some of the class methods(GetSongName, GetSongLength etc) since it's just alluding to just the names of these methods (i.e. without the () parentheses pair)
Your program snippets don't show whether the data members are private or public but assuming they're private (the public case is considerably easier) you'd have to do something on the following lines:
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
#include <iostream>
#include <fstream>
#include <limits>
using namespace std;
class A
{
private :
int m_int;
string m_string;
public :
void setInt (istream& stream){stream>>m_int;};
void setString (istream& stream)
{
string line;
getline(stream, line);
m_string = line;
};
friend ostream& operator <<(ostream& os, const A& a);
};
int main()
{
A* a(new A);
cout<<"Enter int: \n" ;
a->setInt(cin);
cin.clear();
cin.ignore(std::numeric_limits<streamsize>::max(),'\n' );
cout<<"Enter string: \n" ;
a->setString(cin);
cout<<*a;
delete a;
}
ostream& operator <<(ostream& os, const A& a)
{
os<<a.m_int<<": " <<a.m_string<<"\n" ;
return os;
}
Last edited on Nov 29, 2016 at 5:43am UTC
Topic archived. No new replies allowed.