DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   A bit of a problem with Arrays (http://www.daniweb.com/forums/thread158076.html)

MJFiggs Nov 18th, 2008 10:35 am
A bit of a problem with Arrays
 
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.

#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.

Lokolo Nov 18th, 2008 11:12 am
Re: A bit of a problem with Arrays
 
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]

MJFiggs Nov 18th, 2008 2:17 pm
Re: A bit of a problem with Arrays
 
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:

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;
    }

Lokolo Nov 18th, 2008 2:58 pm
Re: A bit of a problem with Arrays
 
#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 :)

MJFiggs Nov 19th, 2008 7:08 pm
Re: A bit of a problem with Arrays
 
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.

Lokolo Nov 19th, 2008 7:16 pm
Re: A bit of a problem with Arrays
 
#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++.

MJFiggs Nov 19th, 2008 7:50 pm
Re: A bit of a problem with Arrays
 
Ah Ha! it works, you have my thanks.
The bunny thanks you too. 8D


All times are GMT -4. The time now is 7:23 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC