| | |
A bit of a problem with Arrays
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
I am a bit confused. I am trying to make a program that will take the choices you make and display them all simular to this:
Item 1. Sword
Item 2. Shield
Item 3. Potion
Item 4. Potion
but when I try to run it it looks more like this
first
Item 1. Sword
then
Item 1. Shield
Item 2. Shield
then
Item 1. Potion
Item 2. Potion
Item 3. Potion
and so on and so forth. I am unsure of what i am doing wrong.
please help if you can.
Item 1. Sword
Item 2. Shield
Item 3. Potion
Item 4. Potion
but when I try to run it it looks more like this
first
Item 1. Sword
then
Item 1. Shield
Item 2. Shield
then
Item 1. Potion
Item 2. Potion
Item 3. Potion
and so on and so forth. I am unsure of what i am doing wrong.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; const int NumItems = 7; const int StringSize = 8; int choice = 0, Shown=0, count = 0, inList=0; int main() { int YesNo = 1; double count = 0; cout << "Welcome to the inn, before you set out on your adventure, \nyou'd best " << "gather your equipment. \nWould you like to pack something?\n 1. Yes\n 2. No\n"; cin >> YesNo; while (YesNo != 2) { count++; cout << "Ok what would you like to pack?\n"; cout << " 1. Sword \n 2. Shield \n 3. Helmet \n 4. Armor \n 5. Potion \n 6. Ether \n 7. Elixir\n"; cin >> choice; char Items[NumItems][StringSize] = { "Sword", "Shield", "Helmet", "Armor", "Potion", "Ether", "Elixir"}; if ((choice != 1) && (choice != 2) && (choice != 3) && (choice != 4) && (choice != 5) && (choice != 6) && (choice != 7)) { cout << "I'm sorry, what was that? "; cin >> choice; } cout << "alright, i'll put one in your pack. \n" << "Your pack contains the following: \n"; for (int index = 0; index < count; index++) { cout << "item " << (index + 1) << ". " << Items[choice-1] << endl; } cout << "Would you like to pack anything else?\n"; cin >> YesNo; } system ("pause"); return 0; }
please help if you can.
Now... FEAR THE BUNNY!!!
|\\ //
|(0.0)
|(> <)
||d b
|\\ //
|(0.0)
|(> <)
||d b
•
•
Join Date: Nov 2008
Posts: 29
Reputation:
Solved Threads: 1
You aren't storing the items anywhere?
All you are doing is increasing the number of items bought each time. And printing the latest item off.
If you buy item 5, 6 and 1. You need to store this somewhere, maybe in another array? Then print out this array. Not the itemlist array.
Currently this is what you are doing:
Add Item? - Yes
Increase numItems
Which Item? - x
FOR LOOP - FOR numItems, print out Item x
What you want is:
Add Item? - Yes
Which Item? - Item = 2
Bag[numItems] = Item
numItems++
for(i = 0; i < numItems; i++) .... print Bag[i]
All you are doing is increasing the number of items bought each time. And printing the latest item off.
If you buy item 5, 6 and 1. You need to store this somewhere, maybe in another array? Then print out this array. Not the itemlist array.
Currently this is what you are doing:
Add Item? - Yes
Increase numItems
Which Item? - x
FOR LOOP - FOR numItems, print out Item x
What you want is:
Add Item? - Yes
Which Item? - Item = 2
Bag[numItems] = Item
numItems++
for(i = 0; i < numItems; i++) .... print Bag[i]
Last edited by Lokolo; Nov 18th, 2008 at 11:12 am.
Ok, i see what you mean. but it doent seem to be working, maybe i am not writing it correctly. here's what i'm trying:
C++ Syntax (Toggle Plain Text)
char Items[NumItems][StringSize] = { "Sword", "Shield", "Helmet", "Armor", "Potion", "Ether", "Elixir"}; Bag[inList]=Items; inList++; if ((choice != 1) && (choice != 2) && (choice != 3) && (choice != 4) && (choice != 5) && (choice != 6) && (choice != 7)) { cout << "I'm sorry, what was that? "; cin >> choice; } cout << "alright, i'll put one in your pack. \n" << "Your pack contains the following: \n"; for (int index = 0; index < inList; index++) { cout << "item " << (index + 1) << ". " << Bag[index] << endl; } cout << "Would you like to pack anything else?\n"; cin >> YesNo; }
Now... FEAR THE BUNNY!!!
|\\ //
|(0.0)
|(> <)
||d b
|\\ //
|(0.0)
|(> <)
||d b
•
•
Join Date: Nov 2008
Posts: 29
Reputation:
Solved Threads: 1
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; const int NumItems = 7; int numOfUserItems = 0; char userItems[10]; //max of 10 items per user const int StringSize = 8; int choice = 0, Shown=0, count = 0, inList=0; int main() { int YesNo = 1; double count = 0; cout << "Welcome to the inn, before you set out on your adventure, \nyou'd best " << "gather your equipment. \nWould you like to pack something?\n 1. Yes\n 2. No\n"; cin >> YesNo; while (YesNo != 2) { cout << "Ok what would you like to pack?\n"; cout << " 1. Sword \n 2. Shield \n 3. Helmet \n 4. Armor \n 5. Potion \n 6. Ether \n 7. Elixir\n"; cin >> choice; char Items[NumItems][StringSize] = { "Sword", "Shield", "Helmet", "Armor", "Potion", "Ether", "Elixir"}; if ((choice != 1) && (choice != 2) && (choice != 3) && (choice != 4) && (choice != 5) && (choice != 6) && (choice != 7)) { cout << "I'm sorry, what was that? "; cin >> choice; } else { userItems[numOfUserItems] = choice; cout << "alright, i'll put one in your pack. \n" } << "Your pack contains the following: \n"; for (int index = 0; index < numOfUserItems; index++) { cout << "item " << (index + 1) << ". " << Items[userItems[index]] << endl; } cout << "Would you like to pack anything else?\n"; cin >> YesNo; } system ("pause"); return 0; }
Try it. I am eating dinner in a min so cba
•
•
Join Date: Nov 2008
Posts: 29
Reputation:
Solved Threads: 1
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; const int NumItems = 7; int numOfUserItems = 0; char userItems[10]; //max of 10 items per user const int StringSize = 8; int choice = 0, Shown=0, count = 0, inList=0; int main() { int YesNo = 1; double count = 0; cout << "Welcome to the inn, before you set out on your adventure, \nyou'd best " << "gather your equipment. \nWould you like to pack something?\n 1. Yes\n 2. No\n"; cin >> YesNo; while (YesNo != 2) { cout << "Ok what would you like to pack?\n"; cout << " 1. Sword \n 2. Shield \n 3. Helmet \n 4. Armor \n 5. Potion \n 6. Ether \n 7. Elixir\n"; cin >> choice; char Items[NumItems][StringSize] = { "Sword", "Shield", "Helmet", "Armor", "Potion", "Ether", "Elixir"}; if ((choice != 1) && (choice != 2) && (choice != 3) && (choice != 4) && (choice != 5) && (choice != 6) && (choice != 7)) { cout << "I'm sorry, what was that? "; cin >> choice; } else { userItems[numOfUserItems] = (choice-1); cout << "alright, i'll put one in your pack. \n"; } cout << "Your pack contains the following: \n"; numOfUserItems++; for (int index = 0; index < numOfUserItems; index++) { cout << "item " << (index + 1) << ". " << Items[userItems[index]] << endl; } cout << "Would you like to pack anything else?\n"; cin >> YesNo; } system ("pause"); return 0; }
Done.
However you should look over this, I'm new with C++ but fixed it in 2min. I was missing a brace or 2 and a variable++.
![]() |
Similar Threads
- Weird Binary to Integer Problem (Java)
- Returning arrays (Pascal and Delphi)
- Newbie Problem: Arrays (C++)
- Problem with Character Arrays (C++)
- Problem with displaying data.. (PHP)
- problem printing output (C++)
- Multiple inputs for char / arrays of arrays (C++)
- I would kill for this, just a little bit (C)
Other Threads in the C++ Forum
- Previous Thread: Problem with memory allocation
- Next Thread: C++ homework...Please help!!
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets





