|
-
February 20th, 2012, 02:28 AM
#26
Re: C++/C conversion to just C
at the beginning of main when i declare all of those ints like "int choice" I right clicked them and selected find implementation/declaration and it says choice is not found, along with all the others.
Code:
/*---------------------------------------------------------------
* Programmer: Charles Cunningham
* Date: 2/5/2012
* Name: Hw#1
* Description: This program generates addition or subtraction problems
based on the users choice. Then the user must guess the correct answer. If correct the user gets a point added to his score
---------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int menu();
void addition(int *Preward); // function to return truth values
void subtraction(int *Preward);
int main()
{
int Finished = 0;
int choice, score = 0;
int reward = 0;
while(Finished != 1); // while finished is not true
{
printf("\nscore: %d", score); // print the score
{
choice = menu(); // calls for user input and enters it to switch
switch (choice){ // accesses the functions
case 0: Finished = 1; break;
case 1: addition(&reward); break;
case 2: subtraction(&reward); break;
default : printf("Error in input\n\n"); break;
}
if(reward == 1){
score += reward; // score = score + reward
}
}
}
return 0;
}
int menu() //main menu that gets called and allows fr user input
{
int MenuChoice;
printf("\n");
printf("Enter 1 for addition\n");
printf("Enter 2 for Subtraction\n");
printf("Enter 0 to quit\n");
printf("-----------------\n");
printf("Choice: ");
scanf("%d", &MenuChoice); // user input
return MenuChoice; //return the choice to choice to select a case
}
void addition(int *Preward) //function that is called by the users selection
{
int answer;
int random1, random2;
srand(time(NULL)); //generates fresh numbers each time.
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99
printf("\n%d - %d = ", random1, random2); //code to format the random math problem
scanf("%d", &answer); // user input of answer
{
if (answer == (random1+random2)){
printf("Congratulations\n\n");
*Preward = 1;// tellsesult whether to add a pt or not
} //if user is correct than the score is incremented
else{
printf("Incorrect\n\n");} //if not than the score is not incremented
}
}
void subtraction(int *Preward) // follows same model as previous function, but instead subtraction.
{
int answer;
int random1, random2;
srand(time(NULL));
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99
printf("\n%d - %d = ", random1, random2);
scanf("%d", &answer);
if (answer == (random1-random2)){
printf("Congratulations\n\n");
*Preward = 1;// tells result whether to add a pt or not
}
else{
printf("Incorrect\n\n");
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|