struct()和数组C ++

时间:2015-12-19 12:09:45

标签: c++ arrays struct

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

#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(enum)中转换TIngrediente的函数,反之亦然,但现在我必须在屏幕上创建2个函数来读取键盘输入/写入,我不知道如何使用这些功能。我在下面写了一些东西,但我不知道它是否正常。

3)当谈到在leer_pedido()中读取键盘输入时我不知道是否因为结构是可行的而且最重要的是,我不知道如何使用布尔值来说明引入的数据是否正确或不。

4)下一个功能包括将最后一个功能leer_pedido()中的数据存储在一个列表中,我不知道。

我希望有人可以帮助我

1 个答案:

答案 0 :(得分:0)

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)你在测试用户输入的开头就使用了布尔变量。例如:

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'类型,因此您可以直接将其插入向量