2014-01-07 31 views
0

好吧,我正在创建这个文本冒险游戏。为了独一无二,我决定增加一个影响系统,让你对其他角色的反应可以影响他们对你的反应以及他们的战斗力。 我在C++方面拥有相当丰富的经验,并且刚刚开始了我的第二学期的C课程。到目前为止,我已经尝试过使用全局变量,结构体,静态变量以及其他函数内部的函数。我也试过使用指针,但每次都会收到错误,所以我停下来了。这是一个代码片段,尝试使用影响系统(很抱歉的明星,不想放弃任何故事情节):在C中,如何评估一个函数中的局部变量,在不同的函数中?

#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"); 
} 
+0

你实际上没有说明你的问题。我的意思是根据你的代码说你遇到的实际问题是什么。 – Brandin

回答

3

您已经声明overall_influence作为一个全球性的,但也为本地在choice_6。本地声明优先;只要删除,你应该没问题。关于变量positive_influencenegative_influence的同样的事情。

1

你能告诉我你得到哪种类型的错误吗?你也已经声明了用于计算影响力的全局变量,所以为什么你再次在choice_6函数中声明了它,并且这是你程序中的错误情况,所以局部变量在它们声明的函数中具有更多优先级,然后是全局变量。因此请从功能choice_6中删除声明。

0

我其实真的很笨,因为我忘了指针的工作(大坝ampersans!)无论如何谢谢大家的回复我真的真的很感激它。我也在研究可以使用多个敌人的战斗系统,所以请继续观看我的个人资料,因为我可能会在稍后发出警告问题。如果您想进行beta测试,我的电子邮件是[email protected],如果您想播放我目前为止的内容。当然,这里是最终的代码(choice_6和story_7)享受:

#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); 
    // ... 
} 
0

您应该删除本地变量和使用范围解析操作者使用你的全局变量 这将解决你的问题。

此外,您可以在稍后需要的任何功能中使用这些变量。

相关问题