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.