#Dont know how to this the right way

16 messages · Page 1 of 1 (latest)

wicked sapphire
#
struct Player_Class {
    int Health;
    int Damage;
    int Int;
    };
void typewriter(char input[]) {
    int inputlenght = strlen(input);
    for (int i = 0; i < inputlenght; i++)
    {
        printf("%c", input[i]);
        Sleep(50);
    }
    printf("\n");
}
void stat_check() {
    int ni;
    char Health[15] = "Health: %d", player.Health;
    char Damage[15] = "Damage: %d", player.Damage;
    char Int[15] = "Int: %d", player.Int;
    typewriter(Health);
    typewriter(Damage);
    typewriter(Int);
    typewriter("[- Type 1 to go back -]");
    scanf("%d", ni);
    return;
}
supple troutBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

dim trench
#

What is your question? What are you trying to do "the right way" that you feel this code does not do?

wicked sapphire
#

I wanna use the variable’s (or I think pointer) value combined with like “Health: “ or “Int: “ then put that in the typewriter function I made

pseudo inlet
#

strlen only works if the char is terminated with \0
also I would use a char * in the typewriter() as well as in the stat_chack()

You also cant do this:
char Damage[15] = "Damage: %d", player.Damage;
you have to first create a char and then use sprintf to format it: https://cplusplus.com/reference/cstdio/sprintf/

dim trench
#

In the typewriter function I would use usleep(50000) rather than Sleep(50). I would also flush the stdout after each character is printed using fflush(stdout); This way the output will print slowly rather than delaying until the end without the flush.

I would pass a struct Player_Class named "player" to the stat_check function so you will have data to print.

Use snprintf to print each line of text to the buffers provided:

    char Health[15];
    char Damage[15];
    char Int[15];
    snprintf(Health, 15, "Health: %d", player.Health);
    snprintf(Damage, 15, "Damage: %d", player.Damage);
    snprintf(Int,    15, "Int:    %d", player.Int);
    typewriter(Health);
    typewriter(Damage);
    typewriter(Int);

Your scanf needs the address of the variable:

scanf("%d", &ni);
grave bane
#

i think you can use const char * in typewriter function to just say typewriter("Health:");

#

but you would need to convert the int values to char

wicked sapphire
#

Oh I completely forgot this was still open

#

I fixed with issue with sprintf()

#

Ty for the help

#

!answered

#

!fixed

#

?

#

!solved