2017-03-18 53 views
-1

我需要創建數組來保存用戶選擇的項目數量,並打印出包含產品,數量和總價格的收據。請幫我理解如何做到這一點。我對數組是什麼有了基本的瞭解。我只是無法弄清楚如何保存用戶輸入。如何使用數組向客戶詢問他們想要的數量並從數組中打印收據?

#include <iostream> 
#include <Windows.h> 
#include <cstdlib> 
#include <string> 
#include "customerclass.h" 
using namespace std; 


//***** Functions to calculate the price of multiple items ***** 
void finalPrice1(int itemQuantity) { 
    float price; 
    price = itemQuantity * 3.00; 
    cout << "Your total is $" << price << endl; 
    cout << "Thank you for using my shop" << endl; 
    exit(0); 
} 
void finalPrice2(int itemQuantity) { 
    float price; 
    price = itemQuantity * 2.50; 
    cout << "Your total is $" << price << endl; 
    cout << "Thank you for using my shop" << endl; 
    exit(0); 
} 
void finalPrice3(int itemQuantity) { 
    float price; 
    price = itemQuantity * 1.25; 
    cout << "Your total is $" << price << endl; 
    cout << "Thank you for using my shop" << endl; 
    exit(0); 
} //***** End of functions that calculate price of multiple items ***** 




int main(void) 
{ 
    char selection = ' '; 
    string lname = ""; 
    string luserAddress; 
    int itemQuantity; 
    string orderFinalized; 
    CustomerInfo myCustomerInfo; 

    do 
    { // Displaying menu 
     cout << "Hello, welcome to my online shop! What is your name? " << endl; 
     cin >> lname; 
     cout << " And what is your shipping address? " << endl; 
     cin >> luserAddress; 
     myCustomerInfo.setName(lname); 
     myCustomerInfo.setAddress(luserAddress); 

     cout << lname + ", nice to meet you. Here are the items in my shop followed by the price, please enter the number that corresponds to the item you want. \n " << endl; 



     cout << "Products \n"; 
     cout << "1 - Chocolate candy bar - $3.00" << endl; 
     cout << "2 - Sour hard candy - $2.50" << endl; 
     cout << "3 - Mints - $1.25" << endl; 
     cout << "4 - Exit" << endl << endl; 
     cout << "Enter selection "; 
     // Reading User Selection 
     cin >> selection; 
     switch (selection) 
     { 
     case '1': 
      cout << "You've chosen a Chocolate candy bar. How many would you like? "; 
      cin >> itemQuantity; 
      cout << "Ok, will this finalize your order? Type and enter either 'Yes' or 'No' " << endl; 
      cin >> orderFinalized; 
      if (orderFinalized == "Yes" || orderFinalized == "yes" || orderFinalized == "YES") { 
       cout << myCustomerInfo.getName() + " your items will be shipped to " << myCustomerInfo.getAddress() << endl; 
       cout << "Printing your receipt now..." << endl; 
       finalPrice1(itemQuantity); 
      } 
      break; 
     case '2': 
      cout << "You've chosen Sour hard candy. How many would you like? "; 
      cin >> itemQuantity; 
      cout << "Ok, will this finalize your order? Type and enter either 'Yes' or 'No' " << endl; 
      cin >> orderFinalized; 
      if (orderFinalized == "Yes" || orderFinalized == "yes" || orderFinalized == "YES") { 
       cout << myCustomerInfo.getName() + " your items will be shipped to " << myCustomerInfo.getAddress() << endl; 
       cout << "Printing your receipt now..." << endl; 
       finalPrice2(itemQuantity); 
      } 
      break; 
     case '3': 
      cout << "You've chosen Mints. How many would you like? "; 
      cin >> itemQuantity; 
      cout << "Ok, will this finalize your order? Type and enter either 'Yes' or 'No' " << endl; 
      cin >> orderFinalized; 
      if (orderFinalized == "Yes" || "yes" || "YES") { 
       cout << myCustomerInfo.getName() + " your items will be shipped to " << myCustomerInfo.getAddress() << endl; 
       cout << "Printing your receipt now..." << endl; 
       finalPrice3(itemQuantity); 

      } 
      break; 
     case '4': 
      cout << "Thank you for using my shop. <exiting now...>" << endl; 
      break; 

     default: cout << "Invalid selection. Please try again"; 
     } 
     cout << endl << endl; 
    } while (selection != '4'); 
    return 0; 
} 
+0

使用'的std :: VECTOR'代表訂單中每個項目的對象。 – Barmar

回答

0

我還沒有運行代碼,但這裏是給你使用最簡單的概念的想法。 您需要更正代碼:

void finalPrice(int itemQuantity[]) { 
float price 

for(int i=0;i<3;i++) 
{ 
price = itemQuantity[]*3.00 + itemQuantity[]*2.5 _+ itemQuantity[]*1.25 

} 


cout << "Your total is $" << price << endl; 
cout << "Thank you for using my shop" << endl; 
exit(0); 
} 
//***** End of functions that calculate price of multiple items ***** 

int main(void) 
{ 
char selection = ' '; 
string lname = ""; 
string luserAddress; 
int totalItemQuantity[3]; 
int itemQuantity; 
string orderFinalized; 
cout << "Hello, welcome to my online shop! What is your name? " << endl; 
    cin >> lname; 
    cout << " And what is your shipping address? " << endl; 
    cin >> luserAddress; 


    cout << lname + ", nice to meet you" << endl; 


do 
{ // Displaying menu 

    cout << "Products \n"; 
    cout << "1 - Chocolate candy bar - $3.00" << endl; 
    cout << "2 - Sour hard candy - $2.50" << endl; 
    cout << "3 - Mints - $1.25" << endl; 
    cout << "4 - Exit" << endl << endl; 
    cout << "Enter selection "; 
    // Reading User Selection 
    cin >> selection; 
    switch (selection) 
    { 
    case '1': 
     cout << "You've chosen a Chocolate candy bar. How many would you like? "; 
     cin >> itemQuantity; 
     cout << "Ok, will this finalize your order? Type and enter either 'Yes' or 'No' " << endl; 
     cin >> orderFinalized; 
     if (orderFinalized == "Yes" || orderFinalized == "yes" || orderFinalized == "YES") { 

      cout << lname + " your items will be shipped to " << luserAddress << endl; 
      cout << "Printing your receipt now..." << endl; 
      totalItemQuantity[0]+=itemQuantity; 

      //finalPrice1(itemQuantity,3.00); 
     } 
     break; 
    case '2': 
     cout << "You've chosen Sour hard candy. How many would you like? "; 
     cin >> itemQuantity; 
     cout << "Ok, will this finalize your order? Type and enter either 'Yes' or 'No' " << endl; 
     cin >> orderFinalized; 
     if (orderFinalized == "Yes" || orderFinalized == "yes" || orderFinalized == "YES") { 
      cout << lname + " your items will be shipped to " << luserAddress << endl; 
      cout << "Printing your receipt now..." << endl; 
      totalItemQuantity[1]+=itemQuantity; 
      //finalPrice2(itemQuantity); 
     } 
     break; 
    case '3': 
     cout << "You've chosen Mints. How many would you like? "; 
     cin >> itemQuantity; 
     cout << "Ok, will this finalize your order? Type and enter either 'Yes' or 'No' " << endl; 
     cin >> orderFinalized; 
     if (orderFinalized == "Yes" || "yes" || "YES") { 
      cout << lname + " your items will be shipped to " << luserAddress << endl; 
      cout << "Printing your receipt now..." << endl; 
      totalItemQuantity[2]+=itemQuantity; 
      //finalPrice3(itemQuantity); 

     } 
     break; 
    case '4': 
     cout << "Thank you for using my shop. <exiting now...>" << endl; 
     break; 

    default: cout << "Invalid selection. Please try again"; 
    } 
    cout << endl << endl; 
} while (selection != '4'); 
finalPrice(totalItemQuantity); 
return 0; 

}

0

你需要動態數組。例如:

cin >> itemQuantity; 
// create a array during runtime 
// and the size is itemQuantity 
// you can access the ith array element by items[i] 
Item *items= new Item[itemQuantity]; 

或者你可以使用vector

vector<Item> items;//you can also access the ith element by items[i] 
items.push_back(hard_candy);//items = {hard_candy} 
items.push_back(soft_candy);//items = {hard_candy, soft_candy} 
items.pop_back();//items = {hard_candy} 

順便說一句,在你的代碼的情況下3有一些錯誤:

orderFinalized == "Yes" || "yes" || "YES"//wrong 
orderFinalized == "Yes" || orderFinalized == "yes" || orderFinalized == "YES"//right