Jun 27, 2013 at 4:26pm UTC
Whats wrong with the code?
My friend sent it to me.
works for him
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include <iostream>
using namespace std;
int main()
{
const int max_items = 10;
string inventory[max_items];
int numitems = 0;
inventory[numitems++] = "sword" ;
inventory[numitems++] = "shield" ;
inventory[numitems++] = "Mana Potion" ;
cout << "Your items: " << endl;
for (int i = 0; i < numitems; i++)
cout << inventory[i] << endl;
cout << "You trade your sword for a great sword!" << endl;
inventory[0] = "great sword" ;
cout << "You're items: " << endl;
for (int i = 0; i < numitems; i++)
cout << inventory[i] << endl;
cout << "You found an item! It's a Health Potion!" << endl;
if (numitems < max_items)
{
inventory[numitems++] = "Health Potion" ; cout << "You took the item!" << endl;
}
else
{
cout << "You're carrying too many items." << endl;
}
cout << "Your items: " << endl;
for (int i =0; i< numitems; i++)
cout << inventory[i] << endl;
return 0;
}
Last edited on Jun 27, 2013 at 4:30pm UTC
Jun 27, 2013 at 4:38pm UTC
Whats wrong with the code?
I works for me too. I didn't spot any error but you should use std::vector instead of string array. Because if numitems>=max_items, the program will crash
1 2 3 4 5 6 7
vector<string> inventory; // create vector of strings
inventory.push_back("sword" ); // add item to vector
// ...
// get vector size (instead of numitems) : inventory.size();
for (int i=0;i<inventory.size();i++)
cout <<inventory[i]<<endl;
// ...
Last edited on Jun 27, 2013 at 4:41pm UTC
Jun 27, 2013 at 4:44pm UTC
I donno whats the problem it dosnt seem to work for me
this is what i get for problem
1 2 3 4 5 6
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(502): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_streambuf<_Elem,_Traits> *)'
1> with
1> [
1> _Elem=char ,
1> _Traits=std::char_traits<char >
1> ]
Null we are also using different compilers ..and also he writes code in notepad and i write it in visual studio....
Last edited on Jun 27, 2013 at 4:47pm UTC
Jun 27, 2013 at 4:45pm UTC
I don't see any issue with the code either.
What problem are you having?
Jun 27, 2013 at 4:46pm UTC
"doesn't seem to work" is not very informative. Does it compile? or it crashes when you run it?
Jun 27, 2013 at 4:48pm UTC
Dosnt compile in visual studio..console never comes up and it says would u like to run previous program
Jun 27, 2013 at 4:55pm UTC
Thanks guys it worked ..in my friends compiler u dont need to include it
Thanks all
Last edited on Jun 27, 2013 at 4:56pm UTC