Semanti c ошибка в условии выбора типа светодиодных ламп - PullRequest
0 голосов
/ 04 марта 2020

У меня есть класс лампочек. В этом классе есть методы и конструкторы. Есть даже деструктор) Проблема в том, что мне нужно определить и отобразить информацию о членах класса с типом «n» в методе TEST () (светодиодные лампы). Для реализации этой задачи он разработал метод gettype (), который возвращает тип объекта, и, фактически, метод TEST (), который отображает информацию о лампочках.

Проблема в том, что ничего работает для меня. Я много чего перепробовал, но мне не удалось реализовать эту задачу. Я новичок в программировании (

Код:

#include <iostream>
using namespace std;

class lamp 
{
public:
    // methods
    void TEST(void);
    char* gettype (void);
    void INIT(void);
    void SHOW(void);
    // construcrors
    lamp();
    lamp(const char *t, int powe, const char *c, double cos);
    lamp(const lamp & obj);
    // destructor
    ~lamp();
private:
    // data
    char type[100]; // LED, energy-saving or incandescent lamp 
    int power;      // LED lamp - "n"
    char color[100];
    double cost;
};

lamp::lamp() {
    cout << "This object was created in the default constructor.\n";
    strcpy(type, "");
    power = 0;
    strcpy(color, "");
    cost = 0;
}

lamp::lamp(const char *t, int powe, const char *c, double cos) {
    cout << "This object was created in the constructor with parameters.\n";
    strcpy(type, t); //*t
    power = powe;
    strcpy(color, c); //*c
    cost = cos;
}

lamp::lamp(const lamp & obj) {
    cout << "This object was created in the copy constructor.\n";
    strcpy(type, obj.type);
    power = obj.power;
    strcpy(color, obj.color);
    cost = obj.cost;
}

lamp::~lamp() {
    cout << "Deletion of object by destructor.\n";
}

void lamp::SHOW(void) {
    cout << "Lamp Information:\n";
    cout << "\nType > " << type;
    cout << "\nPower > " << power;
    cout << "\nColor > " << color;
    cout << "\nCost > " << cost << endl;
}

void lamp::INIT(void) {
    cout << "Enter lamp information:\n";
    cout << "\nType (if LED, then n) > "; cin >> type;
    cout << "\nPower > "; cin >> power;
    cout << "\nColor > "; cin >> color;
    cout << "\nCost > "; cin >> cost;
}

char* lamp::gettype (void) {
    return type;
}

void lamp::TEST(void) {
    cout << "\nType > " << type;
    cout << "\nPower > " << power;
    cout << "\nColor > " << color;
    cout << "\nCost > " << cost << endl;
}

void main() {
    setlocale(0, "");

    // default constructor for 1 class instance
    lamp l1;
    cout << "Entering data for the first product." << endl;
    l1.INIT();

    // constructor with parameters for 2 class instances
    cout << endl << "Information about the second object: \n"; 
    lamp l2("n", 950, "yellow", 1580);

    // copy constructor for the third object
    cout << endl << "Information about the third object: \n";
    lamp l3(l2);

    // Derived information about all the lamps using the method SHOW
    l1.SHOW();
    l2.SHOW();
    l3.SHOW();

    // I create an array of two objects using the default constructor
    lamp la[2];
        I enter data into an array of objects using the method INIT
    cout << "Fill an array of objects with 2 elements." << endl;
    for(int i = 0; i < 2; i++) {
        la[i].INIT();
    }
        // I output data from an array of objects using the method SHOW
    cout << "Showing items." << endl;
    for (int i = 0; i < 2; i++) {
        la[i].SHOW();
    }

    // looking for and displaying information about LED lamps
    cout << "Search and display information about LED lamps." << endl;
    for (int i = 0; i < 3; i++) {
        if (la[i].gettype() == "n") {
            cout << endl << " lamp number : " << (i + 1) << endl;
            la[i].TEST();
            cout << endl;
        }
    }
    system("pause");
}

1 Ответ

0 голосов
/ 04 марта 2020

Есть несколько ошибок в вашем коде:

  1. strcpy включено в <cstring>, что пропущено. Вы должны добавить его в начале:
#include <cstring>
main() функция должна быть объявлена ​​как int main(), и вам необходимо добавить return оператор
int main() {

    //YOUR CODE HERE

    return 0;
}
Вы пропустили знак комментария в строке 104
    lamp la[2];
    //I enter data into an array of objects using the method INIT
    cout << "Fill an array of objects with 2 elements." << endl;

После исправления ваш код должен работать.

...