2015-12-19 46 views
1

所以,我必須爲大學做這個項目。我必須寫一個管理比薩餐廳訂單的程序。到目前爲止,這是我做了什麼:結構()和數組的問題C++

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

確定,但有IM的幾個問題:

1)我已經宣佈TIngredientes2爲一個數組,但是編譯器對我說:Tingredientes2沒有指定類型。我已經設法編寫TIngrediente(enum)和反之亦然的函數String,但現在我必須使2個函數讀取屏幕上的鍵盤輸入/寫入,我不知道如何使用這些函數。我寫了下面的東西,但我不知道它是否可以。

3)當涉及到讀取鍵盤輸入leer_pedido()我不知道如果這是好的,因爲結構和最重要的是,我不知道如何使用布爾值來說,如果引入的數據是正確的。

4)下一個功能是將最後一個功能leer_pedido()的數據存儲在一個列表中,我不知道。

我希望有人能幫助我

+0

你宣佈'TIngredientes2'作爲一個對象,而不是一個類型。它是'array '類型的對象。進一步在你的代碼中,你試圖把它作爲一個對象在某些地方使用,並作爲其他類型的對象。即你混淆了這兩個類別。 –

+0

事情是,我還沒有研究過對象,所以當我聲明'array '作爲Typedef時,編譯器向我發送了另一個錯誤。有林有點麻煩,因爲程序是這樣大的是用來做什麼的IM,所以... –

+0

這裏是你如何定義一個類型: '的typedef陣列<字符串,MAX_INGREDIENTES_PIZZA> TIngredientes2;' ,這裏是如何申報這種類型的對象: 'TIngredientes2 ingredientes;' –

回答

0

1)函數無法返回類型的對象「Ttingrediente2」,如果你還沒有創建類類型的「Tingrediente2」。

在你的函數:

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)您使用的測試用戶的input.Something像年初布爾變量:

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」類型,所以你可以在載體插入它直接