0

I was wondering if someone could help me on figuring out how to create multiple structs and also counting the number of structs created. My Code follows three blocks, header file, Television file and then the main file.

Television.h

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


class Televison {

    string Manufacturer;
    string Type; //Plasma, LCD
    string Model; //Smart, regular
    string Connection; //HDMI, VGA
    string Power;

    double Price;

    int Serial_Number;
    int Screen_size; 
    int Resolution; //larger one, so 1920 and etc.
    int Channel; //any number
    int Volume; //0 - 100

public:

    //constructor
    Televison(string Manufacturer, string Type, string Model, string Connection, string Power, double Price, int Serial_number, int Screen_size, int Resolution, int Channel, int Volume);

    //Destructor
    ~Televison();

    //Accessor Methods
    string get_Manufacturer();
    string get_Type();
    string get_Model();
    string get_Connection();
    string get_Power();

    double get_Price();

    int get_Serial_Number();
    int get_Screen_size();
    int get_Resolution();
    int get_Channel();
    int get_Volume();

    //mutator methods
    string input_Manufacturer(string Manufacturer);
    string input_Type(string Type);
    string input_Model(string Model);
    string input_Connection(string Connection);
    string input_Power(string Power);

    double input_Price(double Price);
    int input_Serial_Number(int Serial_Number);
    int input_Screen_size(int Screen_size);
    int input_Resolution(int Resolution);
    int input_Channel(int Channel);
    int input_Volume(int Volume);

    
    string change_Power(string Power);
    int change_Channel(int Channel);
    int change_Volume(int Volume);

};
static int num_tel = 1; //number of Televisons that will be incremented

Television.cpp

#include <iostream>
#include <string>
#include "Televison.h"
using namespace std;

//constructor
Televison::Televison(string Manufacturer, string Type, string Model, string Connection, string Power, double Price, int Serial_number, int Screen_size, int Resolution, int Channel, int Volume) {
    
    this->Manufacturer = Manufacturer;
    this->Type = Type;
    this->Model = Model;
    this->Connection = Connection;
    this->Power = Power;
    this->Price = Price;
    this->Serial_Number = Serial_Number;
    this->Screen_size = Screen_size;
    this->Resolution = Resolution;
    this->Channel = Channel;
    this->Volume = Volume;
    num_tel++;
    
    // initiliazes the variables to default values
    
    Manufacturer = "Sony";
    Type = "Smart";
    Model = "Plasma";
    Connection = "HDMI";
    Power = "ON";
    Serial_Number = 1234;
    Price = 199.00;
    Screen_size = 50;
    Resolution = 1440;
    Channel = 100;
    Volume = 100;
}

//Destructor
Televison::~Televison() {
    cout << "The " << Manufacturer << " " << Model << " has finished shutting down." << endl;
    num_tel--;
    cout << "The inventory of television is now: " << num_tel << endl;
}

//Get Methods
string Televison::get_Manufacturer() {
    return Manufacturer;
}
string Televison::get_Model() {
    return Model;
}
string Televison::get_Type() {
    return Type;
}
int Televison::get_Serial_Number() {
    return Serial_Number;
}
string Televison::get_Connection() {
    return Connection;
}
string Televison::get_Power() {
    return Power;
}
double Televison::get_Price() {
    return Price;
}
int Televison::get_Screen_size() {
    return Screen_size;
}
int Televison::get_Resolution() {
    return Resolution;
}
int Televison::get_Channel() {
    return Channel;
}
int Televison::get_Volume() {
    return Volume;
}

//mutator methods
int Televison::change_Channel(int Channel1) {

    if (Channel > 0)
    {
        Channel = Channel1;
        return Channel;
    }
    else
    {
        cout << "Invalid Channel, please enter another number" << endl;
        return Channel;
    }

}
int Televison::change_Volume(int Volume1) {
    if (Volume1 > 0)
    {
        Volume = Volume1;
        return Volume;
    }
    else
    {
        cout << "Invalid volume, please enter another number" << endl;
        return Volume;
    }
}
string Televison::change_Power(string Power1) {
    if (Power1 =="Y")
    {
        Power = Power1;
        return Power;
    }
    else
    {
        return Power;
    }
}

string Televison::input_Connection(string Connection1) {
    Connection = Connection1;
    return Connection;
}
string Televison::input_Type(string M) {
    Type = M;
    return Type;
}
string Televison::input_Manufacturer(string M) {
    Manufacturer = M;
    return Manufacturer;
}
/*
string Televison::Manufacturer_code(string M,string N) {
    string J = M.substr(0,4);
    string Manufacturer_code = J + N;
    return Manufacturer_code;
}
*/
string Televison::input_Model(string M) {
    Model = M;
    return Model;
}
string Televison::input_Power(string M) {
    Power = M;
    return Power;
}

double Televison::input_Price(double M) {
    Price = M;
    return Price;
}

int Televison::input_Serial_Number(int M) {
    Serial_Number = M;
    return Serial_Number;
}

int Televison::input_Screen_size(int M) {
    Screen_size = M;
    return Screen_size;
}
int Televison::input_Resolution(int M) {
    Resolution = M;
    return Resolution;
}
int Televison::input_Channel(int M) {
    Channel = M;
    return Channel;
}
int Televison::input_Volume(int M) {
    Volume = M;
    return Volume;
}

Source.cpp

#include <iostream>
#include <string>
#include "Televison.h"
using namespace std;

void displayStatus(Televison& Televison) {
    cout << "Inventory of Televisons: " << num_tel << endl;
    cout << "The Televison serial number: " << Televison.get_Serial_Number() << " " << Televison.get_Manufacturer() << " " << Televison.get_Model() << " " << Televison.get_Type() << " with " << Televison.get_Connection() << " ";
    cout << "\n with " << Televison.get_Power() << " " << Televison.get_Price() << " with " << Televison.get_Screen_size() << " screen size " << Televison.get_Resolution() << "p " << "with " << Televison.get_Channel() << " channel " << Televison.get_Volume() << " Volume " << endl;
}

int main() {

    {
        Televison Televison_1("Sony", "Smart", "Plasma", "HDMI", "ON", 199.00, 1234, 50, 1440, 100, 100); //default Televison

        cout << "This is the Televison program for personal Televison: " << endl;

        bool repeat = true;
        do {
            
                cout << "Please enter the Manufacturer for the Televison: " << endl;
                string Manufacturer;
                getline(cin, Manufacturer);
                int M1 = Manufacturer.length();
                if (M1 < 1)
                {
                    cout << "No input, please try again" << endl;
                    repeat = false;
                }
                else
                    repeat = true;
                Televison_1.input_Manufacturer(Manufacturer);
            
        } while (!repeat);

        repeat = true;
        do {
            
                cout << "Please enter the Type of the Televison: " << endl;
                string Type;
                getline(cin, Type);
                int M3 = Type.length();
                if (M3 < 1)
                {
                    cout << "No input, please try again" << endl;
                    repeat = false;
                }
                else
                    repeat = true;
                Televison_1.input_Type(Type);
        } while (!repeat);

        repeat = true;
        do {
            
                cout << "Please enter the Model for the Televison: " << endl;
                string Model;
                getline(cin, Model);
                int M2 = Model.length();
                if (M2 < 1)
                {
                    cout << "No input, please try again" << endl;
                    repeat = false;
                }
                else
                    repeat = true;
                Televison_1.input_Model(Model);

        } while (!repeat);

        cout << "Please enter the Connection for the Televison: " << endl;
        string Connection;
        cin >> Connection;
        Televison_1.input_Connection(Connection);

        cout << "Enter the Serial Number: " << endl;
        int inp4;
        cin >> inp4;
        Televison_1.input_Serial_Number(inp4);

        cout << "Enter the Screen size: " << endl;
        cin >> inp4;
        Televison_1.input_Screen_size(inp4);

        cout << "Enter the Resolution: " << endl;
        cin >> inp4;
        Televison_1.input_Resolution(inp4);

        int a = 0;
        while (a < 1)
        {
            cout << "Please enter the Price for the Televison: " << endl;
            double Price;
            cin >> Price;
            if (Price > 0) //address speed must be greater than 0
            {
                Televison_1.input_Price(Price);
                a++;
            }
            else
                cout << "Invalid, enter again." << endl;
        }

        displayStatus(Televison_1);

        int b = 0;
        while (b < 1)
        {
            cout << "Would you like to Power your Televison? (Y/N)" << endl;
            char input1;
            cin >> input1;
            if (input1 == 'Y' || input1 == 'y')
            {
                Televison_1.change_Power("ON");
                b++;
            }
            else if (input1 == 'N' || input1 == 'n')
            {
                Televison_1.change_Power("OFF");
                b++;
            }
            else
                cout << "Invalid, enter again." << endl;
        }

        displayStatus(Televison_1);
        int n = 0;
        int n1 = 0;
        while (n < 1)
        {
            cout << "Would you like to change any of the following attributes of your Televison? (Y/N)";
            string input2, input3;
            int input4;
            double input5;
            cin >> input2;
            if (input2 == "Y" || input2 == "y")
            {
                while (n1 < 1) {


                    cout << "Which Televison attribute would you like to change? (Enter the respective number): " << endl;
                    cout << "1: Manufacturer" << endl;
                    cout << "2: Type" << endl;
                    cout << "3: Model" << endl;
                    cout << "4: Serial Number" << endl;
                    cout << "5: Connection" << endl;
                    cout << "6: Power" << endl;
                    cout << "7: Channel" << endl;
                    cout << "8: Screen size" << endl;
                    cout << "9: Resolution" << endl;
                    cout << "10: Volume" << endl;
                    cout << "11: Price" << endl;
                    int choice;
                    cin >> choice;
                    bool repeat1 = true;
                    bool repeat2 = true;
                    bool repeat3 = true;
                    switch (choice)
                    {
                    case 1:
                        cout << "Enter the Manufacturer: " << endl;
                        cin >> input3;
                        Televison_1.input_Manufacturer(input3);
                        break;
                    case 2:
                        cout << "Enter the Type: " << endl;
                        cin >> input3;
                        Televison_1.input_Type(input3);
                        break;
                    case 3:
                        cout << "Enter the Model: " << endl;
                        cin >> input3;
                        Televison_1.input_Model(input3);
                        break;
                    case 4:
                        cout << "Enter the Serial Number: " << endl;
                        cin >> input4;
                        Televison_1.input_Serial_Number(input4);
                        break;
                    case 5:
                        cout << "Enter the Connection: " << endl;
                        cin >> input3;
                        Televison_1.input_Connection(input3);
                        break;
                    case 6:
                        cout << "Enter the power: " << endl;
                        cin >> input3;
                        Televison_1.input_Power(input3);
                        break;
                    case 7:
                        cout << "Enter the Channel " << endl;
                        cin >> input4;
                        Televison_1.input_Channel(input4);
                        break;
                    case 8:
                        cout << "Enter the Screen size: " << endl;
                        cin >> input4;
                        Televison_1.input_Screen_size(input4);
                        break;
                    case 9:
                        do {
                            cout << "Enter the Resolution: " << endl;
                            cin >> input4;
                            if (input4 < 0)
                            {
                                cout << "Please enter a speed above 0" << endl;
                                repeat1 = false;
                            }
                            else
                            {
                                Televison_1.input_Resolution(input4);
                                repeat1 = true;
                            }
                        } while (!repeat1);
                        break;
                    case 10:
                        do {
                            cout << "Enter the Volume: " << endl;
                            cin >> input4;

                            if (input4 >= 0) {
                                cout << "Please enter a valid volume" << endl;
                                repeat2 = false;
                            }
                            else
                            {
                                repeat2 = true;
                                Televison_1.change_Volume(input4);
                            }
                        } while (!repeat2);
                        break;
                    case 11:
                        do {
                            cout << "Enter the Price: " << endl;
                            cin >> input5;
                            if (input5 > 0)
                            {
                                Televison_1.input_Price(input5);
                                repeat3 = true;
                            }
                            else
                            {
                                cout << "Please enter a valid price" << endl;
                                repeat3 = false;
                            }
                        } while (!repeat3);

                        break;
                    }
                    displayStatus(Televison_1);
                    cout << "Would you like to change another attribute? (Y/N)" << endl;
                    cin >> input3;
                    if (input3 == "Y" || input3 == "y")
                    {

                    }
                    else
                        n1++;

                }

                n++;
            }
            else if (input2 == "N" || input2 == "n")
            {
                cout << "The Televison will begin to shut down." << endl;
                n++;
                displayStatus(Televison_1);
            }
            else
                cout << "Invalid, enter again." << endl;
        }

    }
    // object goes out of scope - the destructor which shuts down the Televison



    system("pause");
    return 0;
}

Basically, my code only allows me to create one object and modify it. However, I would like to add the ability to create multiple objects and have a counter for it. I tried adding a counter, but it seems that doesn't work very well... I don't need someone to solve it, but if someone could point me to the right direction, that would be great.

  • That's a lot of code to wade through, for a problem that likely boils down to you defining `num_tel` as `static` inside the header. Instead it should be declared `extern` and then defined in your CPP file. [See this answer](https://stackoverflow.com/a/14349950/1553090) for more detail. – paddy Feb 02 '21 at 01:59
  • You probably simply want a collection such as `std::vector`. You can get size and iterate from it. – Jarod42 Feb 02 '21 at 09:49

0 Answers0