• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

C++/cpp Question "String[index]"

Status
Not open for further replies.
Level 2
Joined
Feb 13, 2013
Messages
5
hi to all HiveWorkshop members : D

here in warcraft/starcraft trigger editors you can make strings like this and use them later with integer actions
String[1]=apple
String[2]=orange
String[3]=carrot

for each Integer A from 1 - 3 do actions
game text to all players:String[Integer A]

i cant find a way to do that in C++,i only managed to use partial strings but that cant work for me because it requires a very looooooong string
it probably must be char[index] in c++ but i cant make it work anyway...
i want something like " printf(char[index]) " , i can use other variables on "printf" like float,int.... but cant do it with string variable type :/

Edit:
im using C-Free code
 
The string is a class. It's not native to C++, but it is available in the standard library.

The solution is to use the c_str() member of the class.

C++:
#include <string>
#include <stdio.h>

int main() {
    std::string myString = "Hi bro";
    printf(myString.c_str());

    // This stops the application from quitting until the user presses a key followed by Enter.
    char c;
    scanf("%c", &c);
    return 0;
}
 
Level 2
Joined
Feb 13, 2013
Messages
5


thanks for replies but i don´t know how i can use that in this way : /

C++:
#include <stdio.h>
#include <math.h>
int main(){ int N = 0;float String[10] = {0};	
String[1] = 11111,
String[2] = 22222,
String[3] = 33333,
N=3,printf("My Strings");N;for(N = 1; N <= 3; N++)
printf("\n""%5.f",String[N]);
return  0;}
This outputs:
My Strings
11111
22222
33333


i would like to do something like this but i want to use string variables so i can print "apple" instead "11111"
 
Last edited:
Level 8
Joined
Aug 13, 2009
Messages
466
Check the printf options in whatever c++ spec you can find, there might be a %s to use with strings.

Otherwise, why not explore that std::string class? It might have some useful methods that render all your fiddling unnecessary.
 
Level 8
Joined
Aug 13, 2009
Messages
466
That's the spirit, now try some more things, struggle for hours, and realize you made a trivial error. It's a rite of passage!
 
Level 2
Joined
Feb 13, 2013
Messages
5
That's the spirit, now try some more things, struggle for hours, and realize you made a trivial error. It's a rite of passage!
Sorry but im new in C++ language...,I did struggle for hours that is why I made a new account here to get help ,I know this is a good site my account was ALiEN95 but i cant login to @live email at all

for example
http://www.tutorialspoint.com/cplusplus/cpp_strings.htm
did not help me at all
 
C++:
const char* str = "apple";
printf(str);

Is this what you're trying to achieve? :v

edit
Also, the code you posted above is quite messy and some of it makes no sense :/

What are you using to learn C++?
When I learned it, I used a book in order to properly code.

This is what a proper version of your code would look like:

C++:
#include <stdio.h>

int main() {
    /*
    *   Declare an array of ints of size 3.
    *   This will also set all the values in the array.
    *
    *   It does the same thing as this:
    *
    *       int numbers[3];
    *       numbers[0] = 11111;
    *       numbers[1] = 22222;
    *       numbers[2] = 33333;
    *
    *   Except this is shorter, so I'd go with it.
    *   I didn't see the need to use floats, so I used ints.
    *   You don't need floats unless you want decimal numbers.
    *   I'd often recommend the usage of doubles instead of floats 
    *   as well, because despite the fact that doubles use more 
    *   memory, they are incredibly accurate.
    */
    int numbers[3] = {11111, 22222, 33333};

    printf("My Strings");

    /*
    *   This will loop 3 times. (For n = 0, n = 1 and n = 2).
    */
    for (int n = 0; n < 3; n++) {
        printf("\n%d", numbers[n]);
    }

    return  0;
}

Point is, you need to learn the language properly from a good source.
If you don't have access to a good book about C++ or anything, then your best bet is this.
 
Last edited:
Level 2
Joined
Feb 13, 2013
Messages
5
Thanks for pointing out the shorter method for start :goblin_good_job:



C++:
#include <stdio.h>

[COLOR="Red"]int main() {

    int numbers[3] = {11111,22222,33333};
    char* str[3] = {"apple","orange","carrot"};

    printf("My Strings");

    
    for (int n = 0; n < 3; n++) {
        printf("\n%d", numbers[n]);
    }
    for (int n = 0; n < 3; n++) {
        printf("\n%s",(str[3]));
    }
    return  0;
}[/COLOR]
Problem - this outputs:
My Strings
11111
22222
33333


♥Press any key to continue . . .

im guessing i need to change "s" to something else at this line
printf("n\%s",(str[3]));
at lest for now i have this 3 hearts just for you :ogre_hurrhurr:

blahblahblah incomeing
with this "char" thing sometimes i would get random things that makes no sense,all i need to get is the damn text :vw_death:
im trying to learn it but this i cant find anywhere
im using this only to count some of my item values i manged to make it output all the numbers attached to them but i want to see the name of my item next to it,for now i see a number and read the name on other page

Edit: ok dont need helping for now : D
F***
ok so the NUMBER needs to be 2 actually it needs to be int "n" i forgot that it starts with 0,1,2 so 3 would output hearts as its not set


now its like this:
#include <stdio.h>

int main() {

char* str[4] = {"","carrot","cevapi","burek"};

for (int n = 1; n < 4; n++) {
printf("\n%s",(str[n]));
}
return 0;
}

i think im gonna be fine now the thread is solved for me : )
 
Status
Not open for further replies.
Top