ghost
ghost

Reputation: 39

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. 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. My instructor has been MIA all week and is of no help. 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

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

Upvotes: 0

Views: 58

Answers (2)

vim_
vim_

Reputation: 2170

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.

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:

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

Upvotes: 1

Jiahao Cai
Jiahao Cai

Reputation: 1250

orderFinalized == "Yes" || "yes" || "YES"should be orderFinalized == "Yes" || orderFinalized == "yes" || orderFinalized == "YES"

Upvotes: 0

Related Questions