Values of a loop displaying twice?
I seem to be having an issue with the values of a loop appearing twice.
Here is my code, I am currently coding an inventory program that utilizes vectors.
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
|
#include<iostream>
#include<vector>
using namespace std;
double goldAmount = 25;
int selection;
vector<string>inventory;
void sword();
void potionHeal();
void shield();
void showInventory();
void sword(){
string name = "\nSword";
if (goldAmount < 5){
cout<<"\nYou Do Not Have Enough Gold!"<<endl;
return;
}
inventory.push_back(name);
goldAmount = goldAmount - 5;
cout<<"\nYou Purchased A Sword."<<endl;
}
void potionHeal(){
string name = "\nHealing Potion";
if (goldAmount < 2){
cout<<"\nYou Do Not Have Enough Gold!"<<endl;
return;
}
inventory.push_back(name);
goldAmount = goldAmount - 2;
cout<<"\nYou Purchased A Healing Potion."<<endl;
}
void shield(){
string name = "\nShield";
if (goldAmount < 5){
cout<<"\nYou Do Not Have Enough Gold!"<<endl;
return;
}
inventory.push_back(name);
goldAmount = goldAmount - 5;
cout<<"\nYou Purchased A Shield."<<endl;
}
void showInventory(){
for (int i = 0; i<inventory.size(); i++){
cout<<inventory[i]<<"\n";
}
}
int main(){
do{
cout<<"\nPurchase Items for your Inventory"<<endl;
cout<<"***********************************"<<endl;
cout<<"1. Sword - 5g"<<endl;
cout<<"2. Potion of Healing - 2g"<<endl;
cout<<"3. Shield - 5g"<<endl;
cout<<"4. Show Inventory"<<endl;
cout<<"5. Exit"<<endl;
cout<<"You Have "<<goldAmount<<" gold"<<endl;
cout<<"Please Make a Purchase: ";
cin>>selection;
switch(selection){
case 1: sword();
break;
case 2: potionHeal();
break;
case 3: shield();
break;
case 4: showInventory();
break;
}
for (int i = 0; i<inventory.size(); i++){
if(goldAmount == 0){
cout<<inventory[i]<<"\n";
}
}
}while(selection != 5);
}
|
This segment of code is where I think the problem is.
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
|
void showInventory(){
for (int i = 0; i<inventory.size(); i++){
cout<<inventory[i]<<"\n";
}
}
int main(){
do{
cout<<"\nPurchase Items for your Inventory"<<endl;
cout<<"***********************************"<<endl;
cout<<"1. Sword - 5g"<<endl;
cout<<"2. Potion of Healing - 2g"<<endl;
cout<<"3. Shield - 5g"<<endl;
cout<<"4. Show Inventory"<<endl;
cout<<"5. Exit"<<endl;
cout<<"You Have "<<goldAmount<<" gold"<<endl;
cout<<"Please Make a Purchase: ";
cin>>selection;
switch(selection){
case 1: sword();
break;
case 2: potionHeal();
break;
case 3: shield();
break;
case 4: showInventory();
break;
}
for (int i = 0; i<inventory.size(); i++){
if(goldAmount == 0){
cout<<inventory[i]<<"\n";
}
}
}while(selection != 5);
}
|
Basically once the amount of gold hits zero, the inventory items are displayed.
However, when I enter the Show Inventory option, the loop is displayed twice.
Example:
Once Gold Counter Hits Zero:
Sword
Sword
Sword
Sword
Sword
When Show Inventory is selected:
Sword
Sword
Sword
Sword
Sword
Sword
Sword
Sword
Sword
Sword
How can I fix this problem?
Something else that I have noticed is that when the program is exited, the loop is displayed again.
However, when I enter the Show Inventory option, the loop is displayed twice. |
Isn't it just that you print the inventory in showInventory() and then again in the loop after the switch in main?
It does not seem to be that way.
The loop on lines 94-97 are inside the do-while loop so the inventory will be displayed on every iteration of the do-while loop.
Topic archived. No new replies allowed.