Stuck need help with Array's

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4

46 Replies - 1449 Views - Last Post: 15 March 2018 - 02:56 PM Rate Topic: -----

#31 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6328
  • View blog
  • Posts: 21,730
  • Joined: 05-May 12

Re: Stuck need help with Array's

Posted 13 March 2018 - 02:18 PM

I don't use Code::Blocks. What do the green underline mean? What error messages or warnings do you get?
Was This Post Helpful? 0
  • +
  • -

#32 josephhoffman28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-February 18

Re: Stuck need help with Array's

Posted 13 March 2018 - 02:49 PM

My errors are:

Quote

Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "char __cdecl getCharFromUser(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" ([email protected]@YADABV?$[email protected]?$[email protected]@[email protected]@V?$[email protected]@[email protected]@[email protected]@@Z) referenced in function _main CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\CSS115_IP4_JosephHoffman.obj 1
Error LNK2019 unresolved external symbol "class Customer __cdecl getCustomerFromUser(void)" ([email protected]@[email protected]@XZ) referenced in function _main CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\CSS115_IP4_JosephHoffman.obj 1
Error LNK2019 unresolved external symbol "void __cdecl displayMenu(class Customer const &)" ([email protected]@[email protected]@@Z) referenced in function _main CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\CSS115_IP4_JosephHoffman.obj 1
Error LNK2019 unresolved external symbol "void __cdecl loadProductQuantity(struct Beverage const &,int * const)" ([email protected]@[email protected]@[email protected]) referenced in function _main CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\CSS115_IP4_JosephHoffman.obj 1
Error LNK2019 unresolved external symbol "void __cdecl doneShowOrder(class Customer const &,int * const)" ([email protected]@[email protected]@[email protected]) referenced in function _main CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\CSS115_IP4_JosephHoffman.obj 1
Error LNK1120 5 unresolved externals CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\Debug\CSS115_IP4_JosephHoffman.exe 1


These are what my compiler shows.
Was This Post Helpful? 0
  • +
  • -

#33 jimblumberg   User is online

  • member icon

Reputation: 5535
  • View blog
  • Posts: 17,143
  • Joined: 25-December 09

Re: Stuck need help with Array's

Posted 13 March 2018 - 02:51 PM

Quote

These are what my compiler shows.

No, those are the errors your linker if finding.

Your linker is telling you it can't find the implementations of those functions.


Jim
Was This Post Helpful? 0
  • +
  • -

#34 josephhoffman28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-February 18

Re: Stuck need help with Array's

Posted 13 March 2018 - 03:06 PM

So I need to create 3 functions correct?
Was This Post Helpful? 0
  • +
  • -

#35 jimblumberg   User is online

  • member icon

Reputation: 5535
  • View blog
  • Posts: 17,143
  • Joined: 25-December 09

Re: Stuck need help with Array's

Posted 13 March 2018 - 03:13 PM

Why don't you try it and see?


Jim
Was This Post Helpful? 0
  • +
  • -

#36 josephhoffman28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-February 18

Re: Stuck need help with Array's

Posted 13 March 2018 - 06:21 PM

So I was able to create two out of three of the functions. I got help with this so I am unsure what to do for the last one and if I even did this properly.

#include <iostream>
#include <string>
#include <iomanip>
#include <sstream> // stringstream
#include <cctype> // isalph, isdigit

class Customer {
	const std::string custName, custAddress;
public:
	void custInfo(std::ostream &) const
	{
		std::cout << "Name" << custName << "\n"
			"Address" << custAddress << "\n" << std::endl;
	}
	void custWelcome(std::ostream &) const
	{
		std::cout << "\nHello " << custName << "\nAddress: " << custAddress << std::endl;
	}
	std::string custValue() const
	{
		return custName;
	}

	Customer(const std::string &inputCustName, const std::string &inputCustAddress)
	{
		custName == inputCustName;
		custAddress == inputCustAddress;
	}
};

struct Beverage {
	const std::string name;
	double price;

	void bevInfo(std::ostream &) const
	{
		std::cout << "Name" << name << "\n"
			<< "Price" << "$" << price << "\n"
			<< "Total" << "$" << std::endl;
	}
	void bevSelect(std::ostream &) const
	{
		std::cout << "Selection: " << name << ", price per unit: $" << price << std::endl;
	}
	Beverage(const std::string &bevName, double bevPrice)
	{
		name == bevName;
		price == bevPrice;
	}
};


const int AVAILABLE_PRODUCTS_COUNT = 3;
const Beverage AVAILABLE_PRODUCTS[AVAILABLE_PRODUCTS_COUNT] = {
	Beverage("Water", 1.45),
	Beverage("Soda", 2.98),
	Beverage("Ice Tea", 3.29)
};

typedef int Quantities[AVAILABLE_PRODUCTS_COUNT];


double totalCost(Quantities);
void orderSummary(Quantities);
std::string getStringFromUser(const std::string &prompt);
char getCharFromUser(const std::string &prompt);
Customer getCustomerFromUser();
void displayMenu(const Customer &);
void loadProductQuantity(const Beverage &, Quantities);
void doneShowOrder(const Customer &, Quantities);



void getStringFromUser(std::string &prompt) //Not sure what should go in this function
{

	
}

void getCharFromUser(std::string &prompt)
{
	std::cout << std::endl << std::endl
		<< &prompt
		<< ", Please select the beverage you would like to purchase from the menu: "
		<< std::endl;

	std::cout << "Drink Menu" << std::endl;
	std::cout << "========" << std::endl;
	std::cout << "1 - Water $1.45" << std::endl;
	std::cout << "2 - Soda $2.98" << std::endl;
	std::cout << "3 - Iced Tea $3.29" << std::endl;
	std::cout << "X - Exit " << std::endl << std::endl;
}


Customer getCustomerFromUser()
{
	std::string custName;
	std::string custAddress;
	std::cout << "Please enter your name ==> ";
	std::getline(std::cin, custName);
	std::cout << "Please enter your address ==> ";
	std::getline(std::cin, custAddress);

	return Customer(custName, custAddress);
}

int main(void) {
	Quantities ordered = { 0 };
	Customer patron = getCustomerFromUser();
	patron.custInfo(std::cout);

	bool done = false;
	while (!done) {
		displayMenu(patron);
		switch (getCharFromUser("Your selection: ")) {
		case '1':
			loadProductQuantity(AVAILABLE_PRODUCTS[0], ordered);
			break;
		case '2':
			loadProductQuantity(AVAILABLE_PRODUCTS[1], ordered);
			break;
		case '3':
			loadProductQuantity(AVAILABLE_PRODUCTS[2], ordered);
			break;
		case 'X': case 'x':
			done = true;
			break;
		default:
			std::cout << "Invalid selection. Please try again";
			std::cin.get();
			// no break in the default case
		}
	}
	doneShowOrder(patron, ordered);

	return 0;
}

void orderSummary(Quantities ordered) {
	std::cout << std::endl;
	std::cout << "======= ORDER SUMMARY ====" << std::endl;
	std::cout << "Items selected" << std::endl;
	for (int i = 0; i < AVAILABLE_PRODUCTS_COUNT; i++) {
		// only show beverages that has at least one order
		if (ordered[i] > 0) {
			// print something for AVAILABLE_PRODUCTS[i] here
		}
	}
}



This is the only part I am completely lost on:
void getStringFromUser(std::string &prompt) //Not sure what should go in this function
{

	
}



Please advise me I am trying to understand here.
Was This Post Helpful? 0
  • +
  • -

#37 josephhoffman28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-February 18

Re: Stuck need help with Array's

Posted 13 March 2018 - 06:46 PM

Quote

Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "void __cdecl displayMenu(class Customer const &)" ([email protected]@[email protected]@@Z) referenced in function _main CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\CSS115_IP4_JosephHoffman.obj 1
Error LNK2019 unresolved external symbol "char __cdecl getCharFromUser(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" ([email protected]@YADABV?$[email protected]?$[email protected]@[email protected]@V?$[email protected]@[email protected]@[email protected]@@Z) referenced in function _main CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\CSS115_IP4_JosephHoffman.obj 1
Error LNK2019 unresolved external symbol "void __cdecl loadProductQuantity(struct Beverage const &,int * const)" ([email protected]@[email protected]@[email protected]) referenced in function _main CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\CSS115_IP4_JosephHoffman.obj 1
Error LNK2019 unresolved external symbol "void __cdecl doneShowOrder(class Customer const &,int * const)" ([email protected]@[email protected]@[email protected]) referenced in function _main CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\CSS115_IP4_JosephHoffman.obj 1
Error LNK1120 4 unresolved externals CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\Debug\CSS115_IP4_JosephHoffman.exe 1


These are the errors given by the compiler.

This post has been edited by josephhoffman28: 13 March 2018 - 06:50 PM

Was This Post Helpful? 0
  • +
  • -

#38 josephhoffman28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-February 18

Re: Stuck need help with Array's

Posted 13 March 2018 - 07:32 PM

I apologize for the code I put up was not the most recent. Here it is:

#include <iostream>
#include <string>
#include <iomanip>
#include <sstream> // stringstream
#include <cctype> // isalph, isdigit

class Customer {
	const std::string custName, custAddress;
public:
	void custInfo(std::ostream &) const
	{
		std::cout << "Name" << custName << "\n"
			"Address" << custAddress << "\n" << std::endl;
	}
	void custWelcome(std::ostream &) const
	{
		std::cout << "\nHello " << custName << "\nAddress: " << custAddress << std::endl;
	}
	std::string custValue() const
	{
			return custName;
	}

	Customer(const std::string &inputCustName, const std::string &inputCustAddress)
		: custName(inputCustName), custAddress(inputCustAddress)
	{
	}
};

struct Beverage {
	const std::string name;
	double price;

	void bevInfo(std::ostream &) const
	{
		std::cout << "Name" << name << "\n"
			<< "Price" << "$" << price << "\n"
			<< "Total" << "$" << std::endl;
	}
	void bevSelect(std::ostream &) const
	{
		std::cout << "Selection: " << name << ", price per unit: $" << price << std::endl;
	}
	Beverage(const std::string &bevName, double bevPrice)
		: name(bevName), price(bevPrice)
	{
	}
};



See error in the last post. Need help almost out of time.
Was This Post Helpful? 0
  • +
  • -

#39 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6328
  • View blog
  • Posts: 21,730
  • Joined: 05-May 12

Re: Stuck need help with Array's

Posted 13 March 2018 - 07:43 PM

The errors in post #37 indicate that you are missing 4 functions.
Was This Post Helpful? 0
  • +
  • -

#40 josephhoffman28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-February 18

Re: Stuck need help with Array's

Posted 13 March 2018 - 07:57 PM

View PostSkydiver, on 13 March 2018 - 07:43 PM, said:

The errors in post #37 indicate that you are missing 4 functions.



Skydiver I don't understand what 4 are missing? I am really struggling on this and am about out of time but I do not want to give up. please explain to me.
Was This Post Helpful? 0
  • +
  • -

#41 jimblumberg   User is online

  • member icon

Reputation: 5535
  • View blog
  • Posts: 17,143
  • Joined: 25-December 09

Re: Stuck need help with Array's

Posted 13 March 2018 - 09:12 PM

Read your #![email protected] error messages they tell you exactly what functions the linker can't find!

Quote

Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "void __cdecl displayMenu(class Customer const &)" ([email protected]@[email protected]@@Z) referenced in function _main CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\CSS115_IP4_JosephHoffman.obj 1
Error LNK2019 unresolved external symbol "char __cdecl getCharFromUser(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" ([email protected]@YADABV?$[email protected]?$[email protected]@[email protected]@V?$[email protected]@[email protected]@[email protected]@@Z) referenced in function _main CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\CSS115_IP4_JosephHoffman.obj 1
Error LNK2019 unresolved external symbol "void __cdecl loadProductQuantity(struct Beverage const &,int * const)" ([email protected]@[email protected]@[email protected]) referenced in function _main CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\CSS115_IP4_JosephHoffman.obj 1
Error LNK2019 unresolved external symbol "void __cdecl doneShowOrder(class Customer const &,int * const)" ([email protected]@[email protected]@[email protected]) referenced in function _main CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\CSS115_IP4_JosephHoffman.obj 1
Error LNK1120 4 unresolved externals CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\Debug\CSS115_IP4_JosephHoffman.exe 1


I highlighted some of the important information.


Quote

So I was able to create two out of three of the functions. I got help with this so I am unsure what to do for the last one and if I even did this properly.


That enlarged part of the quote is why you're so lost. The bolded part is an untruth as evidenced by the large font part of the quote.

Jim

This post has been edited by jimblumberg: 13 March 2018 - 09:13 PM

Was This Post Helpful? 0
  • +
  • -

#42 josephhoffman28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-February 18

Re: Stuck need help with Array's

Posted 13 March 2018 - 09:20 PM

View Postjimblumberg, on 13 March 2018 - 09:12 PM, said:

Read your #![email protected] error messages they tell you exactly what functions the linker can't find!

Quote

Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "void __cdecl displayMenu(class Customer const &)" ([email protected]@[email protected]@@Z) referenced in function _main CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\CSS115_IP4_JosephHoffman.obj 1
Error LNK2019 unresolved external symbol "char __cdecl getCharFromUser(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" ([email protected]@YADABV?$[email protected]?$[email protected]@[email protected]@V?$[email protected]@[email protected]@[email protected]@@Z) referenced in function _main CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\CSS115_IP4_JosephHoffman.obj 1
Error LNK2019 unresolved external symbol "void __cdecl loadProductQuantity(struct Beverage const &,int * const)" ([email protected]@[email protected]@[email protected]) referenced in function _main CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\CSS115_IP4_JosephHoffman.obj 1
Error LNK2019 unresolved external symbol "void __cdecl doneShowOrder(class Customer const &,int * const)" ([email protected]@[email protected]@[email protected]) referenced in function _main CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\CSS115_IP4_JosephHoffman.obj 1
Error LNK1120 4 unresolved externals CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\Debug\CSS115_IP4_JosephHoffman.exe 1


I highlighted some of the important information.


Quote

So I was able to create two out of three of the functions. I got help with this so I am unsure what to do for the last one and if I even did this properly.


That enlarged part of the quote is why you're so lost. The bolded part is an untruth as evidenced by the large font part of the quote.

Jim


I am having trouble with the functions code follows:
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream> // stringstream
#include <cctype> // isalph, isdigit

class Customer {
	const std::string custName, custAddress;
public:
	void custInfo(std::ostream &) const
	{
		std::cout << "Name" << custName << "\n"
			"Address" << custAddress << "\n" << std::endl;
	}
	void custWelcome(std::ostream &) const
	{
		std::cout << "\nHello " << custName << "\nAddress: " << custAddress << std::endl;
	}
	std::string custValue() const
	{
		return custName;
	}

	Customer(const std::string &inputCustName, const std::string &inputCustAddress)
		: custName(inputCustName), custAddress(inputCustAddress)
	{
	}
};

struct Beverage {
	const std::string name;
	double price;

	void bevInfo(std::ostream &) const
	{
		std::cout << "Name" << name << "\n"
			<< "Price" << "$" << price << "\n"
			<< "Total" << "$" << std::endl;
	}
	void bevSelect(std::ostream &) const
	{
		std::cout << "Selection: " << name << ", price per unit: $" << price << std::endl;
	}
	Beverage(const std::string &bevName, double bevPrice)
		: name(bevName), price(bevPrice)
	{
	}
};


const int AVAILABLE_PRODUCTS_COUNT = 3;
const Beverage AVAILABLE_PRODUCTS[AVAILABLE_PRODUCTS_COUNT] = {
	Beverage("Water", 1.45),
	Beverage("Soda", 2.98),
	Beverage("Ice Tea", 3.29)
};

typedef int Quantities[AVAILABLE_PRODUCTS_COUNT];


double totalCost(Quantities);
void orderSummary(Quantities);
std::string getStringFromUser(const std::string &prompt);
char getCharFromUser(const std::string &prompt);
Customer getCustomerFromUser();
void displayMenu(const Customer &);
void loadProductQuantity(const Beverage &, Quantities);
void doneShowOrder(const Customer &, Quantities);

double totalCost(Quantities)//NOT SURE IF IT IS CORRECT
{
	double totalCost = 0;

	for (int i = 0; i < AVAILABLE_PRODUCTS_COUNT; i++) {
		totalCost += (AVAILABLE_PRODUCTS_COUNT*AVAILABLE_PRODUCTS_COUNT);
	}
	return totalCost;

}

void loadProductQuantity(const Beverage &, Quantities)//NEED HELP FIGURING OUT WHAT TO DO
{

}

void doneShowOrder(const Customer &, Quantities)//NEED HELP FIGURING OUT WHAT TO DO
{

}

void displayMenu(const Customer &prompt)//NOT SURE IF IT IS CORRECT
{
	std::cout << std::endl << std::endl
		<< &prompt
		<< ", Please select the beverage you would like to purchase from the menu: "
		<< std::endl;

	std::cout << "Drink Menu" << std::endl;
	std::cout << "========" << std::endl;
	std::cout << "1 - Water $1.45" << std::endl;
	std::cout << "2 - Soda $2.98" << std::endl;
	std::cout << "3 - Iced Tea $3.29" << std::endl;
	std::cout << "X - Exit " << std::endl << std::endl;
}

void getStringFromUser(std::string &prompt)//NEED HELP FIGURING OUT WHAT TO DO
{
	
}

char getCharFromUser(std::string &prompt)//NEED HELP FIGURING OUT WHAT TO DO
{
	
}


Customer getCustomerFromUser()//NOT SURE IF IT IS CORRECT
{
	std::string custName;
	std::string custAddress;
	std::cout << "Please enter your name ==> ";
	std::getline(std::cin, custName);
	std::cout << "Please enter your address ==> ";
	std::getline(std::cin, custAddress);

	return Customer(custName, custAddress);
}

int main(void) {
	Quantities ordered = { 0 };
	Customer patron = getCustomerFromUser();
	patron.custInfo(std::cout);

	bool done = false;
	while (!done) {
		displayMenu(patron);
		switch (getCharFromUser("Your selection: ")) {
		case '1':
			loadProductQuantity(AVAILABLE_PRODUCTS[0], ordered);
			break;
		case '2':
			loadProductQuantity(AVAILABLE_PRODUCTS[1], ordered);
			break;
		case '3':
			loadProductQuantity(AVAILABLE_PRODUCTS[2], ordered);
			break;
		case 'X': case 'x':
			done = true;
			break;
		default:
			std::cout << "Invalid selection. Please try again";
			std::cin.get();
			// no break in the default case
		}
	}
	doneShowOrder(patron, ordered);

	return 0;
}

void orderSummary(Quantities ordered) {
	std::cout << std::endl;
	std::cout << "======= ORDER SUMMARY ====" << std::endl;
	std::cout << "Items selected" << std::endl;
	for (int i = 0; i < AVAILABLE_PRODUCTS_COUNT; i++) {
		// only show beverages that has at least one order
		if (ordered[i] > 0) {
			std::cout << "You selcted: " << std::endl;// print something for AVAILABLE_PRODUCTS[i] NOT SURE IF THIS IS CORRECT
		}
	}
}



I commented everything I am stuck on I am trying to figure this all out.

This post has been edited by josephhoffman28: 13 March 2018 - 09:22 PM

Was This Post Helpful? 0
  • +
  • -

#43 jimblumberg   User is online

  • member icon

Reputation: 5535
  • View blog
  • Posts: 17,143
  • Joined: 25-December 09

Re: Stuck need help with Array's

Posted 13 March 2018 - 09:32 PM

Good luck. I suggest that you go back to that person that gave you all the code that you don't understand. Perhaps he can explain what those functions are supposed to do.

Jim
Was This Post Helpful? 0
  • +
  • -

#44 josephhoffman28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-February 18

Re: Stuck need help with Array's

Posted 13 March 2018 - 10:09 PM

View Postjimblumberg, on 13 March 2018 - 09:32 PM, said:

Good luck. I suggest that you go back to that person that gave you all the code that you don't understand. Perhaps he can explain what those functions are supposed to do.

Jim


I tried. All I wanted was help from post 1 just explanation and guidance but I just get random code thrown at me, by all means, take a look at my 1st post and help if you can with my code.
Was This Post Helpful? 0
  • +
  • -

#45 josephhoffman28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-February 18

Re: Stuck need help with Array's

Posted 15 March 2018 - 05:48 AM

So I am trying to figure out how to call line 41 to function on line 105 after you make your selection it should display Selection: name, price per unit: $price

I need to know how to do this so each time you select it to display this so, for example, hit 1 for water Selection: Water, price per unit: $1.45 then it should ask for quantity.

Also my Order summary is not working properly it displays nothing instead of all the choices and totals.

  #include <iostream>
#include <string>
#include <iomanip>
#include <sstream> // stringstream
#include <cctype> // isalph, isdigit

class Customer {
	const std::string custName, custAddress;
public:
	void custInfo(std::ostream &) const
	{
		
	}
	void custWelcome(std::ostream &) const
	{
		std::cout << "\nHello " << custName << "\nAddress: " << custAddress << std::endl;
	}
	std::string custValue() const
	{
		return custName;
	}

	Customer(const std::string &inputCustName, const std::string &inputCustAddress)
		: custName(inputCustName), custAddress(inputCustAddress)
	{
	}
};

struct Beverage {
	const std::string name;
	double price;

	void bevInfo(std::ostream &) const
	{
		std::cout << "Name" << name << "\n"
			<< "Price" << "$" << price << "\n"
			<< "Total" << "$" << std::endl;
	}
	void bevSelect(std::ostream &) const
	{
		std::cout << "Selection: " << name << ", price per unit: $" << price << std::endl;
	}
	Beverage(const std::string &bevName, double bevPrice)
		: name(bevName), price(bevPrice)
	{
	}
};


const int AVAILABLE_PRODUCTS_COUNT = 3;
const Beverage AVAILABLE_PRODUCTS[AVAILABLE_PRODUCTS_COUNT] = {
	Beverage("Water", 1.45), // Element 0
	Beverage("Soda", 2.98), // Element 1
	Beverage("Ice Tea", 3.29) // Element 2
};

typedef int Quantities[AVAILABLE_PRODUCTS_COUNT];
Quantities ordered = { 0 };

double totalCost(Quantities);
void orderSummary(Quantities);
char getCharFromUser(const std::string &prompt);
Customer getCustomerFromUser();
void displayMenu(const Customer &);
void loadProductQuantity(const Beverage &, Quantities);
void doneShowOrder(const Customer &, Quantities);

double totalCost(Quantities beverageCount)//Cost loop
{
	double totalCost = 0;

	for (int i = 0; i < AVAILABLE_PRODUCTS_COUNT; i++) {
		totalCost += (beverageCount[i] * AVAILABLE_PRODUCTS[i].price);
	}
	return totalCost;

}

void loadProductQuantity(int beverageNumber, int quantity)
{
	ordered[beverageNumber] = quantity;
}

void doneShowOrder(const Customer &, Quantities)
{
	orderSummary(ordered);
}

void displayMenu(const Customer &prompt)
{
	std::cout << std::endl << std::endl
		<< prompt.custValue()
		<< ", Please select the beverage you would like to purchase from the menu: "
		<< std::endl;

	std::cout << "Drink Menu" << std::endl;
	std::cout << "========" << std::endl;
	std::cout << "1 - Water $1.45" << std::endl;
	std::cout << "2 - Soda $2.98" << std::endl;
	std::cout << "3 - Iced Tea $3.29" << std::endl;
	std::cout << "X - Exit " << std::endl << std::endl;
}


char getCharFromUser(const std::string &prompt)
{
	char key;

	std::cout << "Selection: ";

	std::cin >> key;

	return key;
}


Customer getCustomerFromUser()
{
	std::string custName;
	std::string custAddress;
	std::cout << "Please enter your name ==> ";
	std::getline(std::cin, custName);
	std::cout << "Please enter your address ==> ";
	std::getline(std::cin, custAddress);

	return Customer(custName, custAddress);
}

int main(void) {
	Customer patron = getCustomerFromUser();
	patron.custWelcome(std::cout);
	patron.custInfo(std::cout);

	bool done = false;
	while (!done) {
		int amountOrdered = 0;
		displayMenu(patron);
		switch (getCharFromUser("Your selection: ")) {
		case '1':
			std::cout << "Please chose a quantiy: ";
			std::cin >> amountOrdered;
			loadProductQuantity(0, amountOrdered);
			break;
		case '2':
			std::cout << "Please chose a quantiy: ";
			std::cin >> amountOrdered;
			loadProductQuantity(1, amountOrdered);
			break;
		case '3':
			std::cout << "Please chose a quantiy: ";
			std::cin >> amountOrdered;
			loadProductQuantity(2, amountOrdered);
			break;
		case 'X': case 'x':
			done = true;
			break;
		default:
			std::cout << "Invalid selection. Please try again";
			std::cin.get();
			// no break in the default case
		}
	}
	doneShowOrder(patron, ordered);
	std::cout << "Press any key to exit." << std::endl;
	char blah;
	std::cin >> blah;
	return 0;
}

void orderSummary(Quantities ordered) {
	std::cout << std::endl;
	std::cout << "======= ORDER SUMMARY ====" << std::endl;
	std::cout << "Items selected" << std::endl;
	for (int i = 0; i < AVAILABLE_PRODUCTS_COUNT; i++) {
		// only show beverages that has at least one order
		if (ordered[i] > 0) {
			std::cout << "You selcted: " << std::endl;
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4