Jump to content

C++ Arrays

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
nicckk

nicckk

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 629 posts
I never really learned arrays before I quit, and thought I would learn them now in a little game but its not really working out, with a syntax error in my code.


//Coded by Nick Dichev

#include <iostream>

#include <cstdlib>

using namespace std;


int main()

{

    int inventory[5];

        inventory[0] = "Sword";

        inventory[1] = "Sheild";

        inventory[2] = "Health Potion";

    int menuchoice;

    cout << "Welcome to Generic War Game, Please select a choice\n";

    cout << "[1] Inventory   |   [2] Battle   |   [3] Store   |   [4] Exit\n";

    cin >> menuchoice;

    if( menuchoice = 1)

        cout <<"Your inventory consists of " << inventory;

    if( menuchoice = 2)

    if( menuchoice = 3)

    if( menuchoice = 4)

       exit(1);

        

       system ("PAUSE");

       return 0;


}


When I try to compile I get
"Invalid conversion from cont char to int"
Any ideas?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You declared inventory as an array of ints. "Sword" is not an int. I think you meant to declare inventory as an array of std::string.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Well, the first thing I notice is your if statements are wrong. They should be "if (menuchoice == 1)", which is the comparison operator, while "menuchoice = 1" is the assignment operator. That, and you're declaring the 'inventory' array as an int, and then assigning it chars, and you can't do that. :)

I would instead use Vectors. They're easier to use and a lot more versatile than standard C arrays. The way to make the C arrays work is by using "char inventory[5][256];" (256 being the maximum amount of characters you can have). However, all in all, I'd use a Vector of String objects, making everything lot's easier to use.

#include <iostream>
#include <vector>
#include <string>

int main()
{
    std::vector<std::string> inventory;
    inventory.push_back("Sword");
    inventory.push_back("Shield");
    inventory.push_back("Health Potion");
    // everything else here.
}
Notice you don't have to define the size of the vector or the strings, and things should be peachy.

EDIT: Also, (sorry if I'm being super critical here, but I'm trying to be as helpful as possible) the code
    if( menuchoice = 2)
    if( menuchoice = 3)
    if( menuchoice = 4)
       exit(1);
is asking for a world of hurt. Instead it should be
if (menuchoice == 2 || menuchoice == 3 || menuchoice == 4)
    exit(1);
And also I'd drop the "system("pause")" statement, and instead go with "cin.get()", what way you don't need cstdlib and it's platform independent.

Edited by ZekeDragon, 16 August 2009 - 01:36 PM.
See Edit

Wow I changed my sig!

#4
nicckk

nicckk

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 629 posts

ZekeDragon said:

Well, the first thing I notice is your if statements are wrong. They should be "if (menuchoice == 1)", which is the comparison operator, while "menuchoice = 1" is the assignment operator. That, and you're declaring the 'inventory' array as an int, and then assigning it chars, and you can't do that. :)

I would instead use Vectors. They're easier to use and a lot more versatile than standard C arrays. The way to make the C arrays work is by using "char inventory[5][256];" (256 being the maximum amount of characters you can have). However, all in all, I'd use a Vector of String objects, making everything lot's easier to use.

#include <iostream>

#include <vector>

#include <string>


int main()

{

    std::vector<std::string> inventory;

    inventory.push_back("Sword");

    inventory.push_back("Shield");

    inventory.push_back("Health Potion");

    // everything else here.

}
Notice you don't have to define the size of the vector or the strings, and things should be peachy.

EDIT: Also, (sorry if I'm being super critical here, but I'm trying to be as helpful as possible) the code
    if( menuchoice = 2)

    if( menuchoice = 3)

    if( menuchoice = 4)

       exit(1);
is asking for a world of hurt. Instead it should be
if (menuchoice == 2 || menuchoice == 3 || menuchoice == 4)

    exit(1);
And also I'd drop the "system("pause")" statement, and instead go with "cin.get()", what way you don't need cstdlib and it's platform independent.

Thanks Zeke

the if(menuchoice = 2) will be be filled out later, im just working it out peice by peice.

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts

Quote

if (menuchoice == 2 || menuchoice == 3 || menuchoice == 4)
exit(1);

Better off using a switch statement here, it's more like what you were originally thinking.

switch(menuchoice)
{
case 1:
    do_something();
    break;
case 2:
case 3:
case 4:
    something_else();
    break;
default:
    //user picked something invalid
    cout << "Invalid choice, try again." << endl;
    break;
}

(Anal note on top of ZekeDragon's note - you spelled "shield" wrong. :) )
sudo rm -rf /