Closed. This question needs to be more focused。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。
                        
                        19天前关闭。
                                                                                            
                
        
我创建了一个程序作为测验。还差得远,但我正在努力。

代码如下:

#include <stdio.h>
#include <string.h>
#include <iostream>


int optionOne(int choice, int plonkPoints);
int optionTwo(int choice, int plonkPoints);
int optionThree(int choice, int plonkPoints);
int optionFour(int choice, int plonkPoints);
int optionFive(int choice, int plonkPoints);
int optionOnev2(int choice, int plonkPoints);

int plonkPoints = 0;

void main()
{

    char fName[30];
    char sName[30];
    char rightFname[30] = "Thomas";
    char rightSname[30] = "Sloane";

    char rightFname2[30] = "Ryan";
    char rightSname2[30] = "Gleeson";

    int result;
    printf("This is the Plonker test, it will determine where you are on the scale of Plonkerhood. \nGood luck and don't mess up you twat.\nTo answer yes type 1, to answer no type 0 \n");
    printf("\nFirst what is your first name?: ");
    scanf_s("%s", fName, sizeof fName);

    printf("What is your surname?: ");
    scanf_s("%s", sName, sizeof sName);

    int res1 = strcmp(fName, rightFname);
    int res2 = strcmp(sName, rightSname);

    int res3 = strcmp(fName, rightFname2);
    int res4 = strcmp(sName, rightSname2);

    if (res1 == 0 && res2 == 0)
        plonkPoints = 528;


    if (res3 == 0 && res4 == 0)
        plonkPoints = 528;



    int choice;
    printf("\nQuestion 1: Are you a plonker?: ");
    scanf_s("%d", &choice);
    if (choice == 0)
        plonkPoints = plonkPoints + 1;

    if (choice == 1)
        plonkPoints = plonkPoints + 0;

    printf("points: %d\n", plonkPoints);

    while (choice != 0 && choice != 1)
    {
        printf("That's not a valid answer!\n");
        printf("\nQuestion 1: Are you a plonker: ");
        scanf_s("%d", &choice);
        if (choice == 0)
            plonkPoints = plonkPoints + 1;

        if (choice == 1)
            plonkPoints = plonkPoints + 0;


    }



    printf("\nQuestion 2: What does ORTCEMF stand for?\n");
    printf("1. Only Retards Take Canned Eggs Mother Fucker\n");
    printf("2.\n");
    printf("3.\n");
    printf("4.\n");
    printf("5.\n");
    printf("Enter your answer: ");
    scanf_s("%d", &choice);
    optionOnev2(choice, plonkPoints);
    printf("points = %d", plonkPoints);

}


    int optionOne(int choice, int plonkPoints)
    {
        switch (choice)
        {
        case 1: plonkPoints = plonkPoints + 5;
            break;
        case 2: plonkPoints = plonkPoints + 1;
            break;
        case 3: plonkPoints = plonkPoints + 4;
            break;
        case 4: plonkPoints = plonkPoints + 2;
            break;
        case 5: plonkPoints = plonkPoints + 3;
            break;
        }
        return plonkPoints;
        printf("Plonkpints : %d", plonkPoints);

    }

    int optionTwo(int choice, int plonkPoints)
    {
        switch (choice)
        {
        case 1: plonkPoints = plonkPoints + 1;
            break;
        case 2: plonkPoints = plonkPoints + 5;
            break;
        case 3: plonkPoints = plonkPoints + 2;
            break;
        case 4: plonkPoints = plonkPoints + 4;
            break;
        case 5: plonkPoints = plonkPoints + 3;
            break;
        }
        return plonkPoints;
    }

    int optionThree(int choice, int plonkPoints)
    {
        switch (choice)
        {
        case 1: plonkPoints = plonkPoints + 2;
            break;
        case 2: plonkPoints = plonkPoints + 4;
            break;
        case 3: plonkPoints = plonkPoints + 5;
            break;
        case 4: plonkPoints = plonkPoints + 1;
            break;
        case 5: plonkPoints = plonkPoints + 3;
            break;
        }
        return plonkPoints;
    }

    int optionFour(int choice, int plonkPoints)
    {
        switch (choice)
        {
        case 1: plonkPoints = plonkPoints + 3;
            break;
        case 2: plonkPoints = plonkPoints + 4;
            break;
        case 3: plonkPoints = plonkPoints + 2;
            break;
        case 4: plonkPoints = plonkPoints + 5;
            break;
        case 5: plonkPoints = plonkPoints + 1;
            break;
        }
        return plonkPoints;
    }

    int optionFive(int choice, int plonkPoints)
    {
        switch (choice)
        {
        case 1: plonkPoints = plonkPoints + 4;
            break;
        case 2: plonkPoints = plonkPoints + 1;
            break;
        case 3: plonkPoints = plonkPoints + 2;
            break;
        case 4: plonkPoints = plonkPoints + 3;
            break;
        case 5: plonkPoints = plonkPoints + 5;
            break;
        }
        return plonkPoints;

    }




我创建了五个功能。这些功能将用于具有5个选项的问题,每个选项为您提供不同数量的分数。例如,测验的问题2为选项1给出了5分,因为它是正确的选项。我已经定义了每个函数,并在似乎运行良好的主要函数之前声明了它们。对于每个功能,我都希望变量选择(用户选择的选项编号)成为该功能的输入。然后它将起作用,并将变量“ plonkPoints”作为输出。在这种情况下,当我选择问题二的第一个选项时,我应该再加五分。我什至放入printf进行检查。由于某些原因,没有将点添加到变量“ plonkPoints”。

我最初将变量“ plonkPoints”作为局部变量,但后来将其设置为全局变量,但这似乎无济于事。

任何帮助将不胜感激。谢谢你们。

最佳答案

首先:optionOnev2是已声明但从未在程序中定义的,因此当您尝试在main的末尾调用它时,它将不起作用。

第二:将int plonkPoints定义为每个option函数的输入时,您正在创建一个局部变量,该局部变量每次调用时仅存在于该函数中。此局部变量与全局变量plonkPoints完全无关。如果希望您的option函数能够访问全局plonkPoints,则应从每个函数中删除输入plonkPoints

第三:总的来说,全局变量不是一个明智的主意。而不是使用全局plonkPoints,应将其作为main中的局部变量,将指向它的指针传递给每个option函数,并让它们通过取消引用该指针来修改plonkPoints的值。 。

10-06 01:54