I'm a c++ beginner, so I have a lot of questions.

Asked 1 years ago, Updated 1 years ago, 81 views

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

class Ingredient {
    string name[6] = { "Kimchi", "Egg", "Ham", "Potato", "Cheese", "Tuna";
public:
    Ingredient();
    void getName();
    string setName() { return name[6]; }
};

Ingredient::Ingredient()
{
    cout << "Materials";
}

void Ingredient::getName()
{
    cout << ": ";
    for (int i = 0; i < 6; i++)
    {
        cout << name[i] << " ";
    }
    cout << endl;
}

class Combin : public Ingredient
{
    string names[15] = { "Kimchi pancake", "Budae jjigae", "Potato sujebi", "Fried rice", "Tuna jjigae", "Omurice", "Gratin", "Cheese ball", "Tuna mayo rice", "Potato croquet",
    "Sandwich", "Rice burger", "Fried potatoes", "Potato soup", "Tuna kimbap"};
public:
    Combin();
    string calc(string name);
};


Combin::Combin()
{
    cout << "Choose two things you like from among them.";
}

string Combin::calc(string name)
{
    string n, a;
    cin >> n >> a;
    for (int i = 0; i < 6; i++)
    {
        if (n == setName() && a == setName())
        {
            cout << name[i] << name[i]<< endl;
        }
    }

    {
        for (int i = 0; i < 6; i++)
        {
            if (name[0] && name[i + 1])
                cout << names[i];
        }

        for (int i = 0; i < 4; i++)
        {
            if (name[1] && name[i + 2])
                cout << names[i + 6];
        }

        for (int i = 0; i < 3; i++)
        {
            if (name[2] && name[i + 3])
                cout << names[i + 10];
        }

        for (int i = 0; i < 2; i++)
        {
            if (name[3] && name[i + 4])
                cout << names[i + 12];
        }

        if (name[4] && name[5])
            cout << names[14];
    }
    return 0;
}

int main()
{
    string d;
    Ingredient i;
    i.getName();
    Combin c;
    c.calc(d);
}

There's a code here. My purpose is to choose two ingredients from name[6] The goal is to take those values and have one of the dishes in names [15]. But I'm wandering because I don't know what's wrong with the error before that. Help me.

c++ programming

2022-09-21 21:14

1 Answers

//#include <iostream> // sin, cout is defined in iostream.
#include <string>
using namespace std;

class Ingredient {
    string name[6] = { "Kimchi", "Egg", "Ham", "Potato", "Cheese", "Tuna"}; // If you know vector, it is better to use vector<string> name. Or use array<string, len> name.
 /* It is preferable to initialize in the constructor.
Ingressive::Ingressive():name({"Kimchi", "Egg", "Ham", "Potato", "Cheese", "Tuna") {};
*/
public:
    Ingredient();
    voidgetName(); // It would be more appropriate to rename the function to printNames.
    stringsetName() { return name[6];} // It would be more appropriate to rename the function to getName
    // It is recommended that you replace string&getName(intindex) {return name[index];}.


};

Ingredient::Ingredient()
{
    cout << "Materials";
}

void Ingredient::getName()
{
    cout << ": ";
    For (inti = 0; i < 6; i++) // It is preferable to use it as a constant instead of literal
    {
        cout << name[i] << " ";
    }
    cout << endl;
}

class Combin : public Ingredient
{
    string names[15] = { "Kimchi pancake", "Budae jjigae", "Potato sujebi", "Fried rice", "Tuna jjigae", "Omurice", "Gratin", "Cheese ball", "Tuna mayo rice", "Potato croquet",
    "Sandwich", "Rice burger", "Fried potatoes", "Potato soup", "Tuna kimbap"};
public:
    Combin();
    string calc(string name);
};


Combin::Combin()
{
    cout << "Choose two things you like from among them.";
}

string Combin::calc(string name)
{
    String n, a; // It is better to let the name know what is stored in the same way as ingredient_1, ingredient_2. 
                //If you do not want to change the stored value, it is recommended to use const.
    cin >> n >> a;
    for (int i = 0; i < 6; i++)
    {
        if (n == setName() && a == setName())
        {
            It is ambiguous whether it is cout << name[i] << name[i]<< endl; //string name or string name[6] inherited from Ingridiant.
        }
    }

    {
        for (int i = 0; i < 6; i++)
        { // name is a private member of Ingridiant and cannot be accessed from Combin.
        // If you want to access it, use protected! Or create and use the accessor string&getName(intindex).
            If (name[0] && name[i + 1]) // && operator has a boolean type of operand. Thus, n == name[0] &&a == name[i + 1]
                cout << names[i];

        }

        for (int i = 0; i < 4; i++)
        {
            if (name[1] && name[i + 2])
                cout << names[i + 6];
        }

        for (int i = 0; i < 3; i++)
        {
            if (name[2] && name[i + 3])
                cout << names[i + 10];
        }

        for (int i = 0; i < 2; i++)
        {
            if (name[3] && name[i + 4])
                cout << names[i + 12];
        }

        if (name[4] && name[5])
            cout << names[14];
    }
    Cause of return 0; // error, if return is set to string, it must be returned to string.
}

int main()
{
    string d;
    Ingredient i;
    i.getName();
    Combin c;
    c.calc(d); 
}

I actually have a lot more to write, but I tried to write them all down, but I thought there were too many, so I did this much. To give you a tip, we recommend that you test it step by step and implement it.

For example, the constructor of the Ingridiant class and the implementation of the following. This reduces the test range, reducing the number of errors and making debugging easier.


2022-09-21 21:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.