0

So I'm working on this lab, and I'm trying to get the user to input the name of a game, and I want it to look like:

Input name of game x:

where x is the place in an array called games.

When I try to use

cout << "Input name of game " << games[i] << ": ";

there is an error in the less than symbols before games[i] and I cannot figure out why.

Main file:

#include <iostream>
#include <iomanip>
#include <string>
    
    #include "game.h"
    
    using namespace std;
    
    int main()
    {
        int numGames;
        int randomNumber;
        string name;
        string genre;
    
        cout << "How many games have you played in the last year?\n";
        cin >> numGames;
    
        game* games = new game[numGames];
        srand(100);
    
        for (int i = 0; i < numGames; i++)
        {
            cout << "What is the name of the game?\n";
            cin >> name;
            cout << "What is the genre of " << name << "?\n";
            cin >> genre;
    
            cout << "Input name of game " << games[i] << ": ";
    
            game* ptrObj = new game();
            ptrObj->setName(name);
            ptrObj->setGenre(genre);
            randomNumber = rand() % 10 + 1;
            ptrObj->setDifficulty(randomNumber);
            games[i] = *ptrObj;
        }
    }

game.h:

#pragma once

#include <iostream>
#include <string>

class game
{
public:
    game();
    ~game();
    void setName(std::string);
    std::string getName();
    void setGenre(std::string);
    std::string getGenre();
    void setDifficulty(int);
    int getDifficulty();
private:
    std::string name;
    std::string genre;
    int difficulty;
};

gameImp:

#include "game.h"

#include <iostream>
#include <string>

game::game()
{
    std::cout << "Creating a new game\n";
}

game::~game()
{
    std::cout << "In the game destructor\n";
}

void game::setName(std::string n)
{
    this->name = n;
}

std::string game::getName()
{
    return this->name;
}

void game::setGenre(std::string g)
{
    this->genre = g;
}

std::string game::getGenre()
{
    return this->genre;
}

void game::setDifficulty(int d)
{
    this->difficulty = d;
}

int game::getDifficulty()
{
    return this->difficulty;
}
  • I meant to comment out the lines where it says cout << "What is the name of the game?\n"; cin >> name; cout << "What is the genre of " << name << "?\n"; cin >> genre; – Conrad Speak Nov 04 '21 at 18:08
  • 1
    `std::cout` does not know how to print a `game` object. – Quimby Nov 04 '21 at 18:09
  • 1
    You need to tell the compiler how to output a `game`. – NathanOliver Nov 04 '21 at 18:09
  • You should copy and past the exact text of the error message. – drescherjm Nov 04 '21 at 18:09
  • `games[i] = *ptrObj;` that's a memory leak. This is not Java, do not use `new` unless you need to and then use `unique_ptr`. – Quimby Nov 04 '21 at 18:10
  • More specifically, you need to define a method for converting a `game` object into a string. You could define a `toString` method within the `game` class definition for this. – h0r53 Nov 04 '21 at 18:11

0 Answers0