#include <iostream>
#include <string>
using namespace std;




//char inventoryRequest = 'i';
//Function Definitions.
void exitGame();
int playGame();
int main()
{
//Menu Text.
cout<< "\t******Menu******\n\n";
cout<< "Please choose one of the following:\n";
cout<< "1 -Play Game.\n";
cout<< "2 -Exit.\n";


int choice;
cout<< "Choice: ";
cin>> choice;
string name;
switch (choice)
{
//Menu 1 choice.
case 1:
playGame();
break;
//Menu 1 choice.
case 2:
exitGame();
break;
default:
cout<< "That is not a choice!!!\n";
}
getchar();
getchar();
return 0;
}

//Method for play game.
int playGame()
{
//string inventory[10];
//inventory[0] = "stick";
//inventory[1] = "rock";
string name;
int age;
char sex;
cout<< "What is your name?\n";
cin>> name;
cout<< "What is your age?\n";
cin>> age;
cout<< "What is your sex, M or F?\n";
cin>> sex;
cout <<"Name:"<< name<<" Age:" << age<< " Sex:"<< sex;

cout << "\n\nIt is a cold and rainy night on board the Viking ship that is bringing " << name <<" to his terror.";

return 0;
}

void exitGame()
{
}

int inventory()
{

	char input[80];
	cin >> input;
	char inventoryRequest[] = "i";
	int invent;
	//compare the player input to inventaryRequest (i) to see if they want to look at inventarty
	//invent = strcmp (input,inventaryRequest );
	 //char Input[80];
  do {
     printf ("Guess my favourite fruit? ");
     gets_s (input);
  } while (strcmp (input,inventoryRequest) != 0);

	if(invent = 0)
	//int i = displayInvent();


	return 0;
}

int displayInvent()
{
	string inventory[10];
    inventory[0] = "stick";
    inventory[1] = "rock";
	for(int i = 0;i <  inventory.size();i++)
		cout << inventory[i];
}

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

C++ does not have any possibility to learn array length.

i dont get what you mean there has to be a way to print out an array.

i dont get what you mean there has to be a way to print out an array.

There is, but you have to keep track of the size of the array manually, C++ doesn't do it for you. The method you've attempted would work with a std::vector, which is essentially an array wrapped up in a standardized interface, but not with a plain-old array.

Have another look at your displayInvent() function:

int displayInvent()
 {
 string inventory[10];
 inventory[0] = "stick";
 inventory[1] = "rock";
 for(int i = 0;i < inventory.size();i++)
   cout << inventory[i];
 }

Look specifically at Lines 3 and 6. On Line 3, you declare an array of string objects that is 10 elements long. On Line 6, you attempt to call the function std::string::size(). The problem is, you're trying to call it on the actual array instead of one of the strings that the array contains, you can not do that.

This is one of the pitfalls of using "magic numbers", which you have done on Line 3 (the 10 is a "magic number"). Make the 10 a SIZE constant and use it in the array declaration and as the limiting value in the for loop.

void someFunc() {
  const int SIZE = 5;
  int myArray[SIZE] = {0};

  //...

  for (int i = 0; i < SIZE; ++i) {
    std::cout << myArray[i];
  }
}

Edited 5 Years Ago by Fbody: n/a

There is, but you have to keep track of the size of the array manually, C++ doesn't do it for you. The method you've attempted would work with a std::vector, which is essentially an array wrapped up in a standardized interface, but not with a plain-old array.

Have another look at your displayInvent() function:

int displayInvent()
 {
 string inventory[10];
 inventory[0] = "stick";
 inventory[1] = "rock";
 for(int i = 0;i < inventory.size();i++)
   cout << inventory[i];
 }

Look specifically at Lines 3 and 6. On Line 3, you declare an array of string objects that is 10 elements long. On Line 6, you attempt to call the function std::string::size(). The problem is, you're trying to call it on the actual array instead of one of the strings that the array contains, you can not do that.

This is one of the pitfalls of using "magic numbers", which you have done on Line 3 (the 10 is a "magic number"). Make the 10 a SIZE constant and use it in the array declaration and as the limiting value in the for loop.

void someFunc() {
  const int SIZE = 5;
  int myArray[SIZE] = {0};

  //...

  for (int i = 0; i < SIZE; ++i) {
    std::cout << myArray[i];
  }
}

Worked like a charm thanks very much ye saved me from breaking my laptop :) i only started C++ last week in college after previously doing a year in java. anyways, thanks. :)

This question has already been answered. Start a new discussion instead.