-2

I'm having trouble with a simple code I have to for class, I'm trying to figure out how to add in a user input into a class, I've tried multiple things and could really use some assistance.

Code so Far:

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

// Base class
class GameShow{
    public:
        string Name;
        cout << "Enter name of constestant: ";
        cin >> Name;
        cout << "Welcome " << Name <<"! Let's get ready to play the FEUD!!" << endl;
};


// Derived class
class FamilyFued{
    public:
    points;

};


int main ()
{
    GameShow TN;
    TN.Name
    return 0;
}
CV031
  • 3
  • 2
  • 1
    Stackoverflow is not a free teaching service. You need to learn the C++ programming by reading some of the essential C++ books. – 273K Nov 16 '20 at 03:53
  • First of all, Identify and explain the problem that needs to be solved. You cannot just copy and paste your code just ask for help. – Alex Pappas Nov 16 '20 at 05:06

3 Answers3

0

Classes are used to abstract the state and behavior of things. State is in the form of class attributes. The behaviour is in the form of class methods (functions).

This is a behaviour:

cout << "Enter name of constestant: ";
cin >> Name;
cout << "Welcome " << Name <<"! Let's get ready to play the FEUD!!" << endl;

So you need to put it inside a function:

class GameShow{
public:
    string Name;
    
    GameShow(){
        cout << "Enter name of contestant: ";
        cin >> Name;
        cout << "Welcome " << Name <<"! Let's get ready to play the FEUD!!" << endl;
    }
};
0

Based on what you have, you might want to wrap cout >> ...; cin <<...; part in a constructor:

class GameShow{
public:
    string Name;
    GameShow()
    {
        cout << "Enter name of constestant: ";
        cin >> Name;
        cout << "Welcome " << Name <<"! Let's get ready to play the FEUD!!" << endl;
    }
};

Other than that, please refer to: The Definitive C++ Book Guide and List

Ranoiaetep
  • 5,872
  • 1
  • 14
  • 39
0

You code missing too many basics, you need start with a good c++ book.

using namespace std; // is bad idea, mentioned in stack overflow thousands of times.

bad

class GameShow{
    public:
        string Name;
        cout << "Enter name of constestant: ";
        cin >> Name;
        cout << "Welcome " << Name <<"! Let's get ready to play the FEUD!!" << endl;
};

Your cout and cin function should be used inside a constructor

class GameShow{
public:
    string Name;
    GameShow()
    {
        std::cout << "Enter name of constestant: ";
        std::cin >> Name;
        std::cout << "Welcome " 
                  << Name 
                  << "! Let's get ready to play the FEUD!!" << std::endl;
    }

};

TN.Name // what this line doing in your main function ??

Assuming you wanted to print Name of GameShow class member, so change your line to below.

std::cout << TN.Name;
pvc
  • 1,070
  • 1
  • 9
  • 14