1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
void add( void ) //function header add
{ //start add
system("cls");
//local identifiers
int number1;
int number2;
int answer = 0;
int correctanswer;
int counter;
int returnvalue;
//random number generator
srand((unsigned)time(NULL));
number1 = rand() % 50;
srand((unsigned)time(NULL));
number2 = rand() % 50;
//equation so the program has a number to compare to the users answer
correctanswer = number1 + number2;
for (counter = 1; counter <= 5; counter++) //initialization, repetition condition, and increment
{ //open for
//random number generator
srand((unsigned)time(NULL));
number1 = 1 + rand() %50;
srand((unsigned)time(NULL));
number2 = 1 + rand() %50;
//creating a correct answer
correctanswer = number1 + number2;
//equation for player one
printf("\n%s Add: %d + %d: ", player1name, number1, number2, answer);
returnvalue = scanf("%d", &answer); //users input
//fixing the problem when a letter is entered
if(returnvalue < 1) {
while(returnvalue != '\n') {
printf("%c", returnvalue);
returnvalue = getchar();
} //end while
} //end if
//determining if the answer is right or not
if ( correctanswer == answer )
{ //open if
correct(); //function call to correct
player1score++; //adding point to player one
} //close if
else
{// open else
incorrect(); //function call to incorrect
} //close else
//random number generater
srand((unsigned)time(NULL));
number1 = 1 + rand() %50;
srand((unsigned)time(NULL));
number2 = 1 + rand() %50;
//creating a correct answer
correctanswer = number1 + number2;
//equation for player two
printf("\n\n%s Add: %d + %d: ", player2name, number1, number2, answer);
returnvalue = scanf("%d", &answer);
//fixing the problem when a letter is entered
if(returnvalue < 1) {
while(returnvalue != '\n') {
printf("%c", returnvalue);
returnvalue = getchar();
} //end while
} //end if
//determine if player 2 got it right or wrong
if ( correctanswer == answer )
{ //open if
correct(); //function call to correct
player2score++; //adding a point to player one's score
} //close if
else
{// open else
incorrect(); //function call to incorrect
} //close else
} //close for
system("cls"); //clear screen
return; //return to function main
} //end add
|