首先,我非常喜欢编程。当我运行代码并通过我的案例1切换时,我的问题出在我的main.cpp文件中。我希望我的程序在完成购物后打印出客户的姓名和地址。 " myCustomerInfo.getName()"和" myCustomerInfo.getAddress()"没有做任何事情。我的导师整个星期一直都是MIA,没有任何帮助。我做错了什么?
//这是我的班级
#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 :(得分: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
}
答案 1 :(得分:0)
orderFinalized == "Yes" || "yes" || "YES"
应为orderFinalized == "Yes" || orderFinalized == "yes" || orderFinalized == "YES"