gpt4 book ai didi

c - 我试图编写一个程序,看起来完全没问题,但我找不到它不起作用的原因,任何帮助将不胜感激

转载 作者:行者123 更新时间:2023-11-30 20:57:25 25 4
gpt4 key购买 nike

我创建了一个充当测验的程序。虽然还远未完成,但我正在努力。

代码如下:

#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 的值通过取消引用该指针。

关于c - 我试图编写一个程序,看起来完全没问题,但我找不到它不起作用的原因,任何帮助将不胜感激,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59977174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com