2014-01-07 29 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

您應該刪除本地變量和使用範圍解析操作者使用你的全局變量 這將解決你的問題。

此外,您可以在稍後需要的任何功能中使用這些變量。

相關問題