A bit of a problem with Arrays

Thread Solved
Reply

Join Date: May 2008
Posts: 23
Reputation: MJFiggs is an unknown quantity at this point 
Solved Threads: 0
MJFiggs's Avatar
MJFiggs MJFiggs is offline Offline
Newbie Poster

A bit of a problem with Arrays

 
0
  #1
Nov 18th, 2008
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.

  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5. const int NumItems = 7;
  6. const int StringSize = 8;
  7. int choice = 0,
  8. Shown=0,
  9. count = 0,
  10. inList=0;
  11.  
  12. int main()
  13. {
  14. int YesNo = 1;
  15. double count = 0;
  16. cout << "Welcome to the inn, before you set out on your adventure, \nyou'd best "
  17. << "gather your equipment. \nWould you like to pack something?\n 1. Yes\n 2. No\n";
  18. cin >> YesNo;
  19. while (YesNo != 2)
  20. {
  21. count++;
  22. cout << "Ok what would you like to pack?\n";
  23. cout << " 1. Sword \n 2. Shield \n 3. Helmet \n 4. Armor \n 5. Potion \n 6. Ether \n 7. Elixir\n";
  24. cin >> choice;
  25.  
  26. char Items[NumItems][StringSize] =
  27. { "Sword", "Shield", "Helmet", "Armor",
  28. "Potion", "Ether", "Elixir"};
  29.  
  30.  
  31. if ((choice != 1) && (choice != 2) && (choice != 3) &&
  32. (choice != 4) && (choice != 5) && (choice != 6) && (choice != 7))
  33. {
  34. cout << "I'm sorry, what was that? ";
  35. cin >> choice;
  36. }
  37. cout << "alright, i'll put one in your pack. \n"
  38. << "Your pack contains the following: \n";
  39. for (int index = 0; index < count; index++)
  40. {
  41. cout << "item " << (index + 1) << ". " << Items[choice-1] << endl;
  42.  
  43. }
  44. cout << "Would you like to pack anything else?\n";
  45. cin >> YesNo;
  46. }
  47.  
  48.  
  49. system ("pause");
  50. return 0;
  51. }

please help if you can.
Now... FEAR THE BUNNY!!!
|\\ //
|(0.0)
|(> <)
||d b
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 29
Reputation: Lokolo is an unknown quantity at this point 
Solved Threads: 1
Lokolo Lokolo is offline Offline
Light Poster

Re: A bit of a problem with Arrays

 
1
  #2
Nov 18th, 2008
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]
Last edited by Lokolo; Nov 18th, 2008 at 11:12 am.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 23
Reputation: MJFiggs is an unknown quantity at this point 
Solved Threads: 0
MJFiggs's Avatar
MJFiggs MJFiggs is offline Offline
Newbie Poster

Re: A bit of a problem with Arrays

 
0
  #3
Nov 18th, 2008
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:

  1. char Items[NumItems][StringSize] =
  2. { "Sword", "Shield", "Helmet", "Armor",
  3. "Potion", "Ether", "Elixir"};
  4.  
  5. Bag[inList]=Items;
  6. inList++;
  7. if ((choice != 1) && (choice != 2) && (choice != 3) &&
  8. (choice != 4) && (choice != 5) && (choice != 6) && (choice != 7))
  9. {
  10. cout << "I'm sorry, what was that? ";
  11. cin >> choice;
  12. }
  13. cout << "alright, i'll put one in your pack. \n"
  14. << "Your pack contains the following: \n";
  15. for (int index = 0; index < inList; index++)
  16. {
  17. cout << "item " << (index + 1) << ". " << Bag[index] << endl;
  18.  
  19. }
  20. cout << "Would you like to pack anything else?\n";
  21. cin >> YesNo;
  22. }
Now... FEAR THE BUNNY!!!
|\\ //
|(0.0)
|(> <)
||d b
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 29
Reputation: Lokolo is an unknown quantity at this point 
Solved Threads: 1
Lokolo Lokolo is offline Offline
Light Poster

Re: A bit of a problem with Arrays

 
0
  #4
Nov 18th, 2008
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5. const int NumItems = 7;
  6. int numOfUserItems = 0;
  7. char userItems[10]; //max of 10 items per user
  8. const int StringSize = 8;
  9. int choice = 0,
  10. Shown=0,
  11. count = 0,
  12. inList=0;
  13.  
  14. int main()
  15. {
  16. int YesNo = 1;
  17. double count = 0;
  18. cout << "Welcome to the inn, before you set out on your adventure, \nyou'd best "
  19. << "gather your equipment. \nWould you like to pack something?\n 1. Yes\n 2. No\n";
  20. cin >> YesNo;
  21. while (YesNo != 2)
  22. {
  23. cout << "Ok what would you like to pack?\n";
  24. cout << " 1. Sword \n 2. Shield \n 3. Helmet \n 4. Armor \n 5. Potion \n 6. Ether \n 7. Elixir\n";
  25. cin >> choice;
  26.  
  27. char Items[NumItems][StringSize] =
  28. { "Sword", "Shield", "Helmet", "Armor",
  29. "Potion", "Ether", "Elixir"};
  30.  
  31.  
  32. if ((choice != 1) && (choice != 2) && (choice != 3) &&
  33. (choice != 4) && (choice != 5) && (choice != 6) && (choice != 7))
  34. {
  35. cout << "I'm sorry, what was that? ";
  36. cin >> choice;
  37. } else {
  38. userItems[numOfUserItems] = choice;
  39. cout << "alright, i'll put one in your pack. \n"
  40. }
  41. << "Your pack contains the following: \n";
  42. for (int index = 0; index < numOfUserItems; index++)
  43. {
  44. cout << "item " << (index + 1) << ". " << Items[userItems[index]] << endl;
  45.  
  46. }
  47. cout << "Would you like to pack anything else?\n";
  48. cin >> YesNo;
  49. }
  50.  
  51.  
  52. system ("pause");
  53. return 0;
  54. }

Try it. I am eating dinner in a min so cba
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 23
Reputation: MJFiggs is an unknown quantity at this point 
Solved Threads: 0
MJFiggs's Avatar
MJFiggs MJFiggs is offline Offline
Newbie Poster

Re: A bit of a problem with Arrays

 
0
  #5
Nov 19th, 2008
Im sorry to say it, butwhen I pasted your program to my compiler, i had the same problem when i started. Have you any other thoughts? please.
Now... FEAR THE BUNNY!!!
|\\ //
|(0.0)
|(> <)
||d b
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 29
Reputation: Lokolo is an unknown quantity at this point 
Solved Threads: 1
Lokolo Lokolo is offline Offline
Light Poster

Re: A bit of a problem with Arrays

 
0
  #6
Nov 19th, 2008
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5. const int NumItems = 7;
  6. int numOfUserItems = 0;
  7. char userItems[10]; //max of 10 items per user
  8. const int StringSize = 8;
  9. int choice = 0, Shown=0, count = 0, inList=0;
  10.  
  11. int main()
  12. {
  13. int YesNo = 1;
  14. double count = 0;
  15. cout << "Welcome to the inn, before you set out on your adventure, \nyou'd best "
  16. << "gather your equipment. \nWould you like to pack something?\n 1. Yes\n 2. No\n";
  17. cin >> YesNo;
  18. while (YesNo != 2)
  19. {
  20. cout << "Ok what would you like to pack?\n";
  21. cout << " 1. Sword \n 2. Shield \n 3. Helmet \n 4. Armor \n 5. Potion \n 6. Ether \n 7. Elixir\n";
  22. cin >> choice;
  23.  
  24. char Items[NumItems][StringSize] =
  25. { "Sword", "Shield", "Helmet", "Armor",
  26. "Potion", "Ether", "Elixir"};
  27.  
  28.  
  29. if ((choice != 1) && (choice != 2) && (choice != 3) &&
  30. (choice != 4) && (choice != 5) && (choice != 6) && (choice != 7))
  31. {
  32. cout << "I'm sorry, what was that? ";
  33. cin >> choice;
  34. } else {
  35. userItems[numOfUserItems] = (choice-1);
  36. cout << "alright, i'll put one in your pack. \n";
  37. }
  38. cout << "Your pack contains the following: \n";
  39. numOfUserItems++;
  40. for (int index = 0; index < numOfUserItems; index++)
  41. {
  42. cout << "item " << (index + 1) << ". " << Items[userItems[index]] << endl;
  43. }
  44. cout << "Would you like to pack anything else?\n";
  45. cin >> YesNo;
  46. }
  47. system ("pause");
  48. return 0;
  49. }

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++.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 23
Reputation: MJFiggs is an unknown quantity at this point 
Solved Threads: 0
MJFiggs's Avatar
MJFiggs MJFiggs is offline Offline
Newbie Poster

Re: A bit of a problem with Arrays

 
0
  #7
Nov 19th, 2008
Ah Ha! it works, you have my thanks.
The bunny thanks you too. 8D
Now... FEAR THE BUNNY!!!
|\\ //
|(0.0)
|(> <)
||d b
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC