所以,我必须为大学做这个项目。我必须编写一个程序来管理比萨餐厅的订单。到目前为止,这是我所做的:

#include <iostream>
#include <array>
#include <string>
#include <cctype>
#include <cmath>
#include <locale>
#include <algorithm>
using namespace std;
const int MAX_INGREDIENTES_PIZZA=10;
const int MAX_PEDIDOS=20;


enum TIngrediente
{
    TOMATE,
    QUESO,
    NATA,
    CEBOLLA,
    POLLO,
    HUEVO,
    SALAMI,
    ANCHOA,
    BACON,
    GAMBA
};

struct TPedido
{
    string nombre_cliente;
    int telefono;
    int numero_pedido;
    int numero_ingredientes;
    TIngrediente ingredientes;
};



typedef array<float, MAX_PEDIDOS> listado_pedidos;
const array<string, MAX_INGREDIENTES_PIZZA> TIngredientes2 = {{"tomate", "queso", "nata", "cebolla", "pollo", "huevo", "salami", "anchoa", "bacon", "gamba"}};

TIngrediente StrToIngrediente(string s);
string IngredienteTostr(TIngrediente c);
string tolower(string s);

string tolower(string s)
{
    string r = s;
    for (int i = 0; i < s.size(); ++i)
        r[i] = tolower(r[i]);
    return r;
}

TIngrediente StrToIngrediente(string s)
{
    s=tolower(s);
    int i;

     while (i < TIngredientes2.size() and TIngredientes2[i] != s)
        ++i;
    return (TIngrediente)i;
}

string IngredienteTostr(TIngrediente c)
{
    return TIngredientes2[c];
}

TIngredientes2 leer_ingrediente()
{
    TIngredientes2 r;

        for (int i=0; i<MAX_INGREDIENTES_PIZZA;i++){
            cin>>r[i];
            r[i]=tolower(r[i]);
        }
        StrToIngrediente(TIngredientes2);



        return r;
}

TIngredientes2 escribir_ingrediente()
{
TIngredientes2 s;
for(int i=0; i<s.size(); i++){
    cout<<s[i]<<endl;
}



return s;
}

TPedido leer_pedido()
{
    TPedido p;
    string ingredientes;
    bool ok=true;

    getline (cin, p.nombre_cliente);
    cin >> p.telefono;
    cin >> p.numero_pedido;
    cin >> p.numero_ingredientes;
    cin.ignore(100,'\n');
    //getline (cin, p.ingredientes);
    StrToIngrediente(ingredientes);

    //necesitamos inicializar la variable booleana
    if( numero_ingredientes > MAX_INGREDIENTES_PIZZA)
        ok=false;
    else if (numero_pedido > MAX_PEDIDOS)
        ok=false;
    else if (ingredientes != TIngrediente[i])
        ok=false;

    return p;
}


好的,但是我有一些问题:

1)我已经声明TIngredientes2为数组,但是编译器对我说Tingredientes2没有命名类型。

2)我设法编写了将String转换为TIngrediente的函数(枚举),反之亦然,但是现在我必须使2个函数读取屏幕上的键盘输入/写入,并且我不知道如何使用这些函数。我在下面写了一些东西,但我不知道是否可以。

3)当要读取leer_pedido()中的键盘输入时,由于结构和最重要的原因,我不知道这是否可以,我不知道如何使用布尔值来表示输入的数据是否正确。

4)下一个功能是将最后一个功能leer_pedido()中的数据存储在列表中,我一无所知。

我希望有一个人可以帮助我

最佳答案

1)如果您尚未创建类型为“ Tingrediente2”的类,则函数无法返回类型为“ Ttingrediente2”的对象。

在您的职能:

    TIngredientes2 escribir_ingrediente()
{
TIngredientes2 s;
for(int i=0; i<s.size(); i++){
    cout<<s[i]<<endl;
}


您在声明变量'Tingredientes s',然后在屏幕上打印出来,但没有大小(s中没有元素)。

尝试这样:

void escribir_ingrediente(array<string,MAX_INGREDIENTES_PIZZA> s)
{
for(int i=0; i<s.size(); i++)
    cout<<s[i]<<endl;
}


您将已经存在的数组传递给函数,并在其中打印出s数组的元素。

2)只需阅读有关枚举的更多信息,遍历它们并插入新元素。

3)在开始测试用户输入时使用boolean变量,如下所示:

bool good_Input = true; // suppose user's input is ok

if( p.numero_ingredientes > MAX_INGREDIENTES_PIZZA || p.numero_pedido > MAX_PEDIDOS)
    good_input=false; // input is not good if there is more ingredients than max number of ingredients


if(good_Input)
   cout<<"Input is good"<<endl;
else cout << "Input is not good"<<endl;


因此,您在代码中使用了布尔变量,因此,如果有错误,请将其值更改为“ false”,最后将其设置为“ false”,这样您将知道如何在代码中进一步处理它。

4)将结构的数据存储在矢量中是最简单的方法:

vector<Tpedido> structuresVector;
sturcturesVector.push_back(leer_pedido());


leer_pedido函数返回“ Tpedido”类型,因此您可以直接将其插入向量中

关于c++ - struct()和数组C++出现问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34370573/

10-13 07:05
查看更多