[英]Having trouble with my set/get methods for my class in c++
First off, I'm very amateur with programming. 首先,我对编程非常业余。 My issue is in my main.cpp file when I run the code and go through my case 1 switch.
当我运行代码并通过案例1开关时,我的问题在main.cpp文件中。 I want my program to print out the customer's name and address when they are finished shopping.
我希望我的程序在完成购物后打印出客户的姓名和地址。 "myCustomerInfo.getName()" and "myCustomerInfo.getAddress()" aren't doing anything.
“ myCustomerInfo.getName()”和“ myCustomerInfo.getAddress()”没有做任何事情。 My instructor has been MIA all week and is of no help.
我的教练整周都在MIA工作,没有帮助。 What am I doing wrong?
我究竟做错了什么?
//This is my class //这是我的课
#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;
}
//End of class //课程结束
//This is my main.cpp //这是我的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;
}
Take a closer look at your code: 仔细看看您的代码:
string lname = "";
string luserAddress;
int itemQuantity;
string orderFinalized;
CustomerInfo myCustomerInfo;
myCustomerInfo.setName(lname);
Because at this point that you set the name it is ""
. 因为此时您设置的名称为
""
。 You need to set it after you update the lname
. 您需要在更新
lname
之后进行设置。
Few other things. 别的东西。 Changes this:
更改此:
if (orderFinalized == "Yes" || "yes" || "YES")
to this: 对此:
if (orderFinalized == "Yes" || orderFinalized == "yes" || orderFinalized == "YES")
You have it in multiple places. 您在多个地方都有它。 Same goes for the "No"s.
“否”也一样。
[EDIT]: A more elegant way as suggested by @user4581301 is to transform the string to the lower case and make only one comparison: [编辑]:@ user4581301建议的一种更优雅的方法是将字符串转换为小写并仅进行一个比较:
std::transform(orderFinalized.begin(), orderFinalized.end(), orderFinalized.begin(), ::tolower);
if (orderFinalized == "yes"){
// the rest of the code
}
orderFinalized == "Yes" || "yes" || "YES"
orderFinalized == "Yes" || "yes" || "YES"
should be orderFinalized == "Yes" || orderFinalized == "yes" || orderFinalized == "YES"
orderFinalized == "Yes" || "yes" || "YES"
应该被orderFinalized == "Yes" || orderFinalized == "yes" || orderFinalized == "YES"
orderFinalized == "Yes" || orderFinalized == "yes" || orderFinalized == "YES"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.