Hi, experts:

I am very to c++, and here is a program that should:

• create a Customer object– (although you will only be using one constructor here, be sure and test all three)
• Display the Customer’s info using the ‘get’ functions
• Ask the user how many months will be simulated
• For each of N months (the number of months the user desired):
Ask the user to provide all purchase amounts for the customer
Ask the user to provide a payment for the customer
Output the current balance for the customer
Display the Customer’s info using the display function

My question to my code is, why my out put for checking whether the customer is VIP or not is always incorrect?

To be called a VIP, the balance must be less than 0, which means the customer had prepaid some money on its account.

On the attachment, it is the original requirement for this project. I am begging for fixing this problem.

This is my Customer.h

#include<iostream>
using namespace std;

class Customer{

private:
	int accountNo;
	bool VIP;
	double creditLimit;
	double balance;

public:
	Customer();
	Customer(int num, bool vp = false, double creditLine = 500.00);
	Customer(int id, double initBal, bool vp = true, double creditLine = 500.00);

	//sets
	void setAcct(int aNo);
	void setCredit(double amount);
	void setVIP(bool status);
	void setBalance(double bal);
	
	//gets
	int getAcctNo() const;
	double getCreditLimit() const;
	bool getVIP() const;

	double getBal() const;
	double availCredit() const;

	void moreCredit(double amount);
	bool makePurchase(double amount);
	void display(ostream& out) const;
	void makePayment(double amount);
};

And this is my Customer.cpp

#include "Customer.h"
using namespace std;

Customer::Customer(){
}

//
Customer::Customer(int num, bool vp, double creditLine){
	setAcct(num);
	setVIP(vp);
	setCredit(creditLine);
}

//
Customer::Customer(int id, double initBal, bool vp, double creditLine){
	balance = initBal;
	setAcct(id);
	setVIP(vp);
	setCredit(creditLine);
}

void Customer::setAcct(int aNo){
	accountNo = (aNo >= -1) ? aNo : -1; 
}

void Customer::setCredit(double amount){
	creditLimit = (amount == 500.00) ? amount : 0.00; 
}

void Customer::setVIP(bool status){
	if(getBal() < 0)
		VIP = true;
	else
		VIP = false;
}
	
void Customer::setBalance(double bal){
	balance = bal;
}

//
int Customer::getAcctNo() const{
	return accountNo;
}

double Customer::getCreditLimit() const{
	return creditLimit;
}

bool Customer::getVIP() const{
	return VIP;
}

double Customer::getBal() const{
	return balance;
}

double Customer::availCredit() const{
	return getCreditLimit() - getBal();
}

void Customer::moreCredit(double amount){
	creditLimit = creditLimit + amount;
}
	
bool Customer::makePurchase(double amount){
	double temp1;
	bool temp2 = false;
	if(VIP == true){
		temp1 = availCredit() + 100.00;
		if(availCredit() >= amount){
			balance = balance + amount;
			return !temp2;
		}
		else if(temp1 >= amount){
			balance = balance + amount;
			return !temp1;
			return VIP = false;
		}
		else
			return temp2;
	}
	else{
		if(availCredit() >= amount){
			balance = balance + amount;
			return !temp2;
		}
		else
			return temp2;
	}		
}

void Customer::display(ostream& out) const{
	//
	out << "The current account number is " << accountNo << endl;
	out << "The current balance in this account is " << balance << endl;
	out << "The current account credit limit is " << creditLimit << endl;
	if(VIP == true)
		out << "You are a VIP member";
	else
		out << "You are not a VIP member";
}

void Customer::makePayment(double amount){
	double temp;
	temp = balance - amount;
	if(getBal() < 0)
		VIP = true;
	else
		VIP = false;
}

And this is my CustomerApp.cpp

#include "Customer.h"
#include <iostream>
#include <cctype>
#include <string>
#include <cstdlib>
using namespace std;


int getSelection(string message);

int main(){
	//local varaibles
	int aAnswer, payment;
	double bal = 0.00;
	double spendings, initialBal;
	cout << "Project 4" << endl << endl
		 << "Please enter an account number: ";
		cin >> aAnswer;
	Customer newCustomer(aAnswer, bal);
	int numberOfMonth, monthlyNumberOfSpendings;
	char ans;
	bool temp = true;
	initialBal = newCustomer.getBal();
	
	do{
		//six options
		cout << "\n What would you like to do ?" << endl
			 << "\n 1) Get account number"
			 << "\n 2) Get balance"
			 << "\n 3) Get credit limit"
			 << "\n 4) Check VIP status"
			 << "\n 5) Running a simulation"
			 << "\n 6) Overall information"
			 << "\n 7) Exit";
		//switch statment
			switch(getSelection("\n Selection(1 to 8): ")){
				//1) get account number	
				 case 1:
						cout << endl << "The account number is " << newCustomer.getAcctNo();
					 break;
				//2) get balance
				 case 2:
						cout << endl << "The current balance in this account is " << newCustomer.getBal();
					 break;
				//3) get credit limit
				 case 3:
						cout << endl << "The current account credit limit is " << newCustomer.getCreditLimit();
					 break;
				//4) check vip
				 case 4:
					 if(newCustomer.getVIP() == true)
						cout << endl << "The current customner is a VIP member"; 
					 else
						cout << endl << "The current customner is not a VIP member"; 
					 break;
				//5) simulations
				 case 5:
					cout << endl << "Please enter number of the month that wants to be simulated: ";
					cin >> numberOfMonth;

					for (int i = 0; i < numberOfMonth; i++){
						cout << endl << "Please enter number of purchases: ";
						cin >> monthlyNumberOfSpendings;
						for(int j = 0; j < monthlyNumberOfSpendings; j++){
							cout << endl << "Please enter the amounts: ";
							cin >> spendings;
							if (newCustomer.makePurchase(spendings * 1.00) == true){
								newCustomer.setBalance(initialBal);
								initialBal = initialBal + spendings * 1.00;
							}
							else
								cout << "You have reached your credit line, and you current" 
									 << "balance is " << newCustomer.getBal() << "Are you going " 
									 << "to pay your current balance? ";
							cin >> ans;
							if((ans == 'Y')||(ans == 'y')){
								cout << endl << "Please enter the amount that you want to pay: ";
								cin >> payment;
								initialBal = initialBal - payment * 1.00;
								if(newCustomer.getBal() < 0)
									newCustomer.setVIP(true);
							}
						}
					}
					newCustomer.setBalance(initialBal);

					if(temp == false){

						cout << endl << "The cerrent balance is " << newCustomer.getBal() << endl
								 << "Are you going to pay your current balance? ";
							cin >> ans;
							if((ans == 'Y')||(ans == 'y')){
								cout << endl << "Please enter the amount that you want to pay: ";
								cin >> payment;
								initialBal = initialBal - payment * 1.00;
								if(newCustomer.getBal() < 0)
									newCustomer.setVIP(true);
							}
					}
					break;
				//6) 
				 case 6:
					 cout << endl;
					newCustomer.display(cout);
					break;
				//7) 
				 case 7:
					return 0;
		}
		cout << endl;
		cin.ignore(100, '\n');
	}while(true);

}

int getSelection(string message) {
    string input;
    char* pointed;
    int selectedOption;
    do {
        cout << message;
       	getline(cin, input);
        selectedOption = (int)(strtol(input.c_str(),&pointed,10));
        if (*pointed != 0 || selectedOption < 1 || selectedOption > 7) {
        	cout << "Invalid selection(should be 1 to 7): '" << input << "'. Please try again." << endl;
        }
    }while (*pointed != 0 || selectedOption < 1 || selectedOption > 7);
    return selectedOption;
}

I would guess the error comes from line 77-78 which reads:

return !temp1;
			return VIP = false;

which will, of course, never set VIP to false since the function returns before that line.

Also, you should update the setBal() function such that it also checks for a negative balance and sets the VIP value accordingly. It is very important in OOP design to make sure that the internal state of your object is always consistent (what we call the "class invariants"), that means, everywhere where the balance could change, the VIP flag should be updated too (that's why it's important to have only one set-function for the balance, and update the flag within it).


BTW, your poll question is flawed, you are missing the correct C/C++ answer which is a compilation error: "an rvalue cannot appear on the left-hand side of an assignment operation".

Thanks a lot, even though you lost me right after

Also, you should update the setBal() function such that it also checks for a negative balance and sets the VIP value accordingly.

I am so grateful that this is done. lol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.