我應該使用一種結構來存儲機器中的飲料名稱,飲料成本和飲料數量。我將創建一個由五個結構組成的數組,其中包含用名稱,成本和數字初始化的元素。程序應顯示飲料列表,用戶應在第一個功能中選擇(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;
}
}
}
什麼樣的錯誤你越來越,和哪一行代碼是你得到這些錯誤? – 2013-04-30 16:59:05
有一些語法錯誤,例如:int getChoice(Drink,choice){...} Drink的變量是什麼,選擇的類型是什麼? – Amadeus 2013-04-30 17:10:58
@羅伯特哈維在主要當我嘗試getinto函數它說,飲料和選擇是不確定的。我在函數int getChoice(Drink,choice)中有錯誤。它所說的選擇在showTransaction中是未定義的和相同的。所以它通過我的方式 – user1807815 2013-04-30 17:11:34