Stuck need help with Array's

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

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

#16 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6333
  • View blog
  • Posts: 21,743
  • Joined: 05-May 12

Re: Stuck need help with Array's

Posted 11 March 2018 - 08:10 PM

On line 20, you promised the compiler that you will define a Customerconstructor that takes two strings. The first link error you have is the linker, trying to collect on that promise.

A similar situation exists for the Beverage constructor.
Was This Post Helpful? 0
  • +
  • -

#17 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 12 March 2018 - 10:01 AM

Skydiver do you have any resources I can use to understand how to define the constructor I have been doing research and trying since 2 AM. I have not gotten anywhere with this. Please assist.
Was This Post Helpful? 0
  • +
  • -

#18 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6333
  • View blog
  • Posts: 21,743
  • Joined: 05-May 12

Re: Stuck need help with Array's

Posted 12 March 2018 - 11:17 AM

Wow! You really missed out on basic C++ programming if you need help defining constructors.

I hope this will refresh your memory.
Classes (I)
Was This Post Helpful? 1
  • +
  • -

#19 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 869
  • View blog
  • Posts: 3,492
  • Joined: 13-June 14

Re: Stuck need help with Array's

Posted 12 March 2018 - 11:20 AM

It's the same deal as with function prototypes and their implementations.
Was This Post Helpful? 0
  • +
  • -

#20 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 12 March 2018 - 01:37 PM

I figured it out. I was having a blank brain staring at the code got up and walked away figured it out. Now My only errors are lines 65-67:

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

Customer patName()
{
	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
		}
	}
}



What else am I missing?

Also for defining my constructors for both my classes if I use "=" says no operator "=" matches these operands. and "==" says has no affect did you mean "="?

What am I doing wrong here?
Was This Post Helpful? 0
  • +
  • -

#21 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6333
  • View blog
  • Posts: 21,743
  • Joined: 05-May 12

Re: Stuck need help with Array's

Posted 12 March 2018 - 03:02 PM

What error messages are you getting?
Was This Post Helpful? 0
  • +
  • -

#22 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7205
  • View blog
  • Posts: 15,018
  • Joined: 16-October 07

Re: Stuck need help with Array's

Posted 12 March 2018 - 03:24 PM

class Customer {
    const std::string custName, custAddress;
public:
    // if you don't have a variable declared for the std::ostream & that you're supposedly using
    // then you're wrong
    void custInfo(std::ostream &) const {
        std::cout << "Name" << custName << "\n"
                  "Address" << custAddress << "\n" << std::endl;
    }
    // ditto
    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) {
        // this literally does nothing
        // review assignment vs. comparison operators
        custName == inputCustName;
        custAddress == inputCustAddress;
    }
};



Yes, I did get your IM. However, you have a perfectly good thread going right here, so what's the point? Gathering more fodder for your code chimera?

The reason I haven't added any input since my last post is, frankly, you're either unwilling or unable to address your current issues. Rather, disappointingly, you seem to be going with a monkeys typing Shakespeare approach, just mushing code together randomly hoping it will somehow make sense.

The advise here, repeated by several people: learn the basics. Don't just keep rearranging snippets gleaned from the internet hoping all the pieces will magically come together and somehow work.

Break down the problem. Understand the code you're using. Do something smaller with the moving parts you're attempting to juggle until you actually understand the point of those parts.

Good luck.
Was This Post Helpful? 1
  • +
  • -

#23 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 12 March 2018 - 06:56 PM

I appreciate the feedback, however, saying I am unwilling is not okay. I have been trying to understand this. My class has thrown me into the large complex code. I have been trying which is why I ask for resources to learn to guide me.

Skydiver the error I am encountering is:

Quote

binary '=' : no operator defined which takes a left-hand operand of type 'type' (or there is no acceptable conversion)


Why am I getting this error?
Was This Post Helpful? 0
  • +
  • -

#24 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 869
  • View blog
  • Posts: 3,492
  • Joined: 13-June 14

Re: Stuck need help with Array's

Posted 12 March 2018 - 10:56 PM

I can't see the error you're referring to, but look at your constructors (lines 24-28 and 45-49): you're doing two checks for equality (==) in them and don't do anything with the results. Did you really mean to do assignments (=) instead?

This post has been edited by ndc85430: 12 March 2018 - 10:56 PM

Was This Post Helpful? 0
  • +
  • -

#25 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6333
  • View blog
  • Posts: 21,743
  • Joined: 05-May 12

Re: Stuck need help with Array's

Posted 13 March 2018 - 12:41 AM

Please post the exact errors and warnings you are getting. Don't paraphrase or edit. Errors/warnings include line numbers in them.

For example, you say that you get errors on lines 65-67 in post #20, and your post #23 indicates that it regarding assignment of incompatible types, but there is o assignment happening on those lines. Are you sure you are compiling the same code that you are editing and sharing with us?
Was This Post Helpful? 0
  • +
  • -

#26 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7205
  • View blog
  • Posts: 15,018
  • Joined: 16-October 07

Re: Stuck need help with Array's

Posted 13 March 2018 - 02:31 AM

View Postjosephhoffman28, on 12 March 2018 - 08:56 PM, said:

I have been trying to understand this.

Excellent.

View Postjosephhoffman28, on 12 March 2018 - 08:56 PM, said:

My class has thrown me into the large complex code.

Indeed. Presumably expecting you already have the foundation to deal with it. It's clear, I'm afraid, that you don't.

View Postjosephhoffman28, on 12 March 2018 - 08:56 PM, said:

I have been trying which is why I ask for resources to learn to guide me.

Good. Did you read the link offered for Classes (I)? Have you read and understood all of whatever C++ book you're referencing, at least up to classes and inheritance? If you don't have a book, have you made arrangements to get one? Perhaps worked through LearnCpp.com at least up to chapter 9 or something similar?

If you don't have a foundation nothing offered to you will be a suitable guide for anything. Seeds on stones, as it were. Build your foundation so that guidance has somewhere to take root. This is your first step to understanding.
Was This Post Helpful? 2
  • +
  • -

#27 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:43 AM

Here is the exact errors.

Quote

Severity Code Description Project File Line Suppression State Detail Description
Error (active) E0349 no operator "=" matches these operands CSS115_IP4_JosephHoffman c:\Users\jch5625\source\repos\CSS115_IP4_JosephHoffman\CSS115_IP4_JosephHoffman.cpp 47
Error (active) E0349 no operator "=" matches these operands CSS115_IP4_JosephHoffman c:\Users\jch5625\source\repos\CSS115_IP4_JosephHoffman\CSS115_IP4_JosephHoffman.cpp 26 operand types are: const std::string = const std::string
Error (active) E0349 no operator "=" matches these operands CSS115_IP4_JosephHoffman c:\Users\jch5625\source\repos\CSS115_IP4_JosephHoffman\CSS115_IP4_JosephHoffman.cpp 27
Error C2678 binary '=': no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion) CSS115_IP4_JosephHoffman c:\users\jch5625\source\repos\css115_ip4_josephhoffman\css115_ip4_josephhoffman.cpp 26
Error C2678 binary '=': no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion) CSS115_IP4_JosephHoffman c:\users\jch5625\source\repos\css115_ip4_josephhoffman\css115_ip4_josephhoffman.cpp 27
Error C2678 binary '=': no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion) CSS115_IP4_JosephHoffman c:\users\jch5625\source\repos\css115_ip4_josephhoffman\css115_ip4_josephhoffman.cpp 47


code is here:
#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);

Customer patName()
{
	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
		}
	}
}



Was This Post Helpful? 0
  • +
  • -

#28 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 - 11:24 AM

I believe I figured out the code:

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

Customer patName()
{
	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
		}
	}
}




Now the only thing I see wrong is lines 63-65 have green lines under getStringFromUser, getCharFromUser, getCustomerFromUser. What do I need to do I am assuming create functions but I am not positive and need some direction.
Was This Post Helpful? 0
  • +
  • -

#29 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6333
  • View blog
  • Posts: 21,743
  • Joined: 05-May 12

Re: Stuck need help with Array's

Posted 13 March 2018 - 11:47 AM

The error is on lines 26-27 and line 47, not lines 65-67 like your originally described. The problem is that you are trying to assign a constant reference to a string to a constant string. Recall that C++ is a backlash against C's lack of type safety and goes to the other extreme.

Since you are coming up to you deadline, the easiest way forward is for you to take away the const on lines 8 and 32. For the other people following along this thread, I know that we are giving up another great learning opportunity here by going for this simple fix, but I currently have doubts about our OP being prepared to learn about references vs. non-references and type matching.
Was This Post Helpful? 0
  • +
  • -

#30 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 - 01:16 PM

Skydiver, I took away the const on both lines and I am still getting the errors on lines 63-65 underlined in green section of code:

std::string getStringFromUser(const std::string &prompt);
char getCharFromUser(const std::string &prompt);
Customer getCustomerFromUser();



Please let me know what is going on with this?
Was This Post Helpful? 0
  • +
  • -

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