2017-03-17 49 views
0

首先,我非常擅長編程。我的問題是在我的main.cpp文件中,當我運行代碼並通過我的case 1開關。我希望我的程序在購物完成後打印出客戶的姓名和地址。 「myCustomerInfo.getName()」和「myCustomerInfo.getAddress()」沒有做任何事情。我的教練整個星期都是MIA,沒有任何幫助。我究竟做錯了什麼?在C++中我的類的設置/獲取方法遇到問題

//這是我的課

#pragma once 
#include <string> 
using namespace std; 

class CustomerInfo 
{ 
private: 
    string custName; 
    string custAddress; 
public: 

    CustomerInfo(string name, string userAddress) : custName(name), custAddress(userAddress) {} 
    void setName(string); 
    void setAddress(string); 
    string getName(); 
    string getAddress(); 
    CustomerInfo(); 
}; 

// Defining setName 
void CustomerInfo::setName(string name) { 
    custName = name; 
} 
// Defining setAddress 
void CustomerInfo::setAddress(string userAddress) { 
    custAddress = userAddress; 
} 
// Defining getName 
string CustomerInfo::getName() { 
    return custName; 
} 
// Defining getAddress 
string CustomerInfo::getAddress() {`enter code here` 
    return custAddress; 
} 

//

類的結束

//這是我的main.cpp

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

    myCustomerInfo.setName(lname); 
    myCustomerInfo.setAddress(luserAddress); 



    do 
    { // Displaying menu 
     cout << "Hello, welcome to my online shop! What is your name? " << endl; 
     cout << " And what is your shipping address? " << endl; 
     cin >> lname >> 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" || "yes" || "YES") { 
       cout << myCustomerInfo.getName() + " your items will be shipped to " << myCustomerInfo.getAddress() << endl; 
       cout << "Printing your receipt now..." << endl; 
       finalPrice1(itemQuantity); 
      } 
      else if (orderFinalized == "No" || "no" || "NO") { 

      } 

      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" || "yes" || "YES") { 
       finalPrice2(itemQuantity); 
       cout << "What's the address your items will be shipped to? " << endl; 
       cin >> luserAddress; 
       cout << "Ok, your order will be shipped to " << luserAddress << endl; 
      } 
      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") { 
       finalPrice3(itemQuantity); 
       cout << "What's the address your items will be shipped to? " << endl; 
       cin >> luserAddress; 
       cout << "Ok, your order will be shipped to " << luserAddress << endl; 
      } 
      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

你get/set方法看起來很好。 – drescherjm

+1

'orderFinalized ==「No」|| 「不」|| 「NO」不會奏效! ||操作員不會那樣工作。與'orderFinalized ==「是」||「相同「是」|| 「YES」'無論輸入如何,兩個陳述都是真實的。在StackOverflow中,我確信這個bug有很多重複。 – drescherjm

+3

編程的一個重要祕訣是一個鮮爲人知的工具[稱爲調試器](https://en.wikipedia.org/wiki/Debugger)。每個開發環境都包含了所有值得使用的開發環境,學習使用它們將大大縮短您的調試時間,使您可以將更多時間花在其他課程上,併爲更高等級生成更好的軟件。而當你畢業時,你的同事就會有一個嚴肅的邊緣。調試器爲你做的很多事情是允許你控制程序的執行,這樣你就可以停下來看看它在做什麼。 – user4581301

回答

1

取仔細看看你的代碼:

string lname = ""; 
string luserAddress; 
int itemQuantity; 
string orderFinalized; 
CustomerInfo myCustomerInfo; 

myCustomerInfo.setName(lname); 

因爲此時您設置的名稱是""。在更新lname後,您需要設置它。

很少其他的事情。這種變化:

if (orderFinalized == "Yes" || "yes" || "YES")

這樣:

if (orderFinalized == "Yes" || orderFinalized == "yes" || orderFinalized == "YES")

你擁有了它在多個地方。 「否」同樣如此。

[編輯]:通過@ user4581301提出了一個更優雅的方式是將字符串轉換爲小寫,使只有一個比較:

std::transform(orderFinalized.begin(), orderFinalized.end(), orderFinalized.begin(), ::tolower); 
if (orderFinalized == "yes"){ 
    // the rest of the code 
} 
+0

同樣值得在['std :: transform'](http://en.cppreference.com/w/cpp/algorithm/transform)中折騰和['std :: to lower'](http://en.cppreference.com/w/cpp/string/byte/tolower)。一起使用可以讓你將字符串轉換爲小寫字母,所以你需要測試的是'orderFinalized =='yes「' – user4581301

+0

好點。更新。 – Arash

+0

user4581301謝謝。 – CaseyC

0

orderFinalized == "Yes" || "yes" || "YES"應該orderFinalized == "Yes" || orderFinalized == "yes" || orderFinalized == "YES"

+0

雖然此代碼有正確的觀點,但最好包含關於它的詳細信息。解釋問題的根源,提供解決方案的更多細節。 –

+0

感謝您的建議,下次我會更具體了@VictorPolevoy – Jiahao