14 Replies - 406 Views - Last Post: 02 March 2018 - 09:29 AM Rate Topic: -----

#1 josephhoffman28   User is offline

  • New D.I.C Head

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

C++ HELP WITH ERROR USING CLASSES

Posted 28 February 2018 - 12:22 PM

Hello,

These were the requirements for my program to have implemented:

Design the customer class.
Include at least the customer name and address for the class.
Create class functions to set the class variables.
Create class functions to get the values of the class variables.
Create additional functions as necessary.
Complete the following in your code:
*Implement the new class
*Create an object from the new class
*Set the customer details using the class functions
*Use the class functions to access the customer details

I am encountering errors line 188-189 in my code:

Customer patron = snafu(); // get info, create patron
	patron.simpleInfo(); // greet patron 



This is suppose to call from the class to enter your name and then enter your address. After that is entered it should say:

Hello: NAME, Address: ADDRESS

then throughout the program it will display your name I am having trouble calling the class and displaying this I have code commented out from before I implemented my class. Here is my code below:


#include <iostream>
#include <string>
#include <iomanip>
#include <sstream> // stringstream
#include <cctype> // isalph, isdigit
using namespace std;


class Customer
{
	string custName; // Customer name
	string custAddress; //Customer Address

	void info()
	{
		cout << setw(10) << left << "Name" << custName << "\n"
			<< setw(10) << left << "Address" << custAddress << "\n" << endl;
	}
	void simpleInfo()
	{
		cout << "Hello: " << custName << " , Address: " << custAddress << endl;
	}

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

		return Customer(custName, custAddress);
	}

	Customer() {}
	~Customer() {}
	Customer(string n, string a) {
		custName = n;
		custAddress = a;
	}
}; // END OF CLASS

struct Beverage
{
	string name;	// The name
	int quantity{}; // The quantity
	double price{}; //The price

	void info()
	{
		cout << setw(10) << left << "Name" << name << "\n"
			<< setw(10) << left << "Price" << "$" << price << "\n"
			<< setw(10) << left << "Quantity" << quantity << "\n"
			<< setw(10) << left << "Total" << "$"
			<< quantity * price << "\n" << endl;
	}
	void simpleInfo()
	{
		cout << "Selection: " << name << ", price per unit: $" << price << endl;
	}

	Beverage() {}
	~Beverage() {}
	Beverage(string n, double p) {
		name = n;
		price = p;
	}
}; // END OF STRUCT

double totalCost(Beverage beverages[], int numOfBeverages);
void orderSummary(Beverage beverages[], int numOfBeverages);
bool isAllDigits(string input);
int getValidNumber(string text, int min, int max);
void doShopping();


void displayMenu(string userName) //Menu function
{

	cout << endl << endl
		<< userName
		<< ", Please select the beverage you would like to purchase from the menu: "
		<< endl;

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

int main(void)
{
	doShopping();

	return 0;
}


int getQuantity() //Quantity function
{
	int quantity = 0;
	cout << "Enter quantity : ";
	cin >> quantity;
	return quantity;
}


bool isAllDigits(string input)
{
	for (unsigned i = 0; i < input.length(); i++) {
		if (!isdigit(input[i])) {
			return false;
		}
	}
	return true;
}

int getValidNumber(string text, int min, int max)
{
	string input{};
	int num{};
	while (true)
	{
		cout << text << " [" << min << " - " << max << "]: ";
		getline(cin, input);

		stringstream ss(input);

		if (!isAllDigits(ss.str())) {
			cout << "\nInvalid input. Try again!" << endl;
		}
		else {
			if (isAllDigits(ss.str()) && ss >> num) {
				if (num >= min && num <= max) {
					break;
				}
				else {
					std::cout << "\nOut of range. Try again!" << endl;
				}
			}
		}
	}
	return num;
}


double totalCost(Beverage beverages[], int numOfBeverages)  //Total cost function
{
	double totalCost = 0;

	for (int i = 0; i < numOfBeverages; i++) {
		totalCost += (beverages[i].price*beverages[i].quantity);
	}
	return totalCost;

}


void orderSummary(Beverage beverages[], int numOfBeverages) //Summary function
{
	cout << "\n======= ORDER SUMMARY ====" << endl;
	cout << "Items selected" << endl << endl;
	for (int i = 0; i < numOfBeverages; i++) {
		// only show beverages that has at least one order
		if (beverages[i].quantity > 0) {
			beverages[i].info();
		}
	}
}


void doShopping() //Shopping function
{
	// declare our beverages 
	Beverage
		water("Water", 1.45),
		soda("Soda", 2.98),
		iceTea("Ice Tea", 3.29);

	// Beverage array to hold the beverages
	Beverage beverages[] = { water, soda, iceTea };

	char selection = ' ';

	Customer patron = snafu(); // get info, create patron
	patron.simpleInfo(); // greet patron 

	/*string name = "";
	string address = "";

	//Ask user for her/his name
	cout << "Please enter your name ==> ";
	getline(cin, name);

	cout << "Please enter your address ==> ";
	getline(cin, address);

	//display user name
	cout << "Hello " << name << endl;
	cout << "Address: " << address << endl;
	*/

	do
	{
		system("cls");
		// display menu
		displayMenu(name);

		// read user selection
		cout << "Your selection: ";
		cin >> selection;
		cin.get(); // takes last ENTER character in the stream

		switch (selection)
		{
		case '1':
			beverages[0].simpleInfo();
			// assuming the customer can shop at least 1 and max 100
			beverages[0].quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case '2':
			beverages[1].simpleInfo();
			beverages[1].quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case '3':
			beverages[2].simpleInfo();
			beverages[2].quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case 'X':
		case 'x':
			orderSummary(beverages, 3);
			if (totalCost(beverages, 3) > 0) {
				cout << "\nGrand total = $" << totalCost(beverages, 3) << endl;
			}
			cout << "\nThank you for your purchase, " << name << " Come back soon!!!" << endl;
			break;
			// other than 1, 2, 3 and X...
		default: cout << "Invalid selection. Please try again";
			cin.get();
			// no break in the default case
		}
		cout << endl << endl;
	} while (selection != 'X' && selection != 'x'); // 'X' or 'x' displays the summary
	system("pause"); //Pauses the system until a key is hit, The the program will end.
}



I appreciate any and all help figure this out anywhere that asks for 'name' need to ask for the name from the class.

Is This A Good Question/Topic? 0
  • +

Replies To: C++ HELP WITH ERROR USING CLASSES

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14092
  • View blog
  • Posts: 56,466
  • Joined: 12-June 08

Re: C++ HELP WITH ERROR USING CLASSES

Posted 28 February 2018 - 12:26 PM

Please copy/paste the entire error message here.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg   User is offline

  • member icon

Reputation: 5487
  • View blog
  • Posts: 17,063
  • Joined: 25-December 09

Re: C++ HELP WITH ERROR USING CLASSES

Posted 28 February 2018 - 12:40 PM

You really need to read up on class access specifiers. Remember with a class the default access specifier is private, so in your classes you have no way to access any of the members because they are all private and that a the default access specifier for a struct is public.


Edit: also here.

Jim

This post has been edited by jimblumberg: 28 February 2018 - 01:23 PM

Was This Post Helpful? 1
  • +
  • -

#4 josephhoffman28   User is offline

  • New D.I.C Head

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

Re: C++ HELP WITH ERROR USING CLASSES

Posted 28 February 2018 - 02:31 PM

I fixed my code but now I need to add a second member function to my customer class that does return the value of patron's customer name. I am not sure exactly how to do this. Here is my code below:

#include <iostream>
#include <string>
#include <iomanip>
#include <sstream> // stringstream
#include <cctype> // isalph, isdigit
using namespace std;


class Customer
{
	string custName; // Customer name
	string custAddress; //Customer Address

public:
	void info()
	{
		cout << setw(10) << left << "Name" << custName << "\n"
			<< setw(10) << left << "Address" << custAddress << "\n" << endl;
	}
public:
	void simpleInfo()
	{
		cout << "Hello: " << custName << " , Address: " << custAddress << endl;
	}

	Customer() {}
	~Customer() {}
	Customer(string n, string a) {
		custName = n;
		custAddress = a;
	}
}; // END OF CLASS

struct Beverage
{
	string name;	// The name
	int quantity{}; // The quantity
	double price{}; //The price

	void info()
	{
		cout << setw(10) << left << "Name" << name << "\n"
			<< setw(10) << left << "Price" << "$" << price << "\n"
			<< setw(10) << left << "Quantity" << quantity << "\n"
			<< setw(10) << left << "Total" << "$"
			<< quantity * price << "\n" << endl;
	}
	void simpleInfo()
	{
		cout << "Selection: " << name << ", price per unit: $" << price << endl;
	}

	Beverage() {}
	~Beverage() {}
	Beverage(string n, double p) {
		name = n;
		price = p;
	}
}; // END OF STRUCT

double totalCost(Beverage beverages[], int numOfBeverages);
void orderSummary(Beverage beverages[], int numOfBeverages);
bool isAllDigits(string input);
int getValidNumber(string text, int min, int max);
void doShopping();

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

	return Customer(custName, custAddress);
}


void displayMenu(string userName) //Menu function
{

	cout << endl << endl
		<< userName
		<< ", Please select the beverage you would like to purchase from the menu: "
		<< endl;

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

int main(void)
{
	doShopping();

	return 0;
}


int getQuantity() //Quantity function
{
	int quantity = 0;
	cout << "Enter quantity : ";
	cin >> quantity;
	return quantity;
}


bool isAllDigits(string input)
{
	for (unsigned i = 0; i < input.length(); i++) {
		if (!isdigit(input[i])) {
			return false;
		}
	}
	return true;
}

int getValidNumber(string text, int min, int max)
{
	string input{};
	int num{};
	while (true)
	{
		cout << text << " [" << min << " - " << max << "]: ";
		getline(cin, input);

		stringstream ss(input);

		if (!isAllDigits(ss.str())) {
			cout << "\nInvalid input. Try again!" << endl;
		}
		else {
			if (isAllDigits(ss.str()) && ss >> num) {
				if (num >= min && num <= max) {
					break;
				}
				else {
					std::cout << "\nOut of range. Try again!" << endl;
				}
			}
		}
	}
	return num;
}


double totalCost(Beverage beverages[], int numOfBeverages)  //Total cost function
{
	double totalCost = 0;

	for (int i = 0; i < numOfBeverages; i++) {
		totalCost += (beverages[i].price*beverages[i].quantity);
	}
	return totalCost;

}


void orderSummary(Beverage beverages[], int numOfBeverages) //Summary function
{
	cout << "\n======= ORDER SUMMARY ====" << endl;
	cout << "Items selected" << endl << endl;
	for (int i = 0; i < numOfBeverages; i++) {
		// only show beverages that has at least one order
		if (beverages[i].quantity > 0) {
			beverages[i].info();
		}
	}
}


void doShopping() //Shopping function
{
	// declare our beverages 
	Beverage
		water("Water", 1.45),
		soda("Soda", 2.98),
		iceTea("Ice Tea", 3.29);

	// Beverage array to hold the beverages
	Beverage beverages[] = { water, soda, iceTea };

	char selection = ' ';

	Customer patron = snafu(); // get info, create patron
	patron.simpleInfo(); // greet patron 

	/*string name = "";
	string address = "";

	//Ask user for her/his name
	cout << "Please enter your name ==> ";
	getline(cin, name);

	cout << "Please enter your address ==> ";
	getline(cin, address);

	//display user name
	cout << "Hello " << name << endl;
	cout << "Address: " << address << endl;
	*/

	do
	{
		system("cls");
		// display menu
		displayMenu(custName);

		// read user selection
		cout << "Your selection: ";
		cin >> selection;
		cin.get(); // takes last ENTER character in the stream

		switch (selection)
		{
		case '1':
			beverages[0].simpleInfo();
			// assuming the customer can shop at least 1 and max 100
			beverages[0].quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case '2':
			beverages[1].simpleInfo();
			beverages[1].quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case '3':
			beverages[2].simpleInfo();
			beverages[2].quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case 'X':
		case 'x':
			orderSummary(beverages, 3);
			if (totalCost(beverages, 3) > 0) {
				cout << "\nGrand total = $" << totalCost(beverages, 3) << endl;
			}
			cout << "\nThank you for your purchase, " << custName << " Come back soon!!!" << endl;
			break;
			// other than 1, 2, 3 and X...
		default: cout << "Invalid selection. Please try again";
			cin.get();
			// no break in the default case
		}
		cout << endl << endl;
	} while (selection != 'X' && selection != 'x'); // 'X' or 'x' displays the summary
	system("pause"); //Pauses the system until a key is hit, The the program will end.
}



The error I am getting now is:

Quote

In function 'void doShopping()':
212:15: error: 'custName' was not declared in this scope


Please advise.
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14092
  • View blog
  • Posts: 56,466
  • Joined: 12-June 08

Re: C++ HELP WITH ERROR USING CLASSES

Posted 28 February 2018 - 02:37 PM

014public:
015    void info()
016    {
017        cout << setw(10) << left << "Name" << custName << "\n"
018            << setw(10) << left << "Address" << custAddress << "\n" << endl;
019    }
020public:
021    void simpleInfo()
022    {
023        cout << "Hello: " << custName << " , Address: " << custAddress << endl;
024    }


Why do you have two 'publics' here?

Why are these two methods fleshed out here and not with the rest of the methods?


Quote

function to my customer class that does return the value of patron's customer name. I am not sure exactly how to do this.

You have examples of where you are using a method with a return.

Example:
063 bool isAllDigits(string input);


Why not use that line of thinking?
Was This Post Helpful? 0
  • +
  • -

#6 josephhoffman28   User is offline

  • New D.I.C Head

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

Re: C++ HELP WITH ERROR USING CLASSES

Posted 28 February 2018 - 02:57 PM

I am unsure how to do that can you give me some more explanation?
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14092
  • View blog
  • Posts: 56,466
  • Joined: 12-June 08

Re: C++ HELP WITH ERROR USING CLASSES

Posted 28 February 2018 - 03:01 PM

Ah.. which part? The part about not having two publics in the header, or the part about you using the same logic on the code you wrote with "063 bool isAllDigits(string input);" to make a function?

I would advocate peeping at the c++ 'basics' tutorials.. say 'Functions I' and 'Functions II'.

http://www.dreaminco...m/134-c-basics/
Was This Post Helpful? 0
  • +
  • -

#8 josephhoffman28   User is offline

  • New D.I.C Head

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

Re: C++ HELP WITH ERROR USING CLASSES

Posted 28 February 2018 - 04:05 PM

I read through those about functions and I also took out the second public:

My problem is they show math concepts but I am having trouble understanding the concept of returning the value of the customer name and address asked by the user at the beginning of the program so if I used:

bool customerName(string input)
	{
		for (unsigned i = 0; i < input.length(); i++) {
			if (!isdigit(input[i])) {
				return false;
			}
		}
		return true;
	}



This is using numbers like I did for my quantity, not name values I am having a hard time understanding that.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,455
  • Joined: 05-May 12

Re: C++ HELP WITH ERROR USING CLASSES

Posted 28 February 2018 - 06:30 PM

Short answer: A function can return any type. You are not restricted to primitive types like integers, floats, or booleans. Hint: You can return a string.

Long answer: Do a search for "getters and setters" and/or "C++ accessors". That is what your assignment instructions are essentially trying to get you to do.

Extra bonus points: Read Why getters and setters are evil and Getters/Setters. Evil. Period.
Was This Post Helpful? 0
  • +
  • -

#10 josephhoffman28   User is offline

  • New D.I.C Head

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

Re: C++ HELP WITH ERROR USING CLASSES

Posted 28 February 2018 - 08:38 PM

Okay I think I got it minus the end here is my code:

Class code with added return function does this look correct?:
class Customer
{
	string custName; // Customer name
	string custAddress; //Customer Address

public:
	void info()
	{
		cout << setw(10) << left << "Name" << custName << "\n"
			<< setw(10) << left << "Address" << custAddress << "\n" << endl;
	}
	void simpleInfo()
	{
		cout << "Hello: " << custName << ", Address: " << custAddress << endl;
	}
	std::string value() const {
		return custName;
	}

	Customer() {}
	~Customer() {}
	Customer(string n, string a) {
		custName = n;
		custAddress = a;
	}
}; // END OF CLASS



Full code:
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream> // stringstream
#include <cctype> // isalph, isdigit
using namespace std;


class Customer
{
	string custName; // Customer name
	string custAddress; //Customer Address

public:
	void info()
	{
		cout << setw(10) << left << "Name" << custName << "\n"
			<< setw(10) << left << "Address" << custAddress << "\n" << endl;
	}
	void simpleInfo()
	{
		cout << "Hello: " << custName << ", Address: " << custAddress << endl;
	}
	std::string value() const {
		return custName;
	}

	Customer() {}
	~Customer() {}
	Customer(string n, string a) {
		custName = n;
		custAddress = a;
	}
}; // END OF CLASS

struct Beverage
{
	string name;	// The name
	int quantity{}; // The quantity
	double price{}; //The price

	void info()
	{
		cout << setw(10) << left << "Name" << name << "\n"
			<< setw(10) << left << "Price" << "$" << price << "\n"
			<< setw(10) << left << "Quantity" << quantity << "\n"
			<< setw(10) << left << "Total" << "$"
			<< quantity * price << "\n" << endl;
	}
	void simpleInfo()
	{
		cout << "Selection: " << name << ", price per unit: $" << price << endl;
	}

	Beverage() {}
	~Beverage() {}
	Beverage(string n, double p) {
		name = n;
		price = p;
	}
}; // END OF STRUCT

double totalCost(Beverage beverages[], int numOfBeverages);
void orderSummary(Beverage beverages[], int numOfBeverages);
bool isAllDigits(string input);
int getValidNumber(string text, int min, int max);
void doShopping();

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

	return Customer(custName, custAddress);
}


void displayMenu(string userName) //Menu function
{

	cout << endl << endl
		<< userName
		<< ", Please select the beverage you would like to purchase from the menu: "
		<< endl;

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

int main(void)
{
	doShopping();

	return 0;
}


int getQuantity() //Quantity function
{
	int quantity = 0;
	cout << "Enter quantity : ";
	cin >> quantity;
	return quantity;
}


bool isAllDigits(string input)
{
	for (unsigned i = 0; i < input.length(); i++) {
		if (!isdigit(input[i])) {
			return false;
		}
	}
	return true;
}

int getValidNumber(string text, int min, int max)
{
	string input{};
	int num{};
	while (true)
	{
		cout << text << " [" << min << " - " << max << "]: ";
		getline(cin, input);

		stringstream ss(input);

		if (!isAllDigits(ss.str())) {
			cout << "\nInvalid input. Try again!" << endl;
		}
		else {
			if (isAllDigits(ss.str()) && ss >> num) {
				if (num >= min && num <= max) {
					break;
				}
				else {
					std::cout << "\nOut of range. Try again!" << endl;
				}
			}
		}
	}
	return num;
}


double totalCost(Beverage beverages[], int numOfBeverages)  //Total cost function
{
	double totalCost = 0;

	for (int i = 0; i < numOfBeverages; i++) {
		totalCost += (beverages[i].price*beverages[i].quantity);
	}
	return totalCost;

}


void orderSummary(Beverage beverages[], int numOfBeverages) //Summary function
{
	cout << "\n======= ORDER SUMMARY ====" << endl;
	cout << "Items selected" << endl << endl;
	for (int i = 0; i < numOfBeverages; i++) {
		// only show beverages that has at least one order
		if (beverages[i].quantity > 0) {
			beverages[i].info();
		}
	}
}


void doShopping() //Shopping function
{
	// declare our beverages 
	Beverage
		water("Water", 1.45),
		soda("Soda", 2.98),
		iceTea("Ice Tea", 3.29);

	// Beverage array to hold the beverages
	Beverage beverages[] = { water, soda, iceTea };

	char selection = ' ';

	Customer patron = snafu(); // get info, create patron
	patron.simpleInfo(); // greet patron 

						 /*string name = "";
						 string address = "";

						 //Ask user for her/his name
						 cout << "Please enter your name ==> ";
						 getline(cin, name);

						 cout << "Please enter your address ==> ";
						 getline(cin, address);

						 //display user name
						 cout << "Hello " << name << endl;
						 cout << "Address: " << address << endl;
						 */

	do
	{
		system("cls");
		// display menu
		displayMenu(custName);

		// read user selection
		cout << "Your selection: ";
		cin >> selection;
		cin.get(); // takes last ENTER character in the stream

		switch (selection)
		{
		case '1':
			beverages[0].simpleInfo();
			// assuming the customer can shop at least 1 and max 100
			beverages[0].quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case '2':
			beverages[1].simpleInfo();
			beverages[1].quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case '3':
			beverages[2].simpleInfo();
			beverages[2].quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case 'X':
		case 'x':
			orderSummary(beverages, 3);
			if (totalCost(beverages, 3) > 0) {
				cout << "\nGrand total = $" << totalCost(beverages, 3) << endl;
			}
			cout << "\nThank you for your purchase, " << custName << " Come back soon!!!" << endl;
			break;
			// other than 1, 2, 3 and X...
		default: cout << "Invalid selection. Please try again";
			cin.get();
			// no break in the default case
		}
		cout << endl << endl;
	} while (selection != 'X' && selection != 'x'); // 'X' or 'x' displays the summary
	system("pause"); //Pauses the system until a key is hit, The the program will end.
}



Still getting an error on line 214:
system("cls");
		// display menu
		displayMenu(custName);



Error:

Quote

In function 'void doShopping()':
214:15: error: 'custName' was not declared in this scope


What needs done to make this work? Please assist.
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg   User is offline

  • member icon

Reputation: 5487
  • View blog
  • Posts: 17,063
  • Joined: 25-December 09

Re: C++ HELP WITH ERROR USING CLASSES

Posted 01 March 2018 - 02:42 AM

Look at this snippet:
void doShopping() //Shopping function
{
	// declare our beverages 
	Beverage
		water("Water", 1.45),
		soda("Soda", 2.98),
		iceTea("Ice Tea", 3.29);

	// Beverage array to hold the beverages
	Beverage beverages[] = { water, soda, iceTea };

	char selection = ' ';

	Customer patron = snafu(); // get info, create patron
	patron.simpleInfo(); // greet patron 

	do
	{
		system("cls");
		// display menu
		displayMenu(custName);


Where, in that snippet, have you defined a variable named custName?

You can't use a variable until you have told the compiler about the variable (declared it).

Jim
Was This Post Helpful? 0
  • +
  • -

#12 josephhoffman28   User is offline

  • New D.I.C Head

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

Re: C++ HELP WITH ERROR USING CLASSES

Posted 01 March 2018 - 06:43 AM

Declared it. Now it asks what it should but after you enter your name and address it should show "Hello: " << custName << " , Address: " << custAddress it just goes straight to the menu. Also when it gets down to the menu it should say "'custName', Please select the beverage you would like to purchase from the menu:" instead it shows no custName. and at the end it should show "Thank you for your purchase, " << custName << " Come back soon!!!" no custName displays what is the problem?

My code:

#include <iostream>
#include <string>
#include <iomanip>
#include <sstream> // stringstream
#include <cctype> // isalph, isdigit
using namespace std;


class Customer
{
	string custName; // Customer name
	string custAddress; //Customer Address

public:
	void info()
	{
		cout << setw(10) << left << "Name" << custName << "\n"
			<< setw(10) << left << "Address" << custAddress << "\n" << endl;
	}
public:
	void simpleInfo()
	{
		cout << "Hello: " << custName << " , Address: " << custAddress << endl;
	}
public:
	void simpleReturn()
	{

	}

	Customer() {}
	~Customer() {}
	Customer(string n, string a) {
		custName = n;
		custAddress = a;
	}
}; // END OF CLASS

struct Beverage
{
	string name;	// The name
	int quantity{}; // The quantity
	double price{}; //The price

	void info()
	{
		cout << setw(10) << left << "Name" << name << "\n"
			<< setw(10) << left << "Price" << "$" << price << "\n"
			<< setw(10) << left << "Quantity" << quantity << "\n"
			<< setw(10) << left << "Total" << "$"
			<< quantity * price << "\n" << endl;
	}
	void simpleInfo()
	{
		cout << "Selection: " << name << ", price per unit: $" << price << endl;
	}

	Beverage() {}
	~Beverage() {}
	Beverage(string n, double p) {
		name = n;
		price = p;
	}
}; // END OF STRUCT

double totalCost(Beverage beverages[], int numOfBeverages);
void orderSummary(Beverage beverages[], int numOfBeverages);
bool isAllDigits(string input);
int getValidNumber(string text, int min, int max);
void doShopping();

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

	return Customer(custName, custAddress);
}


void displayMenu(string userName) //Menu function
{

	cout << endl << endl
		<< userName
		<< ", Please select the beverage you would like to purchase from the menu: "
		<< endl;

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

int main(void)
{
	doShopping();

	return 0;
}


int getQuantity() //Quantity function
{
	int quantity = 0;
	cout << "Enter quantity : ";
	cin >> quantity;
	return quantity;
}


bool isAllDigits(string input)
{
	for (unsigned i = 0; i < input.length(); i++) {
		if (!isdigit(input[i])) {
			return false;
		}
	}
	return true;
}

int getValidNumber(string text, int min, int max)
{
	string input{};
	int num{};
	while (true)
	{
		cout << text << " [" << min << " - " << max << "]: ";
		getline(cin, input);

		stringstream ss(input);

		if (!isAllDigits(ss.str())) {
			cout << "\nInvalid input. Try again!" << endl;
		}
		else {
			if (isAllDigits(ss.str()) && ss >> num) {
				if (num >= min && num <= max) {
					break;
				}
				else {
					std::cout << "\nOut of range. Try again!" << endl;
				}
			}
		}
	}
	return num;
}


double totalCost(Beverage beverages[], int numOfBeverages)  //Total cost function
{
	double totalCost = 0;

	for (int i = 0; i < numOfBeverages; i++) {
		totalCost += (beverages[i].price*beverages[i].quantity);
	}
	return totalCost;

}


void orderSummary(Beverage beverages[], int numOfBeverages) //Summary function
{
	cout << "\n======= ORDER SUMMARY ====" << endl;
	cout << "Items selected" << endl << endl;
	for (int i = 0; i < numOfBeverages; i++) {
		// only show beverages that has at least one order
		if (beverages[i].quantity > 0) {
			beverages[i].info();
		}
	}
}


void doShopping() //Shopping function
{
	// declare our beverages 
	Beverage
		water("Water", 1.45),
		soda("Soda", 2.98),
		iceTea("Ice Tea", 3.29);

	// Beverage array to hold the beverages
	Beverage beverages[] = { water, soda, iceTea };

	char selection = ' ';
	std::string custName;

	Customer patron = snafu(); // get info, create patron
	patron.simpleInfo(); // greet patron 

	/*string name = "";
	string address = "";

	//Ask user for her/his name
	cout << "Please enter your name ==> ";
	getline(cin, name);

	cout << "Please enter your address ==> ";
	getline(cin, address);

	//display user name
	cout << "Hello " << name << endl;
	cout << "Address: " << address << endl;
	*/

	do
	{
		system("cls");
		// display menu
		displayMenu(custName);

		// read user selection
		cout << "Your selection: ";
		cin >> selection;
		cin.get(); // takes last ENTER character in the stream

		switch (selection)
		{
		case '1':
			beverages[0].simpleInfo();
			// assuming the customer can shop at least 1 and max 100
			beverages[0].quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case '2':
			beverages[1].simpleInfo();
			beverages[1].quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case '3':
			beverages[2].simpleInfo();
			beverages[2].quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case 'X':
		case 'x':
			orderSummary(beverages, 3);
			if (totalCost(beverages, 3) > 0) {
				cout << "\nGrand total = $" << totalCost(beverages, 3) << endl;
			}
			cout << "\nThank you for your purchase, " << custName << " Come back soon!!!" << endl;
			break;
			// other than 1, 2, 3 and X...
		default: cout << "Invalid selection. Please try again";
			cin.get();
			// no break in the default case
		}
		cout << endl << endl;
	} while (selection != 'X' && selection != 'x'); // 'X' or 'x' displays the summary
	system("pause"); //Pauses the system until a key is hit, The the program will end.
}


Was This Post Helpful? 0
  • +
  • -

#13 josephhoffman28   User is offline

  • New D.I.C Head

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

Re: C++ HELP WITH ERROR USING CLASSES

Posted 01 March 2018 - 10:09 AM

Okay I finally understand!!!


The only thing that is not working is after you enter you name and address it does not show the greeting message like it should?

Quote

Hello: custName, Adress: custAdress


Here is my updated code:

#include <iostream>
#include <string>
#include <iomanip>
#include <sstream> // stringstream
#include <cctype> // isalph, isdigit
using namespace std;


class Customer
{
	std::string custName; // Customer name
	std::string custAddress; //Customer Address

public:
	void info()
	{
		cout << setw(10) << left << "Name" << custName << "\n"
			<< setw(10) << left << "Address" << custAddress << "\n" << endl;
	}

	void simpleInfo()
	{
		cout << "Hello: " << custName << " , Address: " << custAddress << endl;
	}
	std::string value() const
	{
		return custName;
	}

	Customer() {}
	~Customer() {}
	Customer(string n, string a) {
		custName = n;
		custAddress = a;
	}
}; // END OF CLASS

struct Beverage
{
	string name;	// The name
	int quantity{}; // The quantity
	double price{}; //The price

	void info()
	{
		cout << setw(10) << left << "Name" << name << "\n"
			<< setw(10) << left << "Price" << "$" << price << "\n"
			<< setw(10) << left << "Quantity" << quantity << "\n"
			<< setw(10) << left << "Total" << "$"
			<< quantity * price << "\n" << endl;
	}
	void simpleInfo()
	{
		cout << "Selection: " << name << ", price per unit: $" << price << endl;
	}

	Beverage() {}
	~Beverage() {}
	Beverage(string n, double p) {
		name = n;
		price = p;
	}
}; // END OF STRUCT

double totalCost(Beverage beverages[], int numOfBeverages);
void orderSummary(Beverage beverages[], int numOfBeverages);
bool isAllDigits(string input);
int getValidNumber(string text, int min, int max);
void doShopping();

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

	return Customer(custName, custAddress);
}


void displayMenu(string userName) //Menu function
{

	cout << endl << endl
		<< userName
		<< ", Please select the beverage you would like to purchase from the menu: "
		<< endl;

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

int main(void)
{
	doShopping();

	return 0;
}


int getQuantity() //Quantity function
{
	int quantity = 0;
	cout << "Enter quantity : ";
	cin >> quantity;
	return quantity;
}


bool isAllDigits(string input)
{
	for (unsigned i = 0; i < input.length(); i++) {
		if (!isdigit(input[i])) {
			return false;
		}
	}
	return true;
}

int getValidNumber(string text, int min, int max)
{
	string input{};
	int num{};
	while (true)
	{
		cout << text << " [" << min << " - " << max << "]: ";
		getline(cin, input);

		stringstream ss(input);

		if (!isAllDigits(ss.str())) {
			cout << "\nInvalid input. Try again!" << endl;
		}
		else {
			if (isAllDigits(ss.str()) && ss >> num) {
				if (num >= min && num <= max) {
					break;
				}
				else {
					std::cout << "\nOut of range. Try again!" << endl;
				}
			}
		}
	}
	return num;
}


double totalCost(Beverage beverages[], int numOfBeverages)  //Total cost function
{
	double totalCost = 0;

	for (int i = 0; i < numOfBeverages; i++) {
		totalCost += (beverages[i].price*beverages[i].quantity);
	}
	return totalCost;

}


void orderSummary(Beverage beverages[], int numOfBeverages) //Summary function
{
	cout << "\n======= ORDER SUMMARY ====" << endl;
	cout << "Items selected" << endl << endl;
	for (int i = 0; i < numOfBeverages; i++) {
		// only show beverages that has at least one order
		if (beverages[i].quantity > 0) {
			beverages[i].info();
		}
	}
}


void doShopping() //Shopping function
{
	// declare our beverages 
	Beverage
		water("Water", 1.45),
		soda("Soda", 2.98),
		iceTea("Ice Tea", 3.29);

	// Beverage array to hold the beverages
	Beverage beverages[] = { water, soda, iceTea };

	char selection = ' ';
	//std::string custName;

	Customer patron = snafu(); // get info, create patron
	patron.simpleInfo(); // greet patron 

	do
	{
		system("cls");
		// display menu
		displayMenu(patron.value());

		// read user selection
		cout << "Your selection: ";
		cin >> selection;
		cin.get(); // takes last ENTER character in the stream

		switch (selection)
		{
		case '1':
			beverages[0].simpleInfo();
			// assuming the customer can shop at least 1 and max 100
			beverages[0].quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case '2':
			beverages[1].simpleInfo();
			beverages[1].quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case '3':
			beverages[2].simpleInfo();
			beverages[2].quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case 'X':
		case 'x':
			orderSummary(beverages, 3);
			if (totalCost(beverages, 3) > 0) {
				cout << "\nGrand total = $" << totalCost(beverages, 3) << endl;
			}
			cout << "\nThank you for your purchase, " << patron.value() << " Come back soon!!!" << endl;
			break;
			// other than 1, 2, 3 and X...
		default: cout << "Invalid selection. Please try again";
			cin.get();
			// no break in the default case
		}
		cout << endl << endl;
	} while (selection != 'X' && selection != 'x'); // 'X' or 'x' displays the summary
	system("pause"); //Pauses the system until a key is hit, The the program will end.
}


Was This Post Helpful? 0
  • +
  • -

#14 josephhoffman28   User is offline

  • New D.I.C Head

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

Re: C++ HELP WITH ERROR USING CLASSES

Posted 02 March 2018 - 08:54 AM

Please assist!!

I am having issues displaying the greeting message after you enter the name and address it should say:

Quote

Hello: custName, Address: custAdress


This will not display after you enter name and address it goes right to the displayMenu.

Class code where the greeting resides:

Quote

lines 9-36

class Customer
{
	std::string custName; // Customer name
	std::string custAddress; //Customer Address

public:
	void info()
	{
		cout << setw(10) << left << "Name" << custName << "\n"
			<< setw(10) << left << "Address" << custAddress << "\n" << endl;
	}

	void simpleInfo()
	{
		cout << "Hello: " << custName << " , Address: " << custAddress << endl;
	}
	std::string value() const
	{
		return custName;
	}

	Customer() {}
	~Customer() {}
	Customer(string n, string a) {
		custName = n;
		custAddress = a;
	}
}; // END OF CLASS



Code where class is called lines:

Quote

195-196

Customer patron = snafu(); // get info, create patron
	patron.simpleInfo(); // greet patron 



Can you please assist me with this issue? Am I doing something incorrectly please advise.
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,455
  • Joined: 05-May 12

Re: C++ HELP WITH ERROR USING CLASSES

Posted 02 March 2018 - 09:29 AM

It shows the greeting, but you clear the screen as the first thing in your do-while loop, so you don't see the greeting unless you have very fast eyes, and instead you see the menu.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1