I was trying to array a string within a string. And I keep on receiving a compiler error. So I’m wondering if is even possible to string a string within a string? Because that’s the error I keep on getting. If not then what is another way to create a similar function?

#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
    //Creating Zidane
    const int MAX_CMDS = 6;
    int cmd = 0; 
    string zidane[MAX_CMDS];

    //Creating the main commadands
    int attack = 50;
    int defend = 20;

    //Creating the magic command
    const int MAX_MAGIC = 4;
    string magic[MAX_MAGIC];
    int numMagic;

    int fire = 20;
    int ice = 20;
    int thunder = 20;
    int wind = 20;

    magic[numMagic++] = fire;
    magic[numMagic++] = ice;
    magic[numMagic++] = thunder;
    magic[numMagic++] = wind;

    //Creating tiem comand
    const int MAX_ITEMS = 4;
    string item[MAX_ITEMS];
    int numItems = 0;

    int potion = 30;
    int hiPotion = 70;
    int ether = 20;

    item[numItems++] = potion;
    item[numItems++] = hiPotion;
    item[numItems++] = ether;
    //Still room for one more item.

    //Creating the character's  HP & MP
    unsigned int short HP = 1000;
    unsigned int short MP = 200;

    zidane[cmd++] = HP;
    zidane[cmd++] = MP;
    zidane[cmd++] = attack;
    zidane[cmd++] = defend;

    //This is where I believe the error compiles when building
    zidane[cmd++] = magic;
    zidane[cmd++] = item;

    return 0;
}

Recommended Answers

All 7 Replies

Why are you using strings to contain numeric (int) values? Why aren't you using arrays of ints instead?

string a string within a string

Pardon?

int potion = 30;
int hiPotion = 70;
int ether = 20;

item[numItems++] = potion;
item[numItems++] = hiPotion;
item[numItems++] = ether;

I'm not exactly sure what you intend those lines to do, but I guarentee you that they won't do that.

string[index] returns a reference to a char, so the right side of the assignment should be a char. Since ints are implicitly convertible to chars, assigning an int works, but all that does is to create the character with the byte value 30, 70 or 20 respectively. So, assuming ASCII or a superset, that's an unprintable character, followed by F, followed by another unprintable character.

The same applies later in the code when you try to assign other integers (or shorts) to characters of strings.

zidane[cmd++] = magic;
zidane[cmd++] = item;

Strings aren't implicitly convertible to char, so that's why that won't compile. If you want to insert a string inside another, use the insert method.

@sepp2k So you are saying that in the end HP, MP, attack, and defend aren't really ints but isntead characters assuming I was referring to the ASCll library to call in charaters?
Ok, I understand it now, I just wasn't aware of strings not taking anything that is not a char.

zidane[cmd++] = HP;
zidane[cmd++] = MP;
zidane[cmd++] = attack;
zidane[cmd++] = defend;

@rubberman Well I’m still learning how to use arrays and so I thought this was possible. Array of ints? I don’t think I have learned about that yet, but I will look it up. Thanks

Actually everything I said in my post was bullshit because I can't read properly. I thought your variables were strings, not arrays of strings. Sorry about that.

Since they're arrays of strings, all of your assignments should cause errors. You can't put ints into an array of strings (because ints aren't strings), nor can you put arrays of strings into array of strings (because again arrays of strings aren't strings and you can only put strings into arrays of strings).

Array of ints? I don’t think I have learned about that yet

If you define an array, you write T name_of_the_array[N]; where N is the length of the array and T is the type of value you want to put inside the array. If you want to put strings into an array, you write string name_of_the_array[N]; like you're currently doing; if you want to put in ints, you write int name_of_the_array[N]; instead. And if you want to put arrays of strings into your array, you write string name_of_the_array[N][M]; or string* name_of_the_array[N]; (the former stores N arrays that all must have size M and the latter stores N pointers that can point to arrays of any size).

If you want to put multiple types of value into the same array, you're out of luck because you can't do that (except using pointers to the values and casting them, but that's almost certainly a bad idea here and probably beyond what you've currently learned anyway).

It is not clear to me why you want to put your arrays into the zidane array, but you should probably think of other ways to accomplish what you want.

Ok It's quiteva bit for me to process but I will keep that in mind. I figured aou a way to do what Im Wanted by separating the ints into separate Cahfunctions and a Calling them into the main function.

Char function

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.