clyp efor
clyp efor

Reputation: 19

C++ Storing a string into a class

I made a topic a bit ago regarding structs. And I know that classes have the same concept as structs. How would I go about storing a string into the class?

This is the class

class studentType
{
public:
    void setData(string, int);
    string getName() const;
    int getId() const;
private:
    string name;
    int sid;
};

void studentType::setData(string, int) {
    name = ??;
    sid = ??;
}

string studentType::getName() const {
    return name;
}

int studentType::getId() const {
    return sid;
}

The main consists of:

int main() {

    studentType object, number;

    cout << "enter name and code of item1: ";
    cin >> object.setData() >> object.setData();
    cout << "enter name and code for item2: ";
    cin >> number.setData() >> number.setData();

    system("pause");
    return 0;
}

How would I fix the cin issue? I already have string defined in the header. And yes, I know using namespace std; isn't preferred but it's for simplicity

Upvotes: 0

Views: 2132

Answers (4)

Tanner Babcock
Tanner Babcock

Reputation: 3350

What you're looking for is std::getline(). I would use cin for getting your integers, but not for the strings. I would separate the user input like this. Like @Suhaib Ahmad said, cin is space-delimited. To get a string including spaces from stdin, use std::getline().

void studentType::setData(string n, int i) {
    this->name = n;
    this->sid = i;
}

-

int main(void) {
    string n;
    int i;
    studentType object, number;

    cout << "Enter name for item1: ";
    getline(cin, n);
    cout << "Enter code for item1: ";
    cin >> i;

    object = studentType(); // make sure you construct your studentType, I also recommend using a constructor function
    object.setData(n, i);

    cout << "Enter name for item2: ";
    getline(cin, n);
    cout << "Enter code for item2: ";
    cin >> i;
    number = studentType();
    number.setData(n, i);

    system("pause");
    return 0;
}

The way you are using setData(), with no arguments given, as a place to store the cin input is incorrect. You would need to declare variables first, and then initialize your class with the variables.

Upvotes: 0

Nikos C.
Nikos C.

Reputation: 51840

int id;
string name;
cout << "enter name and code of item1: ";
cin >> name >> id;
object.setData(name, id);
cout << "enter name and code for item2: ";
cin >> name >> id;
object.setData(name, id);

You should instead use a constructor for your class though:

class studentType {
public:
    studentType(string name, int sid)
        : name(name)
        , sid(sid)
    { }
// ...

You can then initialize your objects with the correct name and ID:

cin >> name >> id;
studentType object(name, id);

But I suppose constructors will come up very soon in the tutorial or course you're currently doing.

Upvotes: 1

Suhaib Ahmad
Suhaib Ahmad

Reputation: 526

Your setData function can be as such:

void studentType::setData(string name, int sid) {
    this->name = name;
    this->sid = sid;
}

And in your main():

First cin the values and then call the setData function, i.e.

int main(){
    string name;
    int code;

    studentType object, number;
    cout << "enter name and code of item1: ";
    cin >> name >> code;

    object = studentType();
    object.setData(name, code);

    // your remaining code

}

Just a note of caution: If you use cin to get the name and code, the name can only be a single word. Say if you are writing a name of two words, the first word will be put in name and the second will be attempted to put in code.

In simpler terms, cin is space-delimited.

Upvotes: 0

Del
Del

Reputation: 1309

cin >> object.setData() >> object.setData();

the >> operator doesn't call object.setData; it takes a reference and sets the reference contents to the extracted input. The expression object.setData() calls the function, which returns void. Of course, the >> operator cannot take a void argument. Also, there aren't enough arguments in the funcall because object.setData expects a string and an int.

You should do this:

std::string name;
int num;
cin >> name >> num;
object.setData(name, num);

Upvotes: 0

Related Questions