Need help with Shopping program?

Hey guys, so I was writing a code for a shopping program assignment, ad the directions are as follows:
Write a program in C++ that allows a customer to go on-line shopping. This is a start-up venture and the stock of our on-line company is currently limited to the following items:
1. A gift card to Home Depot, $50.00
2. A bottle of cologne (The One by Dolce Gabbana), $24.00
3. A key chain with a bath tub ornament, $14.00
4. A card, $4.00
Although all items are in stock, the customer should only be made aware of the items that he or she can afford. Each available item has a specific int purchase code associated with it. To purchase the desired item, the customer has to enter the specified code. Please be aware that there are many customers who just want to waste our precious computer time and will try to go shopping without any money, we cannot allow this.
The program should work as follows:
Prompt the customer to enter his or her first name and the amount of money they have to spend. If they have no money or worse they are in debt and enter a negative amount, just tell them that they need to earn some money first and do not display any choices. If they have money to spend, the program should determine which items, if any, they can afford to purchase. Each item they can afford should be displayed along with the unique single digit purchase code required to select that item. Assuming a purchase can be made (i.e. the customer can afford at least one item), prompt the user to enter the purchase code of the item they wish to purchase. You can assume that the customer will only enter a purchase code from the displayed list of options. Once the customer has indicated his or her choice, the program should ask the customer if they would like an on-line receipt. The program should then prompt them for their preferred e-mail address or not. As a final courtesy, the program should inform the customer how much cash they still have available to spend, just in case they choose to go shopping again.
Challenge:
1. Write a function displayItems that display the list of choices they can afford using a correct numerical order. Think what argument you will need to pass to it. Example: ' 1. A gift card to Home Depot, $50.00 2. A bottle of cologne, $24.00 3. A key chain with a bathtub ornament, $14.00 4. A card $4.00
or 3. A key chain with a bathtub ornament, $14.00 4. A card $4.00
2. getName, returns the user name.
3. displayItems, displays the items that the user can afford
4. canAfford, returns true if the user can afford a specific item, false otherwise. This function should be called from function displayItems.
5. getChoice, returns the user choice (1-4). This function uses the return value of function canAfford to determine if the user can afford the item they selected.
6. shopMore, returns true if the user wishes to continue shopping, false otherwise.
7. Use constants to represent the prices of the different items.
8. Be careful of customer's who try and pull a fast one and enter a purchase code of an item that was not in their list of choices.
9. Allow the customer the option of continuing to shop if they still have money available after making a purchase. Note that this involves implementing a loop.

I have written the following code, although I'm not sure whether the arguments were passed correctly. I would like to know if there are any mistakes in passing the variables, and whether each function is written correctly. Although it seems like the logic makes sense in the code, I am still a little confused. Thanks so much!


#include <iostream>
#include <string>
using namespace std;

void displayItems(float budget);
float getName(string name, float budget);
bool canAfford(string& name1, float& budget1);
void getChoice(string& name1, float& budget1, int choice);
bool shopMore();

const float GIFTCARD = 50.00;
const float COLOGNE = 24.00;
const float KEY_CHAIN = 14.00;
const float CARD = 4.00;

int main()
{
string userName;
float money;
int choice;

getName(userName, money);
displayItems(userName, money);
return 0;
}

float getName(string name, float budget)
{
cout << "Welcome! Who do I have the pleasure of assisting today?" << endl;
cin >> name;
cout << "Excellent " << name << ", and how much money do you have to spend today?" << endl;
cin >> budget;
}

bool canAfford(string& name1, float& budget1)
{
bool afford = false;
if (budget1 >= CARD)
{
afford = true;
cout << "Well " << name1 << " that means you can afford: " << endl;
}
else
cout << "Come back after earning more money." << endl;
return afford;
}

void displayItems(float budget)
{
canAfford(budget);

if (budget >= (GIFTCARD + COLOGNE + KEYCHAIN + CARD))
{
cout << "1. A gift card to Home Depot: $50.00" << endl;
cout << "2. A bottle of cologne: $24.00" << endl;
cout << "3. A keychain with a bathtub ornament: $14.00" << endl;
cout << "4. A boring old card: $4.00" << endl;
}
else if (budget >= (COLOGNE + KEYCHAIN + CARD))
{
cout << "2. A bottle of cologne: $24.00" << endl;
cout << "3. A keychain with a bathtub ornament: $14.00" << endl;
cout << "4. A boring old card: $4.00" << endl;
}
else if (budget >= (KEYCHAIN + CARD))
{
cout << "3. A keychain with a bathtub ornament: $14.00" << endl;
cout << "4. A boring old card: $4.00" << endl;
}
else if (budget >= CARD)
{
cout << "4. A boring old card: $4.00" << endl;
}
else
cout << "You need to earn some money first to afford anything here." << endl;
canAfford();
}


void getChoice(string& name, float& budget, int choice)
{
float remaining;

cout << "Enter your choice of what you would like to purchase (0 to cancel): " << endl;
cin >> choice;
canAfford(choice);
if (choice == 0)
{
cout << "Goodbye" << endl;
return 0;
}
else if (choice == 1)
{
remaining = budget - 50.00;
cout << "Thank you " << name << endl;
cout << "You spent $50.00, and now have $" << remaining << " left to spend." << endl;
}
else if (choice == 2)
{
remaining = budget - 24.00;
cout << "Thank you " << name << endl;
cout << "You spent $24.00, and now have $" << remaining << " left to spend." << endl;
}
else if (choice == 3)
{
remaining = budget - 14.00;
cout << "Thank you " << name << endl;
cout << "You spent $14.00, and now have $" << remaining << " left to spend." << endl;
}
else if (choice == 4)
{
remaining = budget - 4.00;
cout << "Thank you " << name << endl;
cout << "You spent $4.00, and now have $" << remaining << " left to spend." << endl;
}
shopMore();
if(shopMore = true)
{
displayItems();
}
else if (shopMore = false)
{
cout << "Thank you for shopping, come again!" << endl;
}


}
bool shopMore()
{
int choice;
do
{
cout << "Would you like to keep shopping? Enter 1 for yes, 2 for no." << endl;
cin >> choice;
if (choice == 1)
{
return true;
}
else if (choice == 2)
{
return false;
}
else
{
cout << "Invalid input, you must enter either 1 or 2" << endl;
}
} while(choice != 1 && choice != 2);
}
First, for the obvious problems. Couple things I see first, check your variables have the correct name throughout your program. Example, you declare KEY_CHAIN with the underscore yet when you use it in the program you forgot the underscore. Next, you need to check all your functions, at many points when you are calling a function you are attempting to pass it 2 variables when it's only built to take 1 or a similar problem. Also, theirs places where your pass it the right number of variable but of the wrong type. Theirs more but these are a good starting point. The best way see if your program works is to run it and see what error messages you get.
What do you mean when you say " calling a function you are attempting to pass it 2 variables when it's only built to take 1 or a similar problem. Also, theirs places where your pass it the right number of variable but of the wrong type" ? I am new to C++, so any help would be greatly appreciated.
I fixed a few errors and these are the messages I got:
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C:\Users\sabin_000\Desktop\Lab 6_Shopping.cpp||In function 'void getChoice(std::string&, float&, int)':|
C:\Users\sabin_000\Desktop\Lab 6_Shopping.cpp|90|error: invalid initialization of reference of type 'float&' from expression of type 'int'|
C:\Users\sabin_000\Desktop\Lab 6_Shopping.cpp|39|note: in passing argument 2 of 'bool canAfford(std::string&, float&)'|
C:\Users\sabin_000\Desktop\Lab 6_Shopping.cpp|94|error: return-statement with a value, in function returning 'void' [-fpermissive]|
C:\Users\sabin_000\Desktop\Lab 6_Shopping.cpp|121|error: assignment of function 'bool shopMore()'|
C:\Users\sabin_000\Desktop\Lab 6_Shopping.cpp|121|error: cannot convert 'bool' to 'bool()' in assignment|
C:\Users\sabin_000\Desktop\Lab 6_Shopping.cpp|123|error: too few arguments to function 'void displayItems(std::string&, float)'|
C:\Users\sabin_000\Desktop\Lab 6_Shopping.cpp|52|note: declared here|
C:\Users\sabin_000\Desktop\Lab 6_Shopping.cpp|125|error: assignment of function 'bool shopMore()'|
C:\Users\sabin_000\Desktop\Lab 6_Shopping.cpp|125|error: cannot convert 'bool' to 'bool()' in assignment|
||=== Build failed: 7 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|

here is my revised code:
#include <iostream>
#include <string>
using namespace std;

void displayItems(string& name1, float budget);
float getName(string name, float budget);
bool canAfford(string& name1, float& budget1);
void getChoice(string& name1, float& budget1, int choice);
bool shopMore();

const float GIFTCARD = 50.00;
const float COLOGNE = 24.00;
const float KEYCHAIN = 14.00;
const float CARD = 4.00;

int main()
{
string userName;
float money;
int choice;

getName(userName, money);
displayItems(userName, money);
getChoice(userName, money, choice);
return 0;
}

float getName(string name, float budget)
{
cout << "Welcome! Who do I have the pleasure of assisting today?" << endl;
cin >> name;
cout << "Excellent " << name << ", and how much money do you have to spend today?" << endl;
cin >> budget;
}

bool canAfford(string& name1, float& budget1)
{
bool afford = false;
if (budget1 >= CARD)
{
afford = true;
cout << "Well " << name1 << " that means you can afford: " << endl;
}
else
cout << "Come back after earning more money." << endl;
return afford;
}

void displayItems(string& name, float budget)
{
canAfford(name, budget);

if (budget >= (GIFTCARD + COLOGNE + KEYCHAIN + CARD))
{
cout << "1. A gift card to Home Depot: $50.00" << endl;
cout << "2. A bottle of cologne: $24.00" << endl;
cout << "3. A keychain with a bathtub ornament: $14.00" << endl;
cout << "4. A boring old card: $4.00" << endl;
}
else if (budget >= (COLOGNE + KEYCHAIN + CARD))
{
cout << "2. A bottle of cologne: $24.00" << endl;
cout << "3. A keychain with a bathtub ornament: $14.00" << endl;
cout << "4. A boring old card: $4.00" << endl;
}
else if (budget >= (KEYCHAIN + CARD))
{
cout << "3. A keychain with a bathtub ornament: $14.00" << endl;
cout << "4. A boring old card: $4.00" << endl;
}
else if (budget >= CARD)
{
cout << "4. A boring old card: $4.00" << endl;
}
else
cout << "You need to earn some money first to afford anything here." << endl;

}


void getChoice(string& name, float& budget, int choice)
{
float remaining;

cout << "Enter your choice of what you would like to purchase (0 to cancel): " << endl;
cin >> choice;
canAfford(name, choice);
if (choice == 0)
{
cout << "Goodbye" << endl;
return 0;
}
else if (choice == 1)
{
remaining = budget - GIFTCARD;
cout << "Thank you " << name << endl;
cout << "You spent $50.00, and now have $" << remaining << " left to spend." << endl;
}
else if (choice == 2)
{
remaining = budget - COLOGNE;
cout << "Thank you " << name << endl;
cout << "You spent $24.00, and now have $" << remaining << " left to spend." << endl;
}
else if (choice == 3)
{
remaining = budget - KEYCHAIN;
cout << "Thank you " << name << endl;
cout << "You spent $14.00, and now have $" << remaining << " left to spend." << endl;
}
else if (choice == 4)
{
remaining = budget - CARD;
cout << "Thank you " << name << endl;
cout << "You spent $4.00, and now have $" << remaining << " left to spend." << endl;
}
shopMore();
if(shopMore = true)
{
displayItems();
}
else if (shopMore = false)
{
cout << "Thank you for shopping, come again!" << endl;
}


}
bool shopMore()
{
int choice;
do
{
cout << "Would you like to keep shopping? Enter 1 for yes, 2 for no." << endl;
cin >> choice;
if (choice == 1)
{
return true;
}
else if (choice == 2)
{
return false;
}
else
{
cout << "Invalid input, you must enter either 1 or 2" << endl;
}
} while(choice != 1 && choice != 2);

}
Thanks again!
Sorry, I meant you were passing it one variable but it it was looking for 2 variable to be passed but I think you fixed it. couple more things I see. In your getchoice function you have canAfford(name, choice); Your function shows that you want to pass a string and a float by reference. Yet choice is an int, not a float. You can convert between int and float but not when passing by reference. Also right below that you have return 0 but your get choice function is a void and doesn't return a value, this is an easy fix, just remove the zero so it's just return;

Further down in the function you have:
1
2
3
4
5
6
7
8
9
shopMore();
	if (shopMore = true)
	{
		displayItems();
	}
	else if (shopMore = false)
	{
		cout << "Thank you for shopping, come again!" << endl;
	}

Now, shopMore is a function so it needs (). Theirs 2 ways to do this shopMore returns a bool so either create a bool variable and set it equal to shopMore() and then use that bool in your if statements. (Also note when comparing things you use double equals "==")Or the way I would do it would be:
1
2
3
4
5
6
7
8
	if (shopMore())
	{
		displayItems();
	}
	else
	{
		cout << "Thank you for shopping, come again!" << endl;
	}


Lastly, displayItems(); has too few calls, it's supposed to be past 2 variables.

Make fixes then we can see where we're at, keep working at it!

Note: Please also use code tags around your code, it makes it easier to read. They are the <> on the right side of the text box.
Last edited on
I fixed the mistakes you pointed out, but I'm still not sure if my canAfford function is called correctly and whether it would work. This is due tomorrow, and I've spent hours on this, but I can't seem to do anything else. This is what I have now:



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
#include <iostream>
#include <string>
using namespace std;

void displayItems(string& name1, float budget);
float getName(string name, float budget);
bool canAfford(string& name1, float& budget1);
void getChoice(string& name1, float& budget1, float choice);
bool shopMore();

const float GIFTCARD = 50.00;
const float COLOGNE = 24.00;
const float KEYCHAIN = 14.00;
const float CARD = 4.00;

int main()
{
    string userName;
    float money;
    int choice;

    getName(userName, money);
    displayItems(userName, money);
    getChoice(userName, money, choice);
    return;
}

float getName(string name, float budget)
{
    cout << "Welcome! Who do I have the pleasure of assisting today?" << endl;
    cin >> name;
    cout << "Excellent " << name << ", and how much money do you have to spend today?" << endl;
    cin >> budget;
}

bool canAfford(string& name1, float& budget1)
{
    bool afford = false;
    if (budget1 >= CARD)
    {
        afford = true;
        cout << "Well " << name1 << " that means you can afford: " << endl;
    }
    else
        cout << "Come back after earning more money." << endl;
    return afford;
}

void displayItems(string& name, float budget)
{
    canAfford(name, budget);

    if (budget >= (GIFTCARD + COLOGNE + KEYCHAIN + CARD))
    {
        cout << "1. A gift card to Home Depot: $50.00" << endl;
        cout << "2. A bottle of cologne: $24.00" << endl;
        cout << "3. A keychain with a bathtub ornament: $14.00" << endl;
        cout << "4. A boring old card: $4.00" << endl;
    }
    else if (budget >= (COLOGNE + KEYCHAIN + CARD))
    {
        cout << "2. A bottle of cologne: $24.00" << endl;
        cout << "3. A keychain with a bathtub ornament: $14.00" << endl;
        cout << "4. A boring old card: $4.00" << endl;
    }
    else if (budget >= (KEYCHAIN + CARD))
    {
        cout << "3. A keychain with a bathtub ornament: $14.00" << endl;
        cout << "4. A boring old card: $4.00" << endl;
    }
    else if (budget >= CARD)
    {
        cout << "4. A boring old card: $4.00" << endl;
    }
    else
        cout << "You need to earn some money first to afford anything here." << endl;

}


void getChoice(string& name, float& budget, float choice)
{
    float remaining;

    cout << "Enter your choice of what you would like to purchase (0 to cancel): " << endl;
    cin >> choice;
    canAfford(name, choice);
    if (choice == 0)
    {
        cout << "Goodbye" << endl;
        return 0;
    }
    else if (choice == 1)
    {
        remaining = budget - GIFTCARD;
        cout << "Thank you " << name <<  endl;
        cout << "You spent $50.00, and now have $" << remaining << " left to spend." << endl;
    }
    else if (choice == 2)
    {
        remaining = budget - COLOGNE;
        cout << "Thank you " << name <<  endl;
        cout << "You spent $24.00, and now have $" << remaining << " left to spend." << endl;
    }
    else if (choice == 3)
    {
        remaining = budget - KEYCHAIN;
        cout << "Thank you " << name <<  endl;
        cout << "You spent $14.00, and now have $" << remaining << " left to spend." << endl;
    }
    else if (choice == 4)
    {
        remaining = budget - CARD;
        cout << "Thank you " << name <<  endl;
        cout << "You spent $4.00, and now have $" << remaining << " left to spend." << endl;
    }
    shopMore();
        if(shopMore())
        {
            displayItems();
        }
        else
        {
            cout << "Thank you for shopping, come again!" << endl;
        }

}
bool shopMore()
{
    int choice;
    do
    {
        cout << "Would you like to keep shopping? Enter 1 for yes, 2 for no." << endl;
        cin >> choice;
        if (choice == 1)
        {
            return true;
        }
        else if (choice == 2)
        {
            return false;
        }
        else
        {
            cout << "Invalid input, you must enter either 1 or 2" << endl;
        }
    } while(choice != 1 && choice != 2);

}


All help is appreciated!
Here is your code at least running now try testing, entering different values and see where your logic works and doesn't work. From my brief run of your code you are not to far off.
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
#include <iostream>
#include <string>
using namespace std;

void displayItems(string& name1, float budget);
void getName(string &name, float &budget);
bool canAfford(string& name1, float& budget1);
void getChoice(string& name1, float& budget1, float choice);
bool shopMore();

const float GIFTCARD = 50.00;
const float COLOGNE = 24.00;
const float KEYCHAIN = 14.00;
const float CARD = 4.00;

int main()
{
	string userName;
	float money;
	int choice = 1;//needs unitilized

	getName(userName, money);
	displayItems(userName, money);
	getChoice(userName, money, choice);
	return 0;//needs to return a value
}

void getName(string &name, float &budget)
{
	cout << "Welcome! Who do I have the pleasure of assisting today?" << endl;
	cin >> name;
	cout << "Excellent " << name << ", and how much money do you have to spend today?" << endl;
	cin >> budget;
	//needs to return somthing, but im guessing you will want both name and budget so you should pass them by reference and make the function a void
}

bool canAfford(string& name1, float& budget1)
{
	bool afford = false;
	if (budget1 >= CARD)
	{
		afford = true;
		cout << "Well " << name1 << " that means you can afford: " << endl;
	}
	else
		cout << "Come back after earning more money." << endl;
	return afford;
}

void displayItems(string& name, float budget)
{
	canAfford(name, budget);

	if (budget >= (GIFTCARD + COLOGNE + KEYCHAIN + CARD))
	{
		cout << "1. A gift card to Home Depot: $50.00" << endl;
		cout << "2. A bottle of cologne: $24.00" << endl;
		cout << "3. A keychain with a bathtub ornament: $14.00" << endl;
		cout << "4. A boring old card: $4.00" << endl;
	}
	else if (budget >= (COLOGNE + KEYCHAIN + CARD))
	{
		cout << "2. A bottle of cologne: $24.00" << endl;
		cout << "3. A keychain with a bathtub ornament: $14.00" << endl;
		cout << "4. A boring old card: $4.00" << endl;
	}
	else if (budget >= (KEYCHAIN + CARD))
	{
		cout << "3. A keychain with a bathtub ornament: $14.00" << endl;
		cout << "4. A boring old card: $4.00" << endl;
	}
	else if (budget >= CARD)
	{
		cout << "4. A boring old card: $4.00" << endl;
	}
	else
		cout << "You need to earn some money first to afford anything here." << endl;

}


void getChoice(string& name, float& budget, float choice)
{
	float remaining;

	cout << "Enter your choice of what you would like to purchase (0 to cancel): " << endl;
	cin >> choice;
	canAfford(name, choice);
	if (choice == 0)
	{
		cout << "Goodbye" << endl;
		return; // does not return 0
	}
	else if (choice == 1)
	{
		remaining = budget - GIFTCARD;
		cout << "Thank you " << name << endl;
		cout << "You spent $50.00, and now have $" << remaining << " left to spend." << endl;
	}
	else if (choice == 2)
	{
		remaining = budget - COLOGNE;
		cout << "Thank you " << name << endl;
		cout << "You spent $24.00, and now have $" << remaining << " left to spend." << endl;
	}
	else if (choice == 3)
	{
		remaining = budget - KEYCHAIN;
		cout << "Thank you " << name << endl;
		cout << "You spent $14.00, and now have $" << remaining << " left to spend." << endl;
	}
	else if (choice == 4)
	{
		remaining = budget - CARD;
		cout << "Thank you " << name << endl;
		cout << "You spent $4.00, and now have $" << remaining << " left to spend." << endl;
	}

	//shopMore(); // you dont need this
	if (shopMore())
	{
		//displayItems(); // needs to pass 2 variables
		cout << "Display items should be called" << endl;
	}
	else
	{
		cout << "Thank you for shopping, come again!" << endl;
	}

}
bool shopMore()
{
	int choice;
	do
	{
		cout << "Would you like to keep shopping? Enter 1 for yes, 2 for no." << endl;
		cin >> choice;
		if (choice == 1)
		{
			return true;
		}
		else if (choice == 2)
		{
			return false;
		}
		else
		{
			cout << "Invalid input, you must enter either 1 or 2" << endl;
		}
	} while (choice != 1 && choice != 2);

}
Last edited on
It runs, but when canAfford is called, it outputs "Please earn more money" even though the budget was sufficient. I tried changing my code to :
1
2
3
4
5
6
7
8
9
10
11
12
13
bool canAfford(string& name1, float& budget1)
{
	bool afford = false;
	if (budget1 >= CARD || budget1 >= KEYCHAIN || budget1 >= GIFTCARD || budget1 >= COLOGNE)
	{
		afford = true;
		cout << "Well " << name1 << " that means you can afford: " << endl;
		displayItems(name1, budget1);
	}
	else
		cout << "Come back after earning more money." << endl;
	return afford;
}


This still doesn't work. The code runs and prints this:
"Enter your choice of what you would like to purchase:
2
Come back after earning more money.
Thank you, name
You have spent 24.00, and now have 22.00 left to spend"

I'm really confused as to why its printing this, and I'm sorry if I can't seem to get it. Thank you so much for your help!
closed account (48T7M4Gy)
if (budget1 >= CARD || budget1 >= KEYCHAIN || budget1 >= GIFTCARD || budget1 >= COLOGNE)

I haven't looked desperately closely at your code but shouldn't that line be:

if (budget1 >= CARD && budget1 >= KEYCHAIN && budget1 >= GIFTCARD && budget1 >= COLOGNE)

It would be intuitively much better and clearer if you just had
if (budget1 >= cost_of_item)

You could have:
bool canAfford(string& name1, float& budget1, float cost_of_item){

and call this in main() with canAfford(someName, someBudget, KEYCHAIN); etc ...
Last edited on
kemort,
I changed it to your suggestion, and it still outputs the same thing. I just don't understand why.
closed account (48T7M4Gy)
A small point too. double's are more useable/practical/wider-ranging than float's.
What do you mean when you say to call canAfford in main with canAfford(someName, someBudget, KEYCHAIN); ?
closed account (48T7M4Gy)
Well, the idea is to eliminate the complicated code and just compare the cost of the item against what you have in your budget.

So the function just sends the persons name and their buget AND the cost of the item to the function canAfford.

Can afford sends back the bool value (as you already require) for processing in main. There is no special significance in calling it in main, just call it in the lines you already have and use the bool result accordingly.

The significant change to canAfford is my suggestion saves the trauma of complex condition statement. You either have enough money for the (known) cost of the item or you don't! :)
I still don't understand, can you run the code and see why it does not run correctly? I have run out of all options and don't know what else to do, this is due tomorrow. My code is the same as before, but when i press 1 to continue shopping, the program just displays the list and doesnt run get choice or anything else. this is what i have now:
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
#include <iostream>
#include <string>
using namespace std;

void displayItems(string& name1, float budget);
void getName(string &name, float &budget);
bool canAfford(string& name1, float& budget1);
void getChoice(string& name1, float& budget1, float choice);
bool shopMore();

const float GIFTCARD = 50.00;
const float COLOGNE = 24.00;
const float KEYCHAIN = 14.00;
const float CARD = 4.00;

int main()
{
	string userName;
	float money;
	int choice = 1;//needs unitilized

	getName(userName, money);
	displayItems(userName, money);
	getChoice(userName, money, choice);
	return 0;//needs to return a value
}

void getName(string &name, float &budget)
{
	cout << "Welcome! Who do I have the pleasure of assisting today?" << endl;
	cin >> name;
	cout << "Excellent " << name << ", and how much money do you have to spend today?" << endl;
	cin >> budget;
	//needs to return somthing, but im guessing you will want both name and budget so you should pass them by reference and make the function a void
}

bool canAfford(string& name1, float& budget1, float& cost_of_item)
{
	bool afford = false;
	if (budget1 >= CARD && budget1 >= KEYCHAIN && budget1 >= GIFTCARD && budget1 >= COLOGNE)
	{
		afford = true;
		cout << "Well " << name1 << " that means you can afford: " << endl;
	}
	else
		cout << "Come back after earning more money." << endl;
	return afford;
}

void displayItems(string& name, float budget)
{
    canAfford(name, budget);
	if (budget >= (GIFTCARD + COLOGNE + KEYCHAIN + CARD))
	{
		cout << "1. A gift card to Home Depot: $50.00" << endl;
		cout << "2. A bottle of cologne: $24.00" << endl;
		cout << "3. A keychain with a bathtub ornament: $14.00" << endl;
		cout << "4. A boring old card: $4.00" << endl;
	}
	else if (budget >= (COLOGNE + KEYCHAIN + CARD))
	{
		cout << "2. A bottle of cologne: $24.00" << endl;
		cout << "3. A keychain with a bathtub ornament: $14.00" << endl;
		cout << "4. A boring old card: $4.00" << endl;
	}
	else if (budget >= (KEYCHAIN + CARD))
	{
		cout << "3. A keychain with a bathtub ornament: $14.00" << endl;
		cout << "4. A boring old card: $4.00" << endl;
	}
	else if (budget >= CARD)
	{
		cout << "4. A boring old card: $4.00" << endl;
	}
	else
		cout << "You need to earn some money first to afford anything here." << endl;

}

void getChoice(string& name, float& budget, float choice)
{
	float remaining;

	cout << "Enter your choice of what you would like to purchase (0 to cancel): " << endl;
	cin >> choice;

	if (choice == 0)
	{
		cout << "Goodbye" << endl;
		return; // does not return 0
	}
	else if (choice == 1)
	{
	    canAfford(name, choice);
		remaining = budget - GIFTCARD;
		cout << "Thank you " << name << endl;
		cout << "You spent $50.00, and now have $" << remaining << " left to spend." << endl;
	}
	else if (choice == 2)
	{
	    canAfford(name, choice);
		remaining = budget - COLOGNE;
		cout << "Thank you " << name << endl;
		cout << "You spent $24.00, and now have $" << remaining << " left to spend." << endl;
	}
	else if (choice == 3)
	{
	    canAfford(name, choice);
		remaining = budget - KEYCHAIN;
		cout << "Thank you " << name << endl;
		cout << "You spent $14.00, and now have $" << remaining << " left to spend." << endl;
	}
	else if (choice == 4)
	{
	    canAfford(name, choice);
		remaining = budget - CARD;
		cout << "Thank you " << name << endl;
		cout << "You spent $4.00, and now have $" << remaining << " left to spend." << endl;
	}

	//shopMore(); // you dont need this
	if (shopMore())
	{
		displayItems(name, budget);
		getChoice(name, budget, choice);//displayItems(); // needs to pass 2 variables
	}
	else
	{
		cout << "Thank you for shopping, come again!" << endl;
	}

}
bool shopMore()
{
	int choice;
	do
	{
		cout << "Would you like to keep shopping? Enter 1 for yes, 2 for no." << endl;
		cin >> choice;
		if (choice == 1)
		{
			return true;
		}
		else if (choice == 2)
		{
			return false;
		}
		else
		{
			cout << "Invalid input, you must enter either 1 or 2" << endl;
		}
	} while (choice != 1 && choice != 2);

}

Thank you for your help, I really appreciate it!
I finally fixed all the errors, but now when I try to make sure the user does not enter an option other than the ones displayed in the menu, I get an infinite loop. This is my code now:
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <iostream>
#include <string>
using namespace std;

void displayItems(string& name1, float budget);
void getName(string &name, float &budget);
bool canAfford(string& name1, float budget1);
void getChoice(string& name1, float budget1, float choice);
bool shopMore();

const float GIFTCARD = 50.00;
const float COLOGNE = 24.00;
const float KEYCHAIN = 14.00;
const float CARD = 4.00;

int main()
{
	string userName;
	float money;
	int choice = 1;

	getName(userName, money);
	displayItems(userName, money);
	getChoice(userName, money, choice);
	return 0;
}

void getName(string &name, float &budget)
{
	cout << "Welcome! Who do I have the pleasure of assisting today?" << endl;
	cin >> name;
	cout << "Excellent " << name << ", and how much money do you have to spend today?" << endl;
	cin >> budget;
        if (budget <= 0.00)
        {
            cout << "Come back after earning come money." << endl;
            return;
        }
}

bool canAfford(string& name1, float budget1)
{
	bool afford;
	if (budget1 >= CARD)
	{
		afford = true;

	}
	else if (budget1 < CARD)
    {
        afford = false;

    }
	return afford;
}

void displayItems(string& name, float budget)
{
    canAfford(name, budget);
        if (canAfford(name, budget) == true)
        {
            cout << "Well " << name << " that means you can afford: " << endl;
        }
        else if(canAfford(name, budget) == false)
        {
            cout << "Come back after earning more money." << endl;
            return;
        }
	if (budget >= GIFTCARD)
	{
		cout << "1. A gift card to Home Depot: $50.00" << endl;
		cout << "2. A bottle of cologne: $24.00" << endl;
		cout << "3. A keychain with a bathtub ornament: $14.00" << endl;
		cout << "4. A boring old card: $4.00" << endl;
	}
	else if (budget >= COLOGNE && budget <= GIFTCARD)
	{
		cout << "2. A bottle of cologne: $24.00" << endl;
		cout << "3. A keychain with a bathtub ornament: $14.00" << endl;
		cout << "4. A boring old card: $4.00" << endl;
	}
	else if (budget >= KEYCHAIN && budget <= COLOGNE)
	{
		cout << "3. A keychain with a bathtub ornament: $14.00" << endl;
		cout << "4. A boring old card: $4.00" << endl;
	}
	else if (budget >= CARD && budget <= KEYCHAIN)
	{
		cout << "4. A boring old card: $4.00" << endl;
	}
	else
    {
        cout << "You need to earn some money first to afford anything here." << endl;
    }

}

void getChoice(string& name, float budget, float choice)
{
	float remaining;
    if (choice == 1)
	{
	    choice == GIFTCARD;
	}
	else if (choice == 2)
	{
	    choice == COLOGNE;
	}
	else if (choice == 3)
	{
	    choice == KEYCHAIN;
	}
	else if (choice == 4)
	{
	    choice == CARD;
	}

	cout << "Enter your choice of what you would like to purchase (0 to cancel): " << endl;
    cin >> choice;

	while (choice < budget)
    {
        cout << "Enter your choice of what you would like to purchase (0 to cancel): " << endl;
        cin >> choice;
        if (choice < budget)
            {
                cout << "You must choose an option from the menu" << endl;
            }
     }

	if (choice == 0)
	{
		cout << "Goodbye" << endl;
		return;
	}
	else if (choice == 1)
	{
	    choice == GIFTCARD;
	    canAfford(name, choice);
	    if(canAfford(name, budget) == false)
        {
            cout << "Come back after earning more money." << endl;
            return;
        }
		remaining = budget - GIFTCARD;
		cout << "Thank you " << name << endl;
		cout << "You spent $50.00, and now have $" << remaining << " left to spend." << endl;
		budget = remaining;
		if (budget == 0)
        {
            cout << "Goodbye!" << endl;
            return;
        }
	}
	else if (choice == 2)
	{
	    choice == COLOGNE;
	    canAfford(name, choice);
	    if(canAfford(name, budget) == false)
        {
            cout << "Come back after earning more money." << endl;
            return;
        }
		remaining = budget - COLOGNE;
		cout << "Thank you " << name << endl;
		cout << "You spent $24.00, and now have $" << remaining << " left to spend." << endl;
		budget = remaining;
		if (budget == 0)
        {
            cout << "Goodbye!" << endl;
            return;
        }
	}
	else if (choice == 3)
	{
	    choice == KEYCHAIN;
	    canAfford(name, choice);
	    if(canAfford(name, budget) == false)
        {
            cout << "Come back after earning more money." << endl;
            return;
        }
		remaining = budget - KEYCHAIN;
		cout << "Thank you " << name << endl;
		cout << "You spent $14.00, and now have $" << remaining << " left to spend." << endl;
		budget = remaining;
		if (budget == 0)
        {
            cout << "Goodbye!" << endl;
            return;
        }
	}
	else if (choice == 4)
	{
	    choice == CARD;
	    canAfford(name, choice);
	    if(canAfford(name, budget) == false)
        {
            cout << "Come back after earning more money." << endl;
            return;
        }
		remaining = budget - CARD;
		cout << "Thank you " << name << endl;
		cout << "You spent $4.00, and now have $" << remaining << " left to spend." << endl;
		budget = remaining;
		if (budget == 0)
        {
            cout << "Goodbye!" << endl;
            return;
        }

	}
	if (budget < CARD)
    {
        cout << "Come back after earning more money. Goodbye!" << endl;
        return;
    }

	if (shopMore())
	{
		displayItems(name, budget);
		getChoice(name, budget, choice);
	}
	else
	{
		cout << "Thank you for shopping, come again!" << endl;
	}

}
bool shopMore()
{
	int choice;
	do
	{
		cout << "Would you like to keep shopping? Enter 1 for yes, 2 for no." << endl;
		cin >> choice;
		if (choice == 1)
		{
			return true;
		}
		else if (choice == 2)
		{
			return false;
		}
		else
		{
			cout << "Invalid input, you must enter either 1 or 2" << endl;
		}
	} while (choice != 1 && choice != 2);

}

How can I make sure the user does not enter any option other than the ones listed?
Thank you!
closed account (48T7M4Gy)
There are a couple of ways you can limit the options to only valid ones. It depends how advanced you are in your studies but they all do the same.

a) if ... else if ... else as you are obviously familiar with.
b) while or do ... while loop as you as you are obviously familiar with.
c) switch control with a default branch see http://www.cplusplus.com/doc/tutorial/control/

I don't know whether you are familiar with arrays but you've probably run out of time by the sound of it. Arrays allow you to remove the duplication in your code but it would take an hour or two to go through and rebuild what you have.

You can get yours to run so I'll leave it at that. Good luck :)
I used the while loop, but it just runs infinitely. How do I fix that?
If you would kindly show me what to do I would very much appreciate it, since I am running out of ideas.
Last edited on
closed account (48T7M4Gy)
You will have to work out the end condition so that the while condition is no longer true. I have to be away for a couple of hours otherwis I'm happy to help if nobody else jumps in.

pseudocode:

while( condition is true (ie until the right answer is inpt) ){
    do stuff
}

arrives here because condition returned false, maybe quit entered
closed account (48T7M4Gy)
This is a sample pattern to do what you are looking for:

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
#include <iostream>

void menu()
{
    std::cout << "1. Case 1\n";
    std::cout << "2. Case 2\n";
    std::cout << "q to quit\n";
}

void case_number_one(int aNumber){
    std::cout << "*** Double the number is: " << 2*aNumber << '\n';
}

void case_number_two(int aNumber){
    std::cout << "*** Triple the number is: " << 3*aNumber << '\n';
}

int main()
{
    int some_number = 31;
    
    char selection = '0';
    
    menu();
    while (std::cin >> selection && tolower(selection) != 'q')
    {
        switch (selection)
        {
            case '1':
                std::cout << "Case 1\n";
                case_number_one(some_number);
                break;
                
            case '2':
                std::cout << "Case 2\n";
                case_number_two(some_number);
                break;
                
            default:
                std::cout << "Invalid choice\n";
                break;
        }
        menu();
    }
    
    std::cout << "Closing ...\n";
    
    return 0;
}
Topic archived. No new replies allowed.