C++ How can i output all the items in this program
Feb 19, 2015 at 3:01pm UTC
I am having trouble doing my homework assignment I am almost done but in that program I can not figure out how to output all of the users purchases. I dumb down my homework assignment where its still rotatable to the same problem I am having with the homework assignment.I also dont want them to think i just copied and pasted from the internet. I want my code display to display all his purchase like a receipt from a store . I tried the for loop and i like that
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
#include <iostream> // provides: cout, cin
#include <iomanip> // provides: setw
#include <limits>
// setting up the environment
using namespace std;
bool run = true ;
int Item;
double price;
int user_Item_Choice ;
string UsersYorN;
int count = 1;
bool True1 = true ;
bool True2 = true ;
void enterItemNum();
void readItems();
void usersYesOrNo();
int main()
{
while (run == true ){
while (True1 == true ){
enterItemNum();
}
while (True2 == true ){
usersYesOrNo();
}
}
system("pause" );
return 0;
}
void readItems(){
cout <<"Item 1: $4.00\n" ;
cout <<"Item 2: $5.50\n" ;
cout <<"Item 3: $6.50\n" ;
cout <<"Item 4: $7.00\n" ;
}
void enterItemNum()
{
readItems();
cout << "\nPlease choose the item number from the menu above: " ;
if (!(cin >> user_Item_Choice )){
cout<< "Error this is not a number please try again but this time enter number!\n\n" ;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n' );
enterItemNum();
True1 = true ;
}
else if (user_Item_Choice >= 1 && user_Item_Choice <= 4 )
{
switch (user_Item_Choice)
{
case 1: cout <<"Item 1: $4.00\n" ;
Item = 1;
price = 4.00;
break ;
case 2: cout <<"Item 2: $5.50\n" ;
Item =2;
price = 5.50;
break ;
case 3: cout <<"Item 3: $6.50\n" ;
Item =3;
price = 6.50;
break ;
case 4: cout <<"Item 4: $7.00\n" ;
Item =4;
price = 7.00;
break ;
defualt:
break ;
}
True1 = false ;
True2 =true ;
}
else
{
cout<<"Please enter a number 1 - 4 that represent the item that you wanted to buy.\n\n\n" ;
readItems();
True1 = true ;
}
}
void usersYesOrNo()
{
cout << "Would you like to buy more items? if yes enter [y] otherwise enter [n]: " ;
cin>> UsersYorN;
if (UsersYorN == "Y" || UsersYorN == "y" ||UsersYorN == "yes" || UsersYorN == "Yes" || UsersYorN == "YES" )
{
count++;
True1 = true ;
True2 = false ;
run = true ;
}
else if (UsersYorN == "N" ||UsersYorN == "n" ||UsersYorN == "no" || UsersYorN == "No" ||UsersYorN == "NO" )
{ True1 =false ;
True2 =false ;
run = false ;
// I want to display all the strings inputed. i know it a loop but i have no idea
// what loop to use
cout<< " Your purchased items \n" ;
for (int i = 1; i<=count ; i++){
cout << "Item#" <<Item << " $" <<price <<"\n" ;
}
}
else
{
cout << "****Error: Please enter Yes or No!\n\n" ;
True2 =true ;
}
}
Feb 19, 2015 at 3:49pm UTC
If you want to keep track of multiple items purchased, your're going to need an array (or vector).
Line 120: You're outputting the same item and price every time through the loop. This is where you need to get price and item from an array.
Feb 19, 2015 at 5:04pm UTC
can you show me an example on how i would do that
Feb 19, 2015 at 5:20pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <vector>
#include <string>
using namespace std;
struct Item
{ double price;
string descr;
};
vector<Item> items;
void add_item (double price, const string & descr)
{ Item item;
item.price = price;
item.descr = descr;
items.push_back (item);
}
void print_items ()
{ for (unsigned i=0; i<items.size(); i++)
cout << items[i].descr << items[i].price << endl;
}
Topic archived. No new replies allowed.