所以,我必须为大学做这个项目。我必须编写一个程序来管理比萨餐厅的订单。到目前为止,这就是我所做的:
#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) 我已经设法在 TIngrediente
(枚举)和反之亦然中编写了转换 String
的函数,但现在我必须编写 2 个函数来读取键盘输入/在屏幕上写,我不知道如何使用这些功能。我在下面写了一些东西,但我不知道它是否可以。
3) 当谈到在 leer_pedido()
中读取键盘输入时,我不知道这是否可行,因为结构和最重要的是,我不知道如何使用 bool 值来判断是否引入的数据是否正确。
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) 您在开始测试用户输入时使用了 bool 变量。类似于:
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;
因此,您在代码中引导 bool 变量,因此如果可能有错误,您将它的值更改为“false”,最后它将是“false”,因此您将知道如何在代码中进一步处理它.
4) 将结构的数据存储在 vector 中是最简单的方法:
vector<Tpedido> structuresVector;
sturcturesVector.push_back(leer_pedido());
leer_pedido 函数返回 'Tpedido' 类型,因此您可以将其直接插入 vector 中
关于c++ - struct() 和数组 C++ 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34370573/