I was trying to write a program and it seems totally fine but i cannot find the reason why it doesn't work, any help would be thankful

46 views Asked by At

I have created a program which acts as a quiz. It is nowhere near done but I am working on it.

The code is below:

#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;

    }


I have created five functions. These functions will be used for questions that have 5 options and each option gives you a different amount of points. For example question 2 of the quiz gives 5 points for option one because it is the correct option. I have defined each function and declared them before the main function which seems to be working well. For each of the functions I want the variable choice (the option number which the user chooses) to be the input into the function. It will then work and give the variable "plonkPoints" as the output. In this case when I choose the first option of question two I should then have an extra five points. I even put in a printf to check. For some reason no points are added to the variable "plonkPoints".

I originally had the variable "plonkPoints" as a local variable but then made it a global variable but that did not seem to help.

Any help would be greatly appreciated. Thank you all.

1

There are 1 answers

2
Mark Snyder On

First: optionOnev2 is declared but never defined in your program, so it won't work when you try to call it towards the end of main.

Second: when you define int plonkPoints as an input to each of your option functions, you're creating a local variable that exists only within that function each time it is called. This local variable is completely unrelated to your global variable plonkPoints. If you want your option functions to be able to access your global plonkPoints, you should remove the input plonkPoints from each of the functions.

Third: In general, global variables are not a wise idea. Instead of having a global plonkPoints, you should have it be a local variable in main, pass a pointer to it to each of your option functions, and have them modify the value of plonkPoints by dereferencing that pointer.