【问题标题】:Having trouble with a college project (C++)大学项目遇到问题 (C++)
【发布时间】:2016-02-07 17:07:10
【问题描述】:

所以我在做一个大学项目,它是一个管理比萨店订单的软件,我已经编写了代码,但是当我执行程序时,它只允许我使用菜单的第一个选项(除了当我编写成分(option 1) 它崩溃了)。

当我尝试写订单或其他任何东西时它不会做任何事情,我试图找到问题但我看不到它,任何帮助将不胜感激

#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;
    string telefono;
    int numero_pedido;
    int numero_ingredientes;
    array<TIngrediente, MAX_INGREDIENTES_PIZZA> ingredientes;
};

typedef array<TPedido, MAX_PEDIDOS> listado_pedidos;

struct TPizzeria
{
    int numero_pedidos;
    listado_pedidos pedidos;
};


//typedef array<TPedido, MAX_PEDIDOS> listado_pedidos;
const array<string, MAX_INGREDIENTES_PIZZA> INGREDIENTES = {{"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 < INGREDIENTES.size() and INGREDIENTES[i] != s)
        ++i;
    return (TIngrediente)i;
}

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

void inicializar_datos(TPizzeria& p)
{
 p.numero_pedidos=0;
}



void leer_ingrediente(TIngrediente& ing)
{

bool ok=false;
string s;
cout<<"Introduce el Ingrediente a Consultar"<<endl;
while (!ok ){
    int i=0;
    cin>>s;
    s=tolower(s);
    cout<<s<<endl;
    while(i<MAX_INGREDIENTES_PIZZA){
        if (s==INGREDIENTES[i]){
            ok = true;
        }
        cout<<i;
        i++;
    }
    cout<<i<<endl;
    cout<<MAX_INGREDIENTES_PIZZA<<endl;

    if (!ok){
        cout<<"No tenemos disponible ese ingrediente, por favor introduce otro ingrediente que desees"<<endl;
    }
    else {
        ing=StrToIngrediente(s);
    }
}
}


void escribir_ingrediente(TIngrediente ing)
{
    string s;
    s=IngredienteTostr(ing);
    cout<<s<<endl;
}



void leer_pedido(TPedido& ped, bool& ok)
{
    //TPedido p;
    string pedi;
    //bool ok=true;

    getline (cin, ped.nombre_cliente);
    getline (cin, ped.telefono);
    cin >> ped.numero_pedido;
     if (ped.numero_pedido > MAX_PEDIDOS)
        ok=false;
    cin >> ped.numero_ingredientes;
    cin.ignore(100,'\n');
    if(ped.numero_ingredientes > MAX_INGREDIENTES_PIZZA)
        ok=false;
    else{

    for (int i=0; i<ped.numero_ingredientes; i++){
        leer_ingrediente(ped.ingredientes[i]);
    }
    }
    if(!ok){
        cout<<"error"<<endl;
    }

}

void insertar_pedido(TPizzeria& p, TPedido ped, bool& ok)
{
    if(p.numero_pedidos=MAX_PEDIDOS){
        ok=false;
    }
    else{
        p.pedidos[p.numero_pedidos]=ped;
        ok=true;
    }
    //if (!ok){
    //cout<<"error"<<endl;
    //}

}

void escribir_pedido(TPedido ped)
{
    cout<<ped.nombre_cliente<<endl;
    cout<<ped.telefono<<endl;
    cout<<ped.numero_pedido;
    for(int i=0; i<(ped.numero_ingredientes)-1; i++){
        escribir_ingrediente(ped.ingredientes[i]);
        cout<<endl;
    }
}

void escribir_pedidos(TPizzeria p)
{
    for (int i=0;i<p.numero_pedidos; i++){
    escribir_pedido(p.pedidos[i]);
}
}


void eliminar_pedido(TPizzeria& p, int num_pedido, bool& ok)
{
if(num_pedido <= p.numero_pedidos){
    int k=num_pedido-1;
    for (int i=0; i<(p.numero_pedidos)-1-k;i++ ){
    p.pedidos[k+i]=p.pedidos[k+i+1];
    p.pedidos[k+i].numero_pedido-=1;
    }
ok=true;
//int j=p.numero_pedidos;
//p.pedidos[j-1]="";
} else{
    ok=false;
}
}
    //if(ok=true){
        //cout<<"true"<<endl;
    //}
    //else{
        //cout<<"false"<<endl;
    //}
//}

int buscar_pedido(TPizzeria p, string nombre)
{
    int i=0;
    while(p.pedidos[i].nombre_cliente!=nombre and i<p.numero_pedidos){
        i++;
    }
    if(i<p.numero_pedidos){
        return -1;
    }
}

int frec_ingr(TPizzeria p, TIngrediente ing)
{

    int i;
    int suma=0;
    string ingrediente;
    ingrediente = tolower(ing);
    for(int i=0; i<p.numero_pedidos; i++){
        for(int k=0; k<p.pedidos[i].numero_ingredientes; k++){
            if(p.pedidos[i].ingredientes[k]==ing){

                suma++;
            }
        }
    }


    return suma;
}


void frec_ingredientes(TPizzeria p)
{
    int freq;
    TIngrediente ing;
    for(int i=0; i<INGREDIENTES.size(); i++){
        ing = StrToIngrediente(INGREDIENTES[i]);
        escribir_ingrediente(ing);
        cout<<frec_ingr(p,ing);
    }

}

int menu(){

    int m;
    cout<<"Escriba que desea realizar"<<endl;
    cout<<"1 : leer ingredientes"<<endl;
    cout<<"2 : escribir ingredientes"<<endl;
    cout<<"3: leer pedido"<<endl;
    cout<<"4: insertar pedido"<<endl;
    cout<<"5: escribir pedido"<<endl;
    cout<<"6: escribir pedidos"<<endl;
    cout<<"7: eliminar pedido"<<endl;
    cout<<"8: buscar pedido"<<endl;
    cout<<"9: frecuencia de ingrediente"<<endl;
    cout<<"10: frecuencia de todos los ingredientes"<<endl;
    cin>>m;
    return m;
}



int main()
{
    TPizzeria p;
    TPedido ped;
    TIngrediente ing;
    string nombre;
    bool ok;
    inicializar_datos(p);
    int num_pedido;
    int m=menu();
    //int m=menu();
        switch(m){
        case 1 : leer_ingrediente(ing);
        case 2 : escribir_ingrediente(ing);
        case 3 : leer_pedido(ped,ok);
        case 4 : insertar_pedido(p, ped, ok);
        case 5 : escribir_pedido(ped);
        case 6 : escribir_pedidos(p);
        case 7 : eliminar_pedido(p, num_pedido, ok);
        case 8 : buscar_pedido(p, nombre);
        case 9 : frec_ingr(p, ing);
        case 10 : frec_ingredientes(p);
        default: cout<<"no es una opción";
        break;
        }

return 0;
}

【问题讨论】:

  • 了解如何使用调试器,以及如何使用它来“实际”捕获崩溃。
  • 所以人们需要阅读整个代码并找出真正发生的情况,然后向您报告?
  • 公平地说,当您“使用第一个选项”时,您实际上是在使用所有选项,从第一个开始。请阅读switch 条款了解详情。更新:如果不为您调试代码,VS 中的静态分析器会报告在整个代码中使用未初始化内存的多种情况。这可能是坠机的原因。此外,if 子句中有一个使用赋值而不是比较的实例。
  • 您应该在每个案例之后使用break 语句,以防止执行失败到下一个案例。

标签: c++ arrays string struct


【解决方案1】:

首先你必须写下switch语句properly,因为你正在调用你的switch case的所有函数......

如果你想做多于 1 个选项,你必须将 switch 语句放在像 for, while, do-while 这样的循环中。 例如,这就是我会用一个while循环来做的方式:

while(m!=11)
{
    switch(m)
    {
    case 1 : leer_ingrediente(ing);
        break;
    case 2 : escribir_ingrediente(ing);
        break;
    case 3 : leer_pedido(ped,ok);
        break;
    case 4 : insertar_pedido(p, ped, ok);
        break;
    case 5 : escribir_pedido(ped);
        break;
    case 6 : escribir_pedidos(p);
        break;
    case 7 : eliminar_pedido(p, num_pedido, ok);
        break;
    case 8 : buscar_pedido(p, nombre);
        break;
    case 9 : frec_ingr(p, ing);
        break;
    case 10 : frec_ingredientes(p);
        break;
    case 11 : cout << "End of program..." << endl;
    default:
        cout<<"no es una opción";
    break;
    }
    m=menu();
}

为此,我在menu 函数中添加了一行以显示另一个退出程序的选项:

cout<<"11: salir del menu"<<endl;

【讨论】:

    【解决方案2】:

    您应该在每个 case 之后使用 break 语句,以防止执行落到下一个 case。

    switch(m){
    case 1 : leer_ingrediente(ing);
      break;
    case 2 : escribir_ingrediente(ing);
       break;
    case 3 : leer_pedido(ped,ok);
      break;
    case 4 : insertar_pedido(p, ped, ok);
      break;
    case 5 : escribir_pedido(ped);
      break;
    case 6 : escribir_pedidos(p);
      break;
    case 7 : eliminar_pedido(p, num_pedido, ok);
      break;
    case 8 : buscar_pedido(p, nombre);
      break;
    case 9 : frec_ingr(p, ing);
      break;
    case 10 : frec_ingredientes(p);
      break;
    default: cout<<"no es una opción";
    break;
    }
    

    【讨论】:

    • 删除了评论。
    • 请忽略之前的评论。
    • 我已经用中断重写了开关,但没有解决方案。问题是,每当我从菜单中执行一个功能时,它不允许我执行另一个功能,应该用一段时间或如果但它没有修复的东西,我一遍又一遍地锁定它,但我找不到合适的解决方案
    • switch 声明是最明显的问题。可能还有更多问题。我建议您使用调试器执行您的程序。您可能还希望使用 cout 语句来显示变量值并指示程序执行到的位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    相关资源
    最近更新 更多