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;
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);
}
}
}
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 :+)