Having trouble with a college project (C++)

So im doing a college project, it is a software to manage orders in a pizzeria, i already wrote the code but when i execute the program it only allows me to use the first option of the menu ( besides when i write the ingredient (option 1) it crashes).

And it wont do anything when i try to write an order or whatever, im trying to find the problem but i cant see it, any help would be appreciate





#include <iostream>
#include <array>
#include <string>
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;
};



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)
{

string pedi;


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;
}


}

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;

} else{
ok=false;
}
}

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();
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 << "salir del menu" << endl;
default:
cout<<"no es una opciĆ³n";
break;
}
m=menu();
}
return 0;
}
Hi,



The first thing I did was compile with cpp.sh http://www.cpp.sh/

Edit: Please always use code tags, it's easier for us to look at and compile with the gear icon top right of the code. http://www.cplusplus.com/articles/z13hAqkS/

I turned on all the warnings, because they are your friend :+)

Here they are:


In function 'std::string tolower(std::string)':
51:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
In function 'TIngrediente StrToIngrediente(std::string)':
61:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
In function 'void insertar_pedido(TPizzeria&, TPedido, bool&)':
147:32: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
In function 'int frec_ingr(TPizzeria, TIngrediente)':
206:5: warning: unused variable 'i' [-Wunused-variable]
In function 'void frec_ingredientes(TPizzeria)':
228:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] 226:5: warning: unused variable 'freq' [-Wunused-variable]
In function 'int buscar_pedido(TPizzeria, std::string)':
201:1: warning: control reaches end of non-void function [-Wreturn-type]
In function 'TIngrediente StrToIngrediente(std::string)':
61:10: warning: 'i' may be used uninitialized in this function [-Wmaybe-uninitialized]
In function 'int main()':
179:1: warning: 'num_pedido' may be used uninitialized in this function [-Wmaybe-uninitialized]
264:5: note: 'num_pedido' was declared here


Some things to help you out :+)

The size functions return a type of std::size_t so line 51 should be:
for (std::size_t i = 0; i < s.size(); ++i)

On line 147:

if(p.numero_pedidos=MAX_PEDIDOS){

= is for assignment , == is for comparison

In this function:

192
193
194
195
196
197
198
199
200
201
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;
}
}


The warning is because the return statement is inside the if statement, if it is false it wont be executed.


With this code, i is unitialized. A Golden Rule -> always initialize all the variables

56
57
58
59
60
61
62
63
64
TIngrediente StrToIngrediente(string s)
{
s=tolower(s);
int i;

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


Overall a good effort, there are some good techniques, better than the average beginner we see here :+)

Good Luck !!
Last edited on
TheIdeasMan (6115) seriously I tried to fix the errors but without result plz help me to make all options work
Topic archived. No new replies allowed.