2013-04-30 71 views
0

我應該使用一種結構來存儲機器中的飲料名稱,飲料成本和飲料數量。我將創建一個由五個結構組成的數組,其中包含用名稱,成本和數字初始化的元素。程序應顯示飲料列表,用戶應在第一個功能中選擇(1-6)。該選擇被驗證並通過值傳遞迴主例程。在第二個功能中,用戶插入錢幣並顯示變化量,並且應該從機器中飲料的數量中減去一個,並循環。當用戶退出時,它顯示機器所賺的總金額。我的問題是將數組傳遞給函數。我認爲我做對了,但是隨着函數和數組的出現,我得到了錯誤。有誰知道我將如何傳遞數組並返回函數的值?由於如何正確傳遞這些值?

#include <iomanip> 
#include <iostream> 
#include <string> 
using namespace std; 


struct Drink 
{ 
string drinkName; 
double cost; 
int numberInMachine; 
}; 

struct Drink options[] = {{"Cola", .75, 0}, {"Root Beer", .75, 2}, 
     {"Lemon-Lime", .75,  10}, 
         {"Grape Soda", .80, 3}, {"Cream Soda", .80, 20}}; 

int getChoice(Drink, int); 
void showTransaction(Drink&); 

int main() 
{ 
const int NUM_DRINKS = 5; // Number of drink options 
Drink options[NUM_DRINKS]; 

getChoice(Drink, value); 
showTransaction(choice); 


system("pause"); 
return 0; 
} 

int getChoice(Drink, choice) 
{ 
int choice; 

cout << "Enter the number(1-6) of the drink you would like: " << endl; 
cout << "Drink Name   Cost  Number in Machine " << endl; 
cout << "1. Cola   .75    " << endl; 
cout << "2. Root Beer  .75    " << endl; 
cout << "3. Lemon-lime  .75    " << endl; 
cout << "4. Grape Soda  .80    " << endl; 
cout << "5. Cream Soda  .80    " << endl; 
cout << "6. Quit " << endl; 

cout << " Enter the number of your selection: "; 
cin >> choice; 

while(choice != 1 && choice != 2 && choice != 3 && choice != 4 
      && choice != 5 &&  choice != 6) 
{ 
    cout << "Please enter a valid number 1-6" << endl; 
    cin >> choice; 
} 

return choice; 
} 

void showTransaction(choice) 
{ 
double moneyIn; 
double moneyOut; 

if(choice ==1) 
{ 
    cout << "Enter money inserted up to $1.00: "; 
    cin >> moneyIn; 
    while(moneyIn < options[0].cost) 
    { 
     cout << "Enter correct amount" << endl; 
     cin >> moneyIn; 
    } 

    if(moneyIn > options[0].cost) 
    { 
     cout << "Your change is: " << (moneyIn - options[0].cost) << endl; 
    } 

} 

} 
+2

什麼樣的錯誤你越來越,和哪一行代碼是你得到這些錯誤? – 2013-04-30 16:59:05

+1

有一些語法錯誤,例如:int getChoice(Drink,choice){...} Drink的變量是什麼,選擇的類型是什麼? – Amadeus 2013-04-30 17:10:58

+0

@羅伯特哈維在主要當我嘗試getinto函數它說,飲料和選擇是不確定的。我在函數int getChoice(Drink,choice)中有錯誤。它所說的選擇在showTransaction中是未定義的和相同的。所以它通過我的方式 – user1807815 2013-04-30 17:11:34

回答

1

我有你的代碼中固定的一些事情。我已經評論了大部分內容;請仔細閱讀並理解爲什麼我有我的所作所爲。

主要的事情需要注意的是:

  1. 您聲明數組全局叫options,所以也沒有必要,因爲他們有機會獲得它
  2. 在這個數組傳遞給您的任何功能你正在處理數組及其索引,不需要使用if語句來找出客戶正在談論的內容;我們可以使用選擇 - 1作爲索引到數組(你會知道我說的是閱讀代碼後)

固定碼:

#include <iomanip> 
#include <iostream> 
#include <string> 
using namespace std; 

// Structure to hold information about drink 
struct Drink 
{ 
    string drinkName; 
    double cost; 
    int numberInMachine; 
}; 

// Essentially the machine with information about what is in it 
Drink options[] = {{"Cola", .75, 0}, {"Root Beer", .75, 2}, {"Lemon-Lime", .75, 10},{"Grape Soda", .80, 3}, {"Cream Soda", .80, 20}}; 

int getChoice(); 
double showTransaction(int); 

int main() 
{ 
    int choice; 
    double moneyEarned = 0.0; 

    // Figuring out what the cutomer chose 
    choice = getChoice(); 

    // Figuring out how much money the machine earned 
    moneyEarned = showTransaction(choice); 

    cout << "The machine earned: $" << moneyEarned << "." << endl; 
    return 0; 
} 

int getChoice() 
{ 
    int choice; 

    cout << "Enter the number(1-6) of the drink you would like: " << endl; 
    cout << "Drink Name   Cost  Number in Machine " << endl; 
    cout << "1. Cola   .75   " << options[0].numberInMachine << endl; 
    cout << "2. Root Beer  .75   " << options[1].numberInMachine << endl; 
    cout << "3. Lemon-lime  .75   " << options[2].numberInMachine << endl; 
    cout << "4. Grape Soda  .80   " << options[3].numberInMachine << endl; 
    cout << "5. Cream Soda  .80   " << options[4].numberInMachine << endl; 
    cout << "6. Quit " << endl; 

    cout << "Enter the number of your selection: "; 
    cin >> choice; 

    while(choice < 1 || choice > 6) 
    { 
     cout << "Please enter a valid number 1-6" << endl; 
     cin >> choice; 
    } 

    return choice; 
} 

double showTransaction(int choice) 
{ 
    double moneyIn; 

    // If there isn't enough drinks ie. more than 0, then we can't sell any 
    if(options[choice-1].numberInMachine < 1) 
    return 0.0; 

    cout << options[choice-1].drinkName << "costs $" << options[choice-1].cost << "." << endl; 
    cout << "Enter money inserted up to $1.00: "; 
    cin >> moneyIn; 

    // If they enter less money than we need 
    while(moneyIn < options[choice-1].cost) 
    { 
     cout << "The entered money is not enough, Please enter more: "; 
     cin >> moneyIn; 
    } 

    cout << "Your change is: $" << (moneyIn - options[choice-1].cost) << "." << endl; 

    return moneyIn; 
} 
+0

哦,謝謝!我只是不能確定普通數組和結構數組之間的區別。謝謝,但它真的幫助我真正理解它 – user1807815 2013-04-30 20:23:38

0

在主,「喝」是一種類型,你正在嘗試使用它作爲一個變量,「選擇」一直沒有 聲明。我不明白你爲什麼要將任何東西傳遞給「getchoice」,因爲函數確實使用了它的參數 。此外,您還有一個「showTransaction」 函數原型的簽名和其他實現。

修復所有這一切開始。你可能也想讀這個。

http://www.cprogramming.com/tutorial/lesson4.html

+0

在原型中我有void showTransaction([],int,int);並在主要我showTransaction(選項,NUM_DRINKS,選擇);原型中的結構數組是什麼類型?它不能是int – user1807815 2013-04-30 17:59:16