0

I have this simple code for Class training. It mostly looked up from a book though. What I'm trying to do is take user input on gradebook1 and gradebook2 object. I don't know even if it's possible so I'm seeking your precious help.

#include <iostream>
#include <string>
using namespace std;

class GradeBook{
public:

    void setCourseName(string name)
    {
        courseName = name;
    }
    
    string getCourseName() const
    {
        return courseName;
    }
    
    void displayMessage() const{
        cout << "Welcome to the gradebook for\n" << getCourseName() << "!" << endl;
    }

private:
    string courseName;  
};

int main(){
    GradeBook gradebook1;
    GradeBook gradebook2;

    
    cout << "gradeBook1 created for course: " << gradebook1.getCourseName() << "\ngradeBook2 created for course: " << gradebook2.getCourseName() << endl;
}
zebercet
  • 13
  • 4
  • You wrote a `displayMessage()` function. Couldn't you wrote an `input()` function as well which uses `std::cin` to input the members? – Scheff's Cat Jun 28 '21 at 08:37
  • Or better yet, overload `<<` and `>>` operators, so you class isn't dependant on specifically `std::cout` and `std::cin`. – YSC Jun 28 '21 at 08:42
  • See https://stackoverflow.com/q/4421706/5470596 – YSC Jun 28 '21 at 08:43
  • Do you have any questions or comments on the proposed solution below? – rawrex Jun 28 '21 at 08:52

1 Answers1

0

Almost the same as your displayMessage(), only that is should not be a const member:

std::istream& input(std::istream& is = std::cin) { // Default argument 
    is >> courseName;
    return is; // It's good practice to return a reference to input stream
}

Considering this, you may want to change your displayMessage():

std::ostream& displayMessage(std::ostream& os = std::cout) const {
    // Minimal formatting, no newlines
    os << "Welcome to the gradebook for " << getCourseName() << "!";
    return os;
}

In this configuration, you can do like this (and more), which would not be possible in case of void return:

int main() {
    GradeBook book;
    while(book.input()) /* do something */;
}

If you'll keep going further in learning C++, you will get to know how you can use the >> and << operators on your own classes - Operator overloading.

Spoiler: almost the same as we did here, only with special function names.

Update

A simplified version of the input:

void input() {
    cout << "Enter a course name: "; // Print a prompt
    // cin >> courseName;
    getline(cin, courseName);
}
rawrex
  • 4,044
  • 2
  • 8
  • 24
  • Thank you all for your helpful comments and advices. I'm nothing more than a beginner it's hard for me to implement those. But I took notes and will get back to it after I get better at C++. – zebercet Jun 28 '21 at 09:18
  • @zebercet understandable! You can strip off most of what's in here. Or even better, I will edit in a simplified version of the input. – rawrex Jun 28 '21 at 09:22
  • So it had to be "cin >> courseName;". I tried getline (cin, gradebook1.getCourseName()); and of course it didn't work. – zebercet Jun 28 '21 at 09:44
  • @zebercet it does not have to be, you can use `getline()` but you can't use it on your `getCourseName()`, you have to use it directly. I will edit in. – rawrex Jun 28 '21 at 09:48
  • Okay now stuff started to make sense. Can I use this on int main()? I want my program to work like this: - User enters a data. - The output goes as "gradeBook1 created for: (user's data). Or am I doing this whole class stuff wrong? Because I was trying to see what can I do with classes and wanted to add stuff I learned earlier on. – zebercet Jun 28 '21 at 10:11
  • @zebercet you define a class, then you can make instances of this class and use them, including using them in `main()`. – rawrex Jun 28 '21 at 10:12