Thread: Passing variable from an outside function to main?

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    6

    Passing variable from an outside function to main?

    This code is for a program that allows you to play a guessing game or arithmetic game and shows your total score from both as you go along. The program works fine the only problem I'm having is with the score. Is there a way to call the score value from the outside function into main?

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    
    //prototype functions
    void menu();
    int guessGame();  //A game to guess a number between 1 and 100
    int arithgame(); //Taken out to make reading easier atm
    
    
    //main function
    int main () {
        int choice, total_score=0, score;
        //choice refers to menu selection
    
    
        printf("Welcome User!\n\n");
        //A simple welcome message for the user
    
    
         while (choice != 4) {
            menu();
            scanf("%d", &choice);
    
    
            if(choice == 1){
    
    
            }
    
    
            else if(choice == 2){
                guessGame();
            }
    
    
            else if(choice == 3){
                total_score = guessGame(&score);
                printf("Your score is %d.\n\n", total_score);
                //This is supposed to show the current score
    
            }
         }
        printf("Thank you for playing!");
        //Quit Statement
    
    
    return 0;
    }
    
    
    int guessGame(){
        srand(time(0));
        //seeding random generator
        int num, guess, turn_count=0, score=0;
        //turn_count will count the number of guesses and turn that into the final score
    
    
        num = rand() % 100 + 1;
    
    
        printf("Guess a number between 1 and 100.\n");
        scanf("%d", &guess);
        turn_count++;
    
    
        while (guess != num) {
            if (guess > num){
            printf("Your guess is too high, try again:\n\n");
            scanf("%d", &guess);
            turn_count++;
            }
    
    
            else if (guess < num){
            printf("Your guess is too low, try again:\n\n");
            scanf("%d", &guess);
            turn_count++;
            }
        }
        printf("Great, you guessed the number %d in %d guesses!", num, turn_count);
        score= 17 - turn_count;
    
    
        return score;
    }
    
    
    int arithgame(){}
    //again removed for reading purposes
    
    
    //menu for convinient calling
    void menu() {
        printf("\nPlease make a selection from the following:\n");
        printf("\t 1 - Play Arithmetic Game.\n");
        printf("\t 2 - Play Guessing Game.\n");
        printf("\t 3 - Print Score.\n");
        printf("\t 4 - Quit.\n");
        return;
    }
    So trying to pass the value of score from the outer function int guessGame() into the score print statement in choice 3 of the main function? Oh and the "17 -turn_count" was just part of the requirements of the assignment. Help is greatly appreciated.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    You need to add some arguments to your function
    Code:
    int guessgame(int *score_input)
    Fact - Beethoven wrote his first symphony in C

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Have a look here for more info
    Functions in C - Cprogramming.com
    Fact - Beethoven wrote his first symphony in C

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Adding a parameter is certainly possible, but I think it's overkill here in that you already have the line
    Code:
    return score;
    in your function which (surprisingly) returns the score to your main program (as desired). Your main program's task, then, is to not throw this value away (which is what you are currently doing). In your main program, the bit of code "guessgame()" has the value that the function returns -- you need to add this value to your total score.

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    6
    Tabstop, I added in:

    Code:
    else if(choice == 2){
                total_score += guessGame();
            }
    Is that what you meant? When I do this though, the score is incorrect. I set both score and total_score to equal zero in main too. Oh and choice 3 looks like this now:

    Code:
    else if(choice == 3){
                printf("Your score is %d.\n\n", total_score);
            }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Surely you don't still have the line
    Code:
    total_score = guessGame(&score);
    in your code, do you? Not only is the function call bad (since you pass a parameter to a function that doesn't take one), but you are then forcing people to play the game when they only want to see the score, and then resetting the total_score to score of that one game.

  7. #7
    Registered User
    Join Date
    Sep 2013
    Posts
    6
    Alright it's working now thank you so much for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing variable from function - main - function
    By ulti-killer in forum C Programming
    Replies: 2
    Last Post: 11-01-2012, 12:14 PM
  2. Help needed with passing a variable back to main
    By Wiretron in forum C Programming
    Replies: 3
    Last Post: 08-18-2006, 09:53 AM
  3. passing variables from main to another function
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 03-06-2006, 07:30 PM
  4. Passing a double array from main to another function
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2005, 05:19 PM
  5. Big Problem With Passing an Matrix to Main Function
    By Maragato in forum C Programming
    Replies: 4
    Last Post: 06-14-2004, 11:06 PM

Tags for this Thread