In the future, please post your code in [code][/code] tags, so we can read it without needing to download it. Like so:
Code:
/* Header Comment
* These should include your name, the date,
* and the title and purpose of your program
*/
//included libraries
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//prototypes
int arithgame(int max, int op);
int guessgame();
int max(int a, int b);
int min(int a, int b);
//main function
int main(){
//seed random number generator
srand(time(0));
//list all variables used in the program
int choice1, i, op, randnum1, randnum2, answer1, a=0, b=0, max, min, randnum3, guess, counter, score=0, score2=0;
//give user menu to choose from
printf("\nWhat would you like to do?\n");
printf("\t1 - Play Arithmetic Game\n");
printf("\t2 - Play Guessing Game\n");
printf("\t3 - Print Score\n");
printf("\t4 - Quit\n");
scanf("%d", &choice1);
while (choice1 != 4){
switch (choice1){
case 1:{
//let user choose which arithmetic game
printf("\t1 - Addition\n");
printf("\t2 - Multiplication\n");
scanf("%d", &op);
while (op == 1){
printf("\nWhat is the maximum value you would like?\n");
scanf("%d", &a);
//create a for loop give the user 10 different problems to solve
for (i=1; i<=10; i++){
//save random number and set its parameters between 1 and the number the user gave
randnum1 = rand() % a + 1;
randnum2 = rand() % a + 1;
//give user addition problem with 2 random numbers to solve
printf("\n%d + %d = ", randnum1, randnum2);
scanf("%d", &answer1);
//if user answers correctly, add a point to their overall score
if (answer1 == arithgame(randnum1,randnum2)){
printf("\n Congradulations, you are correct!\n");
score += 1;}
else
printf("Sorry, the correct answer was %d", arithgame(randnum1,randnum2));
}
//once loop is done, bring user back to menu
break;
}
while (op == 2){
//create random intergers between 1 and 100
randnum1 = rand() % 100 + 1;
randnum2 = rand() % 100 + 1;
//give user mulitplication problem to solve
printf("\n %d * %d = ", randnum1, randnum2);
scanf("%d", &answer1);
//if user answers correctly, add 1 point to their score
if (answer1 != arithgame(randnum1,randnum2))
printf("\nSorry, the correct answer was %d\n", arithgame(randnum1,randnum2));
else{
printf("\n Congradulations, you are correct!\n");
score += 1;
}}
break;
}
case 2:{
//have the user guess a random number between 1 & 100
//loop keeps repeating until answer is found
randnum3 = rand() % 100 + 1;
printf("\nEnter your guess?\n");
scanf("%d",&guess);
while (guess > randnum3){
printf("\nYour guess is too high, try again.\n");
counter += 1;
}
while (guess < randnum3){
printf("\nYour guess is too low, try again.\n");
counter += 1;
}
while (guess == randnum3) {
printf("\nGreat, you guessed the correct number %d in %d guesses", randnum3, counter);
//to calculate score, subtract the number of guesses from 17
//if the number is greater than or equal to 10, give the user 10 points
//if the number is less than or equal to 10, give the user 0 points
//if the number is between 1 & 10, give the user this amount of points
if (a<7)
score2 += 10;
if (a > 17)
score2 += 0;
else
score2 = a;
printf("\nYour score for the round is %d", score2);
break;
}
}
case 3:
//give user their overall score from both arithmetic games and guessing game
score += score2;
printf("\nYour score is %d\n", score);
break;
default:
printf("\nYou did not make a valid selection\n");
break;
}
//give user menu to choose from
printf("\nWhat would you like to do?\n");
printf("\t1 - Play Arithmetic Game\n");
printf("\t2 - Play Guessing Game\n");
printf("\t3 - Print Score\n");
printf("\t4 - Quit\n");
scanf("%d", &choice1);
}
return 0;
}
//arithgame function
int arithgame(int max, int op)
{
while(op == 1)
return max + op;
while(op == 2)
return max*op;
}
//guessing game function
int guessgame()
{
}
//max value function
int max(int a, int b)
{
while (a > b)
return a;
while (b > a)
return b;
}
//min value function
int min(int a, int b)
{
while (a < b)
return a;
while (b < a)
return b;
}
Make sure you compile at the maximum warning level, and fix them all:
Code:
$ make game
gcc -Wall -ggdb3 -pedantic -std=c99 -O0 -o game game.c -lm -lpthread -lrt
game.c: In function ‘main’:
game.c:24:65: warning: unused variable ‘min’ [-Wunused-variable]
game.c:24:60: warning: unused variable ‘max’ [-Wunused-variable]
game.c:24:55: warning: unused variable ‘b’ [-Wunused-variable]
game.c: In function ‘min’:
game.c:183:1: warning: control reaches end of non-void function [-Wreturn-type]
game.c: In function ‘max’:
game.c:174:1: warning: control reaches end of non-void function [-Wreturn-type]
game.c: In function ‘guessgame’:
game.c:164:1: warning: control reaches end of non-void function [-Wreturn-type]
game.c: In function ‘arithgame’:
game.c:158:1: warning: control reaches end of non-void function [-Wreturn-type]
Unused variables are not that big a deal, but the others are. Note, a while is a loop for repeating. A while loop that runs once, and has a break statement at the end that always executes is just pointless. Consider if/else if you don't actually want to repeat.
Also, note that listing explicitly a < b and a > b ignores the case of a == b, which is why you get the error. If a == b, your code never returns a value, which means it exhibits undefined behavior, and anything can happen (in reality, it probably appears as though your functions return garbage values).
Code:
if (a < b)
return a;
else
return b; // if they are equal, they are both the min, so returning a or b is ok
Now, on to your real question:
> First, when the user selects the arithmetic game, I keep on getting different incorrect answers from the arithgame function.
Fixing the above will almost certainly help with this, but you have a bigger problem:
Code:
// call the arithgame function passing in the two numbers to add/mult
arithgame(randnum1,randnum2)
// define the arith game function to take two params, one operand and one operator
int arithgame(int max, int op)
Notice that how you define and how you call your function are different. You must define the function to take the right number and type of parameters (and return the proper type), and you must make sure to call it with the right number and type of parameters in the right order, and handle the return value correctly. You need to pass in both operands (random numbers) and the operator. Not some bizarre "max" parameter.
EDIT: There is almost certianly plenty more wrong, but you have some pretty fundamental issues to clean up here, so fix all the issues I mentioned first, then post back if you are still stuck, and we'll help you fix your program some more.