我給了一個問題域。編寫一個程序,詢問用戶想要購買哪些水果以及數量。根據用戶的輸入,它會產生一張賬單。 交互示例: 是否要購買水果?是創建2個2結構的數組。
哪個水果?橙色
多少磅? 2
你想購買另一種水果嗎?是
哪個水果? Apple
多少磅? 1.5
你想購買其他水果嗎?沒有
感謝您的業務。
這是您的賬單:每磅價格
橙2磅1.5 3
蘋果1.5磅2 3
總金額6
#include <iostream>
#include <string>
using namespace std;
struct fruitData
{
string fruitName;
float price;
};
struct invoiceData
{
char fruit_ordered;
float price1;
float quantity_ordered;
float totalFruitprice;
} ;
int main()
char choice;
string fruit_choice;
float pounds_choice;
int count=0;
fruitData fruits[5];
invoiceData customers[5];
strcpy(fruits[0].fruitName, "Banana");
strcpy(fruits[1].fruitName, "Apple");
strcpy(fruits[2].fruitName, "Pears");
strcpy(fruits[3].fruitName, "Oranges");
strcpy(fruits[4].fruitName, "Papaya");
fruits[0].price=1;
fruits[1].price=2;
fruits[2].price=2.5;
fruits[3].price=1.5;
fruits[5].price=1.40;
strcpy(customers[0].fruitName, "Banana");
strcpy(customers[1].fruitName, "Apple");
strcpy(customers[2].fruitName, "Pears");
strcpy(customers[3].fruitName, "Oranges");
strcpy(customers[4].fruitName, "Papaya");
customers[0].price=1;
customers[1].price=2;
customers[2].price=2.5;
customers[3].price=1.5;
customers[4].price=1.40;
cout << "Welcome to the fruit market" << endl;
cout << "Would you like to purchase fruit?" << endl;
cin >> choice >> endl;
while (choice == 'Y')
{
cout << "Which fruit?" << endl;
cin >> fruit_choice >> endl;
cout << "How many pounds?" << endl;
cin >> pounds_choice >> endl;
if (fruit_choice == "Banana")
{
customers[0].quantity_ordered = pounds_choice;
customers[0].total_price = customers[0].quantity_ordered * customers[0].price;
}
else if (fruit_choice == "Apple")
{
customers[1].quantity_ordered = pounds_choice;
customers[1].total_price = customers[1].quantity_ordered * customers[1].price;
}
else if (fruit_choice == "Pears")
{
customers[2].quantity_ordered = pounds_choice;
customers[2].total_price = customers[2].quantity_ordered * customers[2].price;
}
else if (fruit_choice == "Oranges")
{
customers[3].quantity_ordered = pounds_choice;
customers[3].total_price = customers[3].quantity_ordered * customers[3].price;
}
else (fruit_choice == "Papaya")
{
customers[4].quantity_ordered = pounds_choice;
customers[4].total_price = customers[4].quantity_ordered * customers[4].price;
}
}
for (count = 1 ; count <=5 ; count++)
{
if (customers[0].total_price != 0)
{
cout << customers[0].fruit_name <<
cout << customers[0].quantity_ordered <<
cout << customers[0].price <<
cout << customers[0].total_price <<
}
}
}
水果數量價格
我a m得到很多錯誤。其中之一是「string fruit_chioce」中的字符串不會變成藍色。編譯器說char選擇之前應該有一個「;」 。我也無法爲這些結構關聯數組。任何幫助將不勝感激,謝謝。
沒有開頭{for main()for starters。 – codah
水果[5]如水果[5]。價格超出界限。 –
你不能(很容易)混合使用C++字符串和'C'字符串。堅持C++。嘗試'fruits [0] .fruitName =「香蕉」等。或者更好,初始化一個數組爲'fruitData fruits [] = {「Banana」,「Apple」,...};' – codah