You are here:

C++/C++ classes

Advertisement


Question
Hi. I am trying to ask user for input which is string type. Then I use pointer to point to that string address. Then I use pointer again to name the class by using the value that the pointer contains. Though software does not allow me to do that because it now allowed to convert variable types... I also have the program here:

#include <iostream>
#include <string>
using namespace std;
class account
{
     public:
     int age;
     char gender;
     char name;
};
int main()
{
string internal_user_input;
string *UI_ptr;
cout << "Please specify name: ";
cin >> internal_user_input;
UI_ptr=&internal_user_input;          account *UI_ptr;  //I GET THE ERROR HERE
}
system("pause");
}

//Thank you soooo much for the help bro!


Answer
Hey bro!

In the line
string* UI_ptr;
you are declaring that UI_ptr is a pointer to a string.
Later in the code is says
account* UI_ptr;
which declares that UI_ptr is a pointer to an account.

You cannot have 2 declarations for UI_ptr in the same code block.

I suppose you are trying to create an account with a particular name.

You need to create a constructor in class account, and pass the internal_user_input to the constructor as a parameter. In the constructor you need to assign the parameter to the name in the class. The class needs string name, not char name. Char is just 1 character.

Best regards.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2016 About.com. All rights reserved.