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