Проблема при перегрузке оператора "<<" не читает все атрибуты - PullRequest
0 голосов
/ 10 ноября 2019

Я делаю домашнее задание для университета. Я создал класс и overloaded >> и << операторы. Я пытаюсь прочитать объект с перегруженными операторами. Я пытаюсь ввести атрибуты. Я ввожу количество сайтов, затем меня просят ввести имена сайтов, после того как я напишу имя первого, выполнение переходит к следующей инструкции, и ввод останавливается, и я не заполняю все атрибуты

СначалаЯ думал, что это проблема с выделением памяти, но, похоже, что удаление и новые выделения не являются проблемой, поэтому я попросил больше коллег, но никто не смог дать мне ответ.

Вот мой класс с включенными перегруженными операторами, у меня также есть конструкторы и деструктор для класса, я буду включать конструктор без параметров, но я не думаю, что требуется больше кода. Если это так, пожалуйста, попросите меня добавить.

#include <iostream>
#include <string>
using namespacestd;
class Game{
private: string gameName;       
         float gamePrice = 0;
         int gameNoPlatforms = 0;
         string *gamePlatforms = NULL;
         int gameNoSitesRating = 0;
         int *gameRatings = NULL;
         int gameSales[19];
         string gameLaunchers[5];   
const int gameReleaseYear;
static int noGames;
//constructor no parameters
Game():gameReleaseYear(2000)
    { 
        this->gameName = "Counter-Strike";

        this->gamePrice = 20;
        this->gameNoPlatforms = 10;
        this->gamePlatforms = new string[this->gameNoPlatforms];
        for (int i = 0; i < this->gameNoPlatforms; i++)
        {
            this->gamePlatforms[i] = "Platform" + to_string(i+1);
        }
        this->gameNoSitesRating = 5;
        this->gameRatings = new int[this->gameNoSitesRating];
        for (int i = 0; i < this->gameNoSitesRating; i++)
            this->gameRatings[i] = i + 1;
        for (int i = 0; i < 19; i++)
        {
            this->gameSales[i] = rand() % 5;
        }
        for (int i = 1; i < 5; i++)
        {
            this->gameLaunchers[i] = "Launcher" + to_string(i);
        }

        this->noGames++; //incrementare nr games
    }

friend ostream& operator<<(ostream& out, Game& g)
    {
        out << "For the game " << g.gameName << "We have the following informations:" << endl;
        out << "The game was release in the year " << g.gameReleaseYear << endl;
        out << "The full price of the game is: " << g.gamePrice << endl;
        out << "The game is running on a total number of " << g.gameNoPlatforms <<" platforms"<< endl;
        out << "Those platforms are: " << endl;
        for (int i = 0; i < g.gameNoPlatforms; i++)
        {
            out << g.gamePlatforms[i] << ' ';
        }
        out << endl;
        out << "The number of sites that are rating this game is: " << g.gameNoSitesRating << endl;
        out << "The ratings are: " << endl;
        for (int i = 0; i < g.gameNoSitesRating; i++)
        {
            out << "The site number " << i + 1 << " rated the site with" <<g.gameRatings[i] << "Stars out of 10" << endl;
        }
        out << "The sales for the game, each month are the following: " << endl;
        for (int i = 0; i < 19; i++)
        {
            out << "For the year " << i + 1 << " the game sales estimate around" << g.gameSales[i] << " millions" << endl;
        }
        out << "The launchers that are hosting this game are: " << endl;
        for (int i = 0; i < 5; i++)
        {
            out << g.gameLaunchers[i];
        }

        return out;
    }


    friend istream& operator>>(istream& in, Game& g)
    {
        cout << "The game's name: " << endl;
        in >> g.gameName;
        cout << "The game's price: " << endl;
        in >> g.gamePrice;
        cout << "The number of platforms that the game is running on: " << endl;
        in >> g.gameNoPlatforms;
        cout << "The platforms: " << endl;

            if (g.gamePlatforms != NULL)
            {
                delete[] g.gamePlatforms;
            }

            g.gamePlatforms = new string[g.gameNoPlatforms];
        for (int i = 0; i < g.gameNoPlatforms; i++)
        {
            in >> g.gamePlatforms[i];
        }
        cout << "The number of sites that are rating the game: " << endl;
        in >> g.gameNoSitesRating;
        cout << "The sites: " << endl;

            if (g.gameRatings != NULL)
            {
                delete[] g.gameRatings;
            }

        g.gameRatings = new int[g.gameNoSitesRating];
        for (int i = 0; i < g.gameNoSitesRating; i++)
        {
            cout << "The site number " << i + 1 << ' ' << endl;
            in >> g.gameRatings[i];
        }
        cout << "The sales for each year of the game: " << endl;
        for (int i = 0; i < 19; i++)
        {
            cout << "For the year " << i + 1 << ' ' << endl;
            in >> g.gameSales[i];
        }
        cout << "The game launchers: " << endl;
        for (int i = 0; i < 5; i++)
        {
            cout << "The launcher " << i + 1 << ' ' << endl;
            in >> g.gameLaunchers[i];
        }

        return in;
    }

};
int main()
{ 
Game g1;
cin>>g1;
cout<<g1;
}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...