Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 01-16-2005, 09:39 PM   #11 (permalink)
 
Newb Techie

Join Date: Aug 2004

Posts: 22

vidyaputra

Default

why is the "items " variabel made as double array?
isit always like that for an arry of char?
or it has other reason^^
(items[100][20]; )

-vip-
vidyaputra is offline  
Old 01-18-2005, 08:56 PM   #12 (permalink)
 
Newb Techie

Join Date: Jan 2005

Posts: 9

holy_devil_

Default

items[100][20] means that it can store 100 strings each 20 characters long.

for example an array like item[5][5] ;

0 1 2 3 4 5 6 (col)
1 v i k i
2 g u n
3 d o g
4 c o o l
5
6
(row)

this array can store maximum of 5 strings each 5 characters long not actually 5 chars long its 4 chars long .. becoz in strings the last character is the NULL character"\0".
holy_devil_ is offline  
Old 01-19-2005, 02:07 AM   #13 (permalink)
 
True Techie

Join Date: Jan 2005

Posts: 113

RedRecon2004

Default

Yeah, here is an example of arrays:

string array[3][5] = {
{"sword","knife","shield","dagger","bow"},
{"spear","rocks","club","shield","torch"},
{"knife","pistol","rifle","SMG","grenade"}
};


You can look at it this way. Act as if you are talking about boxes, which can hold objects.

Since it is [3][5], it would be 3 boxes, and each box can hold 5 items (in this case, the items are strings - which represent weapons).
__________________
I\'m not a vegetable!
RedRecon2004 is offline  
Old 01-19-2005, 04:17 AM   #14 (permalink)
 
Newb Techie

Join Date: Aug 2004

Posts: 22

vidyaputra

Default OOO I see

mmm, so thats why the items variabel made as double array^^

i see......thanks a lot^^

-vip-
vidyaputra is offline  
Old 01-19-2005, 10:44 AM   #15 (permalink)
 
True Techie

Join Date: Jan 2005

Posts: 113

RedRecon2004

Default Re: OOO I see

Quote:
Originally posted by vidyaputra
mmm, so thats why the items variabel made as double array^^

i see......thanks a lot^^

-vip-
Hehe, yeah. Multi-dimensional (or double) arrays can be quite confusing if you forget the [box][items held] idea...

Normal arrays are very easy to make... here is an example of one that can hold five string elements. (With comments)

#define MAX_ITEMS 5 // Create a constant named "MAX_ITEMS" that is set to 5.

string array[MAX_ITEMS] = {"one","two","three","four","five"}; //Creates the array, giving it 5 string objects.

for (int i = 0; i < MAX_ITEMS; ++i)
cout << array[i] << endl; //Give output as to what is in the array.

--------------

And now without the comments:

#define MAX_ITEMS 5

string array[MAX_ITEMS] = {"one","two","three","four","five"};

for(int i = 0; i < MAX_ITEMS; ++i)
cout << array[i] << endl;
RedRecon2004 is offline  
Old 01-19-2005, 08:45 PM   #16 (permalink)
 
Newb Techie

Join Date: Jan 2005

Posts: 9

holy_devil_

Default

[QUOTE]Originally posted by RedRecon2004
[B]Yeah, here is an example of arrays:

string array[3][5] = {
{"sword","knife","shield","dagger","bow"},
{"spear","rocks","club","shield","torch"},
{"knife","pistol","rifle","SMG","grenade"}
};


You can look at it this way. Act as if you are talking about boxes, which can hold objects.



I didnt know that string is a data type in c++. IS IT REALLY.
I dont think we can define string arr[20]
holy_devil_ is offline  
Old 01-20-2005, 12:49 AM   #17 (permalink)
 
Newb Techie

Join Date: Aug 2004

Posts: 22

vidyaputra

Arrow mine doesnt work well

mine, cant show the items well???
(why)....^^

-vip-

#include <conio.h>
#include <stdio.h>
#include <string.h>

void main()
{
int chs;
int gold = 10000;
int i = 0;
char item[100][20];
clrscr();
do
{
printf("\nMerchant Game\n");
printf("-------------\n");
printf("1. Hand Gun 50\n");
printf("2. Mechine Gun 150\n");
printf("3. Bazooka 200\n");
printf("4. Nuclear Bomb 250\n");
printf("5. Bought Item\n");
printf("6. << Exit >>\n");
printf("Gold : %d\n",gold);
printf("buy >>");
scanf("%d",&chs);

switch(chs)
{
case 1 :{
if (gold <= 0)
{
printf("Not enough gold");
}else
{
gold = gold - 50;
i++;
strcpy(item[i],"Hand Gun");
}
}break;
case 2 :{
if(gold <= 0)
{
printf("Not enough gold");
}else
{
gold = gold - 150;
i++;
strcpy(item[i],"Machine Gun");
}
}break;
case 3 :{
if(gold <= 0)
{
printf("Not enough gold");
}else
{
gold = gold - 200;
i++;
strcpy(item[i],"Bazooka");
}
}break;
case 4 :{
if(gold <= 0)
{
printf("Not enough gold");
}else
{
gold = gold - 250;
i++;
strcpy(item[i],"Nuclear Bomb");
}
}break;
case 5 :{
for(int j=0;j<=i;j++)
{
printf("%s",item[j]); //not show the data perfectly???
}
}break;
}
}while(chs != 6);

getch();
}
vidyaputra is offline  
Old 01-20-2005, 12:49 AM   #18 (permalink)
 
True Techie

Join Date: Jan 2005

Posts: 113

RedRecon2004

Default

[QUOTE]Originally posted by holy_devil_
[B]
Quote:
Originally posted by RedRecon2004
Yeah, here is an example of arrays:

string array[3][5] = {
{"sword","knife","shield","dagger","bow"},
{"spear","rocks","club","shield","torch"},
{"knife","pistol","rifle","SMG","grenade"}
};


You can look at it this way. Act as if you are talking about boxes, which can hold objects.



I didnt know that string is a data type in c++. IS IT REALLY.
I dont think we can define string arr[20]
Are you being sarcastic about the string data type... or are you being serious? If you're being serious, then yeah, C++ has string data types.

You can, indeed define string arr[20] ..... why wouldn't you be able to!?

Just do it like this:


string arry[20] = {"one","two","three","four","five","six","seven"," eight","nine","ten","eleven","twelve","thirteen"," fourteen","fifteen","sixteen","seventeen","eightee n","nineteen","twenty"};

---------------------------
To vidyaputra:

You need to change alot of the things in your code... first of all, just use #include like this:
#include <iostream>
#include <string>
using namespace std;

Then, instead of "void main()" use "int main()". And for one last thing, DO NOT use printf! Use cout!

Study this code that mimics an item shop but that uses proper, modern C++.



#include <iostream>
#include <string>
using namespace std; // Use the following instead

int main() //Use this instead of void main()
{
int choice;
int gold = 100;

#define MAX_ITEMS 15 //Helps create an array that can hold 30 items (Just in case...)
int numItems = 0; //Helps keep track of items currently held
string inventory[MAX_ITEMS]; //Creates an array named "inventory" that can hold "15" items (Note: MAX_ITEMS)

cout << "Welcome to my item shop! \n\n";
cout << "Purchase anything you'd like! \n";

//The main loop
while(gold > 0)
{
if(numItems >= MAX_ITEMS)//Checks to see if the max capacity for items is met - if so, the program ends.
{
cout << "Your inventory is full!\n\n";
system("pause");
return 0;
}
cout << "You have " << gold << " gold remaining.\n\n";
cout << "[1] Potion - 5 gold.\n";
cout << "[2] Magic Potion - 5 gold.\n";
cout << "[3] Knife - 10 gold.\n";
cout << "[4] Sword - 20 gold.\n";
cout << "[5] Bow - 15 gold.\n";
cout << "Your choice: \n";
cin >> choice;

switch (choice)
{
case 1:
cout << "You purchased a potion.\n";
inventory[numItems++] = "Potion"; //Add potion to inventory
gold = gold - 5; //Take away 5 gold for the purchase

cout << "\nYour inventory is now: \n";
for(int i = 0; i < numItems; ++i)
cout << inventory[i] << endl; //Display inventory (Used in 4 other cases)

cout << endl << endl;
break;

case 2:
cout << "You purchased a magic potion.\n";
inventory[numItems++] = "Magic potion"; //Add magic potion to inventory
gold = gold - 5; //Take away 5 gold for the purchase

cout << "\nYour inventory is now: \n";
for(int i = 0; i < numItems; ++i)
cout << inventory[i] << endl;

cout << endl << endl;
break;

case 3:
cout << "You purchased a knife.\n";
inventory[numItems++] = "Knife"; //Add knife to inventory
gold = gold - 10; //Take away 10 gold for the purchase

cout << "\nYour inventory is now: \n";
for(int i = 0; i < numItems; ++i)
cout << inventory[i] << endl;

cout << endl << endl;
break;

case 4:
cout << "You purchased a sword.\n";
inventory[numItems++] = "Sword"; //Add sword to inventory
gold = gold - 20; //Take away 20 gold for the purchase

cout << "\nYour inventory is now: \n";
for(int i = 0; i < numItems; ++i)
cout << inventory[i] << endl;

cout << endl << endl;
break;

case 5:
cout << "You purchased a bow.\n";
inventory[numItems++] = "Bow"; //Add bow to inventory
gold = gold - 15; //Take away 15 gold for the purchase

cout << "\nYour inventory is now: \n";
for(int i = 0; i < numItems; ++i)
cout << inventory[i] << endl;

cout << endl << endl;
break;

default:
cout << "You did not chose a valid choice.";
cout << endl << endl;
break;
}
}
system("pause");
return 0;

}
RedRecon2004 is offline  
Old 01-20-2005, 01:16 AM   #19 (permalink)
 
Newb Techie

Join Date: Aug 2004

Posts: 22

vidyaputra

Default

so, is it for an array of char we always need multi-dimensional array
and for string we could use single array?!?

is it correct?
vidyaputra is offline  
Old 01-20-2005, 01:24 AM   #20 (permalink)
 
True Techie

Join Date: Jan 2005

Posts: 113

RedRecon2004

Default

Quote:
Originally posted by vidyaputra
so, is it for an array of char we always need multi-dimensional array
and for string we could use single array?!?

is it correct?
It is correct, but some of your code is old C++ and needs to be "modernized".

Read the second part of my last post, directed to yours... good luck!
RedRecon2004 is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On