Question

Ok so I'm creating this text adventure game. To be all unique and different, I decided to add an influence system where your responses to other characters can affect their responses to you as well their combat effectiveness. I have a fair amount of experience in C++ and I just started my second semester-long course in C. So far, I have tried using global variables, structs, static variables, and using functions inside of other functions. I also tried using pointers but I kept getting errors every time so I stopped. This is a snippet of code that tries to use the influence system(Sorry for the stars, wouldn't want to give away any story plots):

#include <stdlib.h>
#include <stdio.h>
static int positive_Influence; int negative_Influence; int overall_Influence;

void printpInI(int positive_Influence, int negative_Influence);//positive and negative influence

int choice_6()
{
    int choice = 0;
    int positive_Influence, negative_Influence, overall_Influence;

    positive_Influence = 0;
    negative_Influence = 0;
    overall_Influence = 0;

    printf("What do you do?\n");
    printf("\t1. You: ****\n");
    printf("\t2. You: ****\n");
    printf("\t3. You: ****\n");
    do 
    {
        scanf_s("%i", &choice);
        switch (choice)
        {
            case 1:
                {
                    printf("\t****?\n");
                    system("pause");
                    negative_Influence += 10;
                    printf("You lose influence and are now at %i with ****.\n", positive_Influence-negative_Influence);
                    break;
                }
            case 2:
                {
                    printf("\t****");
                    system("pause");
                    positive_Influence += 10;
                    printf("You gain influence and are now at %i influence with ****.\n", positive_Influence-negative_Influence);
                    break;
                }
            case 3:
                {
                    printf("**** smiles at this.\n");
                    system("pause");
                    positive_Influence += 10;
                    printf("You gain influence and are now at %i influence with ****.\n", positive_Influence-negative_Influence);
                    break;
                }
        }
    }while(choice != 1 && choice != 2 && choice != 3);
    overall_Influence = positive_Influence-negative_Influence;
    printf("%i\n", overall_Influence);
}

void story_7()
{
    printf("Your overall influence is %i\n", overall_Influence);
}
int main()
{
    choice_6();
    story_7();
    system("pause");
}
Was it helpful?

Solution

You have declared overall_influence as a global but also as a local in choice_6. The local declaration takes precedence; just remove that and you should be OK. Same thing about the variables positive_influence and negative_influence.

OTHER TIPS

Can you tell me which type of errors you got? Also you already declared global variables for calculating influence so why you again declared it locally in choice_6 function and that is the error case in you program so local variables have more precedence then global one within function in which they declared. So remove declaration from function choice_6.

I actually was just being really dumb because I forgot hows pointers worked (dam ampersans!) anyway thank you all for replying I really really do appreciate it. I am also working on a combat system that can use multiple enemies so keep watching my profile because I'll probably have moar questions later. If you would like to beta test, my email is mikemanahan15@gmail.com if you would like to play what I have so far. Of course, here is the finished code (for choice_6 and story_7) enjoy:

#include <stdlib.h>
#include <stdio.h>


int choice_6(int *influence)
{
int choice = 0;
    printf("What do you do?\n");
    printf("\t1. You: No. I know absolutely nothing about you. You could be a monster\n");
    printf("\ttoo for all I know! Normal people don't turn into frogs!\n");
    printf("\t2. You: Your right we should keep moving. You can tell me when your\n");
    printf("\tready.\n");
    printf("\t3. You: Okay where do you think should we go then?\n");
    do 
    {
        scanf_s("%i", &choice);
        switch (choice)
        {
            case 1:
                {
                    printf("\tBrie flashes a pained look on her face and you immediately regret saying that.\n");
                    printf("\tBrie: You really think I'm a monster?\n");
                    system("pause");
                    *influence -= 10;
                    printf("You lose influence and are now at %i with Brie.\n", *influence);
                    system("pause");
                    printf("Influence affects characters reactions towards you, their combat effectiveness, and even their allegiances in rare cases.\n");
                    printf("However, depending on the situation, low influence is not always a bad thing...\n");
                    system("pause");
                    printf("\tYou: Your right we should keep moving.\n");
                    break;
                }
            case 2:
                {
                    printf("\tBrie: Thank you. I'd rather not discuss this my life story in a dark\n");
                    printf("\tdungeon.\n");
                    system("pause");
                    *influence += 10;
                    printf("You gain influence and are now at %i influence with Brie.\n", *influence);
                    system("pause");
                    printf("Influence affects characters reactions towards you, their combat effectiveness, and even their allegiances in rare cases.\n");
                    printf("However, depending on the situation, low influence is not always a bad thing...\n");
                    system("pause");
                    printf("\tYou: I'd have to agree with you there. Let's see what is up these stairs.\n");
                    choice = 2;
                    break;
                }
            case 3:
                {
                    printf("Brie smiles at this.\n");
                    printf("\tBrie: Well the only way out seems to be these stairs so let's go up.\n");
                    system("pause");
                    *influence += 10;
                    printf("You gain influence and are now at %i influence with Brie.\n", *influence);
                    system("pause");
                    printf("Influence affects characters reactions towards you, their effectiveness in combat, and even their allegiances in rare cases.\n");
                    printf("However, depending on the situation, low influence is not always a bad thing...\n");
                    system("pause");
                    printf("\tYou: Sounds good to me I am quite frankly done with dungeons about now.\n");
                    break;
                }
            default:
                {
                    printf("Type the number for the choice you want to do\n");
                    system("pause");
                    break;
                }
        }
    }while(choice != 1 && choice != 2 && choice != 3);
}

int story_7(int influence)
{
    if (influence > 0)
    {
    printf("Brie laughs at this and slowly leans you off her to have both of you crouch.");
    system("pause");
    }
    else
    {
        printf("Brie: Ugh just get off of me!\n");
        printf("Brie pushes you off her violently and you manage to stay crouched.");
        system("pause");
    }
}

int main()
{
    int influence = 0;
    // ...
    choice_6(&influence);
    story_7(influence);
    // ...
}

You should remove the local variables and use scope resolution operator to use your global variables This would solve your question.

Also you can use these variables in any function you want later.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top