Assistance With Classes (This is to help understand for my Studies - applies to homework)

I am an adult, very new to programming, and working on a C++ course. We have been growing our assignment from Week 1, where the code we have written continues to evolve based on new learned concepts.

We are currently learning the class concept, and I am honestly having trouble understanding how it applies. I've tried watching videos (Bucky's for example) and doing some reading, but I am just not getting it. I'll place my code below, but please remember 1) I am brand new - I am sure this is not efficient nor is it 100% right, 2) As mentioned this is in support of homework. While this will not give me "the answer" - it will help put me on the right path, and 3) I am very appreciative of anyone who takes the time to help.

My goal is to create a customer class that gets at least the user name and user address. This is in the context of a virtual store, where we sell items, let the customer know what has been purchased and the cost, and thank them.

I have added switch statements to allow for corrections in the address - more as a test to see if I am "getting concepts" than anything. However, that is not required. Please see my code below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Initial Project Week 1. Goal is to create a responsive store.
//Some of these are not used. They are here in case I decided to make alterations and use.
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <iomanip>


// put in place to eliminate the need to type std prior to cin/cout etc. due to C++ being built upon C
using namespace std;

// functions should be before main, and as such are here and below

// This function just displays the store name upon beginning the program


void storeName();

int totalCost(int, int, int, int, int);

void displayGoodbye(string);



// function main begins program execution - variables are identified
int main() {
	string userName;
	string userAddress;
	char userSelection;
	char userConfirm;
	int starwarsNum = 0, starshipNum = 0, indyNum = 0, futureNum = 0, gfNum = 0;
	int starwarsCost = 0, starshipCost = 0, indyCost = 0, futureCost = 0, gfCost = 0;
	int total;
	bool done = false;
	//int userConfirm = 0;

	storeName();


	cout << "Welcome to J-Mart! So that we may best suit your needs, \
please provide me with your name by typing it here ==> ";

	//This is where the username is captured
	getline(cin, userName);

	cout << "\n" << "We at J-Mart are so excited you chose us for your shopping needs, " << \
		userName << "." << "\n\n" << endl;


	cout << userName << ", please provide us with your shipping information to better improve \
your shopping experience. Please use the following format: Street Number and Street Name, \
Apartment (if applicable), City, State, Zip Code. ** Of note - we currently can only \
take orders shipping to U.S. addresses and APO addresses. **" << "\n" << endl;

	// Here a do...while loop begins to capture the user address and allow for corrections
	do {
		cout << "Please enter your address here, ==> ";

		getline(cin, userAddress);

		cout << "\n" << "Thank you so much " << userName << ". We have updated our records.  You address is " << userAddress << endl << " Do you need to make \
any changes "<< userName << "? " << "\n" << endl;

		cout << "Enter Y for Yes " << "\n" << endl;
		cout << "Enter N for No." << "\n" << endl;

		cin >> userConfirm;
		cin.ignore();
// This is the case to decide if corrections are needed
		switch (userConfirm) {
		case 'y':
		case 'Y':
			cout << "\n" << "Sorry, let's correct that issue. Enter your correct address: " << endl;
			done = false;
			break;

		case 'n':
		case 'N':
			cout << "\n" << "Wonderful! " << userName << ", thanks for providing your shipping information." << endl;
			done = true;
			break;

		default:
			cout << "\n" << "It looks like you have made an invalid selection." << endl << endl;
			done = false;
		}
		

	} while (done != true);


// This do...while loop captures the entirety of the order.
	do {
		cout << "\n" << "Please select which purchase you would like to make. Make your selections by typing \
the associated number only for each item only." << endl;

		cout << "\n" << "1 - Star Wars Complete Box Set - Digital Only - $60 each" << endl;
		cout << "\n" << "2 - Starship Troopers Complete Box Set - Digital Only - $30 each" << endl;
		cout << "\n" << "3 - Indiana Jones Complete Box Set - Digital Only - $45 each" << endl;
		cout << "\n" << "4 - Back to the Future Complete Box Set - Digital Only - $35 each" << endl;
		cout << "\n" << "5 - The Godfather Complete Box Set - Digital Only - $50 each" << endl;
		cout << "\n" << "6 - Calculate totals and exit." << "\n" << endl;

		cin >> userSelection;

		//Responses will be output based on user selections. If the user selects 6, the while loop is exited and the program exits.
		switch (userSelection) {

		case '1':
			cout << "\n" << "You have selected Star Wars Complete Box Set. May the force be with you!" << " \
\n" << "How many would you like?" << "\n" << endl;
			cin >> starwarsNum;
			starwarsCost = starwarsNum * 60;
			done = false;
			break;

		case '2':
			cout << "\n" << "Citizen! You have selected Starship Troopers Complete Box Set - the bugs don't stand a chance!" << "\
\n" << "How many would you like?" << "\n" << endl;
			cin >> starshipNum;
			starshipCost = starshipNum * 30;
			done = false;
			break;

		case '3':
			cout << "\n" << "You have selected Indiana Jones Complete Box Set. I hope you like snakes..." << endl;
			cout << "\n" << "How many would you like?" << endl;
			cin >> indyNum;
			indyCost = indyNum * 45;
			done = false;
			break;

		case '4':
			cout << "\n" << "Great Scott! You have selected Back to the Future Complete Box Set." << endl;
			cout << "\n" << "How many would you like?" << endl;
			cin >> futureNum;
			futureCost = futureNum * 35;
			done = false;
			break;

		case '5':
			cout << "\n" << "You have selected The Godfather Complete Box Set. I'd recommend not sleeping with the fishes while watching this \
 film." << "\n" << endl;
			cout << "How many would you like?" << endl;
			cin >> gfNum;
			gfCost = gfNum * 50;
			done = false;
			break;

		case'6':
			cout << "Thank you!!!" << endl << endl << endl;
			done = true;
			break;

		default:
			cout << "\n" << "Oops. It appears you made an improper selection. Try again." << endl;
			done = false;
			}
				
		} while (done != true);

		cout << userName << ", " << "you have ordered " << starwarsNum << " Star Wars Complete Box Set(s) for a cost of $" << starwarsCost << "." << endl;
		cout << "You have ordered " << starshipNum << " Starship Trooper Complete Box Set(s) for a cost of $" << starshipCost << "." << endl;
		cout << "You have ordered " << indyNum << " Indianna Jones Complete Box Set(s) for a cost of $" << indyCost << "." << endl;
		cout << "You have ordered " << futureNum << " Back to the Future Complete Box Set(s) for a cost of $" << futureCost << "." << endl;
		cout << "You have ordered " << gfNum << " God Father Complete Box Set(s) for a cost of $" << gfCost << "." << endl << endl << endl;

		// Calling the function totalCost to input into the output statement providing the customer with the final cost
		total = totalCost(starwarsCost, starshipCost, indyCost, futureCost, gfCost);

		cout << "Your total today comes to $" << total << endl << endl << endl;

		displayGoodbye("Thanks again for choosing J-Mart for your video needs!");

	return 0;
}

// This function when called calculates the total price of the order
int totalCost(int starwarsCost, int starshipCost, int indyCost, int futureCost, int gfCost)
{
	int total = starwarsCost + starshipCost+ indyCost + futureCost + gfCost;
	return total;
}

// This function when called displays the store name
void storeName()
{
	cout << "\n      ---------------" << endl;
	cout << "\n        J-Mart" << endl;
	cout << "\n	Video Store" << endl;
	cout << "\n      ---------------" << endl;

	cout << "\n" << "\n" << endl;
}

// This function when called does an output of the message take into the function
void displayGoodbye(string msg) {
	
	cout << msg << endl;
}


Regards,
J
I have updated to this, using a template I found in my text. Does this appear to be proper?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

//header

#include<string>

using namespace std;


class Account {
	
	
	private: 
		string address;
		string name;
	
	
	public:
		void setAddress(string userAddress) {
			address = userAddress;
		}
		string getAddress() const {
			return address;
		}
		void setName(string userName)  {
			name = userName;
		}
		string getName() const {
			return name;
		}
		
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

//Main.cpp
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <iomanip>

#include "Account.h"

using namespace std;


void storeName();

int totalCost(int, int, int, int, int);

void displayGoodbye(string);



// function main begins program execution - variables are identified
int main() {
	Account userAccount;
	string Name;
	string Address;	
	char userSelection;
	char userConfirm;
	int starwarsNum = 0, starshipNum = 0, indyNum = 0, futureNum = 0, gfNum = 0;
	int starwarsCost = 0, starshipCost = 0, indyCost = 0, futureCost = 0, gfCost = 0;
	int total;
	bool done = false;
	//int userConfirm = 0;

	storeName();


	cout << "Welcome to J-Mart! So that we may best suit your needs, \
please provide me with your name by typing it here ==> ";

	//This is where the username is captured
	getline(cin, Name);
	userAccount.setName(Name);

	cout << "\n" << "We at J-Mart are so excited you chose us for your shopping needs, " << \
		Name << "." << "\n\n" << endl;


	cout << Name << ", please provide us with your shipping information to better improve \
your shopping experience. Please use the following format: Street Number and Street Name, \
Apartment (if applicable), City, State, Zip Code. ** Of note - we currently can only \
take orders shipping to U.S. addresses and APO addresses. **" << "\n" << endl;

	// Here a do...while loop begins to capture the user address and allow for corrections
	do {
		cout << "Please enter your address here, ==> ";

		getline(cin, Address);
		userAccount.setAddress(Address);

		cout << "\n" << "Thank you so much " << Name << ". We have updated our records.  You address is " << Address << endl << " Do you need to make \
any changes "<< Name << "? " << "\n" << endl;

		cout << "Enter Y for Yes " << "\n" << endl;
		cout << "Enter N for No." << "\n" << endl;

		cin >> userConfirm;
		cin.ignore();
// This is the case to decide if corrections are needed
		switch (userConfirm) {
		case 'y':
		case 'Y':
			cout << "\n" << "Sorry, let's correct that issue. Enter your correct address: " << endl;
			done = false;
			break;

		case 'n':
		case 'N':
			cout << "\n" << "Wonderful! " << Name << ", thanks for providing your shipping information." << endl;
			done = true;
			break;

		default:
			cout << "\n" << "It looks like you have made an invalid selection." << endl << endl;
			done = false;
		}
		

	} while (done != true);


// This do...while loop captures the entirety of the order.
	do {
		cout << "\n" << "Please select which purchase you would like to make. Make your selections by typing \
the associated number only for each item only." << endl;

		cout << "\n" << "1 - Star Wars Complete Box Set - Digital Only - $60 each" << endl;
		cout << "\n" << "2 - Starship Troopers Complete Box Set - Digital Only - $30 each" << endl;
		cout << "\n" << "3 - Indiana Jones Complete Box Set - Digital Only - $45 each" << endl;
		cout << "\n" << "4 - Back to the Future Complete Box Set - Digital Only - $35 each" << endl;
		cout << "\n" << "5 - The Godfather Complete Box Set - Digital Only - $50 each" << endl;
		cout << "\n" << "6 - Calculate totals and exit." << "\n" << endl;

		cin >> userSelection;

		//Responses will be output based on user selections. If the user selects 6, the while loop is exited and the program exits.
		switch (userSelection) {

		case '1':
			cout << "\n" << "You have selected Star Wars Complete Box Set. May the force be with you!" << " \
\n" << "How many would you like?" << "\n" << endl;
			cin >> starwarsNum;
			starwarsCost = starwarsNum * 60;
			done = false;
			break;

		case '2':
			cout << "\n" << "Citizen! You have selected Starship Troopers Complete Box Set - the bugs don't stand a chance!" << "\
\n" << "How many would you like?" << "\n" << endl;
			cin >> starshipNum;
			starshipCost = starshipNum * 30;
			done = false;
			break;

		case '3':
			cout << "\n" << "You have selected Indiana Jones Complete Box Set. I hope you like snakes..." << endl;
			cout << "\n" << "How many would you like?" << endl;
			cin >> indyNum;
			indyCost = indyNum * 45;
			done = false;
			break;

		case '4':
			cout << "\n" << "Great Scott! You have selected Back to the Future Complete Box Set." << endl;
			cout << "\n" << "How many would you like?" << endl;
			cin >> futureNum;
			futureCost = futureNum * 35;
			done = false;
			break;

		case '5':
			cout << "\n" << "You have selected The Godfather Complete Box Set. I'd recommend not sleeping with the fishes while watching this \
 film." << "\n" << endl;
			cout << "How many would you like?" << endl;
			cin >> gfNum;
			gfCost = gfNum * 50;
			done = false;
			break;

		case'6':
			cout << "Thank you!!!" << endl << endl << endl;
			done = true;
			break;

		default:
			cout << "\n" << "Oops. It appears you made an improper selection. Try again." << endl;
			done = false;
			}
				
		} while (done != true);

		cout << Name << ", " << "you have ordered " << starwarsNum << " Star Wars Complete Box Set(s) for a cost of $" << starwarsCost << "." << endl;
		cout << "You have ordered " << starshipNum << " Starship Trooper Complete Box Set(s) for a cost of $" << starshipCost << "." << endl;
		cout << "You have ordered " << indyNum << " Indianna Jones Complete Box Set(s) for a cost of $" << indyCost << "." << endl;
		cout << "You have ordered " << futureNum << " Back to the Future Complete Box Set(s) for a cost of $" << futureCost << "." << endl;
		cout << "You have ordered " << gfNum << " God Father Complete Box Set(s) for a cost of $" << gfCost << "." << endl << endl << endl;

		// Calling the function totalCost to input into the output statement providing the customer with the final cost
		total = totalCost(starwarsCost, starshipCost, indyCost, futureCost, gfCost);

		cout << "Your total today comes to $" << total << endl << endl << endl;

		displayGoodbye("Thanks again for choosing J-Mart for your video needs!");

	return 0;
}

// This function when called calculates the total price of the order
int totalCost(int starwarsCost, int starshipCost, int indyCost, int futureCost, int gfCost)
{
	int total = starwarsCost + starshipCost+ indyCost + futureCost + gfCost;
	return total;
}

// This function when called displays the store name
void storeName()
{
	cout << "\n      ---------------" << endl;
	cout << "\n        J-Mart" << endl;
	cout << "\n	Video Store" << endl;
	cout << "\n      ---------------" << endl;

	cout << "\n" << "\n" << endl;
}

// This function when called does an output of the message take into the function
void displayGoodbye(string msg) {
	
	cout << msg << endl;
}



Again, I am merely trying to learn - not have my work done for me. Thanks for your time and patience.


Regards,
J
Hi, Gr8Tortuga.
People usually come here to ask for help about specific problems and your question is too vague.
Does your program compile? Does it give you any trouble?

- - -
Just a note.
Let me only suggest you not to spend too time on graphic appearance, since it’s very difficult to get a good one in a textual console: it’s better to focus on the code.
Don’t assume everybody uses a 24" display: try to read or execute your code on a 13" notebook and notice how it looks.
In general, it’s better to remain inside a reasonable line length, let’s say 100-120 characters, to keep the code readable (I’m used to splitting my IDE window in two when I write classes, so I still stick to the old-fashionable 80 characters border). That’s apply both to code lines and console output.
The usage of a backslash ‘\’ to divide string literals is correct, but there’s a far simpler method: just close and re-open commas:
1
2
3
4
5
6
7
8
9
std::cout << "Lorem ipsum dolor sit amet, ei usu exerci vocibus invidunt, ea tantas "
             "civibus sed, ne vitae alterum probatus eam. Assum ornatus ius ea, "
             "autem commodo tamquam eam ex.\nEst habeo signiferumque an, ut his "
             "novum definitionem.\nQuod hendrerit omittantur ad duo, ei usu diceret "
             "delicata.\nTe cum populo scripta gubergren.\n\n"
             "Duo vide consul et, ne est abhorreant moderatius. Option partiendo eos "
             "eu, per ea timeam mandamus mediocritatem.\nNec at minim sonet consequuntur."
             "\nSea eu semper aliquid volumus, et solum copiosae pertinax eum.\n"
             "Possit dictas facilisis mei ex.\n";


Happy coding!

@Gr8Tortuga, classes, conceptually, are for packaging pertinent data together under one roof. All Account-related methods, etc. should be under Account. Perhaps you could eventually create a Store class and encapsulate all the store ideas in there.

Other things to try:
- make an explicit constructor for Account (the compiler automatically made one for you). Initialize "address" and "name" private variables in there. Put a breakpoint to get an idea of when this is being called. Try to then refactor to initialize these same variables using "initializer list" notation, and also put something else in the constructor (e.g. int k=5; k+=1;) put breakpoint on the initializer list and in the constructor to verify which one gets called first.
- make an explicit destructor for Account (the compiler automatically made one for you). As long as you stick with STL structures, you usually won't need to do much here, since they clean up on their own, but for now get a feel for when this is called. In main() do something like { Account k; } , with the extra curly brackets on purpose. An account will be created and destroyed in this scope and you'll be able to see this if you breakpoint inside the destructor (again, write something harmless in there).
- experiment with constructor overloading. Perhaps have both Account() and Account(string address, string name) constructors.

Side note: from what I've seen, private variables are usually written with trailing underscore (e.g. address_ , name_ ). When you have bigger classes, it sometimes helps at a glance to know whether something was public or private, though some modern IDEs are helping out with clever syntax highlighting (at least for locals vs. members).

Google is your friend. Good luck.
Topic archived. No new replies allowed.