9 Replies - 792 Views - Last Post: 26 April 2012 - 03:31 PM Rate Topic: -----

#1 Waloo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-April 12

Computer Assisted Instruction (CIA) Program

Posted 26 April 2012 - 02:05 PM

Hi everybody ....
i should write a program help an elementary school student learn addition, subtraction,
division, multiplication.
the user choose the operation (An option of 1 means
addition problems only, 2 means subtraction problems only, 3 means multiplication
problems only and 4 means a random mixture of all these types, 5 for a combination of 1
through 4)
The student then inputs the answer. Next, the program checks the student’s answer. If it’s
correct, display the message "Very good!" and ask another multiplication question. If the
answer is wrong, display the message "No. Please try again." and let the student try the
same question repeatedly until the student finally gets it right.


any way out put must be

Posted Image



& i write a program but it doesn't work

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
int menu (void);
void arithmetic (void);
void correctMessege (void);
void incorrectMessege (void);

int main (void)
{
	int num1,num2;
	int operation;
	int result;

	srand( (unsigned int)time( 0 ) );
	menu ();
	scanf("%d", &operation );
	arithmetic ();

	if ( num1*num2 !=result || num1+num2 !=result || num1-num2 !=result || num1/num2 !=result )
	{
		incorrectMessege ();
	}
	if ( num1*num2 ==result || num1+num2 ==result || num1-num2 ==result || num1/num2 ==result )
	{
		correctMessege ();
	}


	getch ();
	return 0;
}
int menu (void)
{
	
	printf("Enter: 1 for addition, 2 for subraction\n");
	printf("Enter: 3 for multiplication, 4 for division\n");
	printf("Enter: 5 for combination of 1 throgh 4\n");
	printf("?");
	
	

	return 0;
}

void arithmetic (void)
{
	int num1;
	int num2;
	int operation;
	int result;
	int answer;

	

	num1 = rand() % 10; 
    num2 = rand() % 10;


	if (operation == 1) {
	         result = num1 + num2;
	         printf("How much is %d + %d?\n", num1, num2);
			 scanf_s("%d",& answer);
	        } 

	else if (operation == 2) {
	            result = num1 - num2;
	         printf("How much is %d - %d?\n", num1, num2);
			 scanf_s("%d",& answer);
			  } 

	else if (operation == 3) {
	            result = num1 * num2;
                printf("How much is %d * %d?\n", num1, num2);
				scanf_s("%d",& answer);


	} else if (operation == 4) {
				 result = num1 / num2;
            printf("How much is %d / %d?\n", num1, num2);
			scanf_s("%d",& answer);
	}
			return ;


}
void correctMessege (void){

switch ( 1+rand()%4 )
{
case 1:
printf( "Very good!");
break;
case 2:
printf( "Excellent!");
break;
case 3:
printf( "Nice work!");
break;
case 4:
printf( "Keep up the good work!");
break;
} 


}
void incorrectMessege (void)
{

switch ( 1+rand()%4 )
{
case 0:
printf( "No. Please try again.");
break;
case 1:
printf( "Wrong. Try once more.");
break;
case 2:
printf( "Don't give up!");
break;
case 3:
printf( "No. Keep trying.");
break;
}
}


what can i do ????


:code:

This post has been edited by r.stiltskin: 26 April 2012 - 02:11 PM
Reason for edit:: Added code tags. Please learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: Computer Assisted Instruction (CIA) Program

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Computer Assisted Instruction (CIA) Program

Posted 26 April 2012 - 02:13 PM

First, please learn to use code tags.

This Posted Image is an opening code tag. The whole thing. A pair of brackets [ ] with the word code between them. This tag goes before your code.

This Posted Image is a closing code tag. A pair of brackets [ ] with /code inside. This tag goes after your code.

************************************

Second, "it doesn't work" isn't good enough.

Exactly what is wrong? Describe your problem in detail. Do you have compiler errors that you don't understand? If so, what are they?

Do you not get the output you expected? What did you expect, and what did you get?

Help us to help you.
Was This Post Helpful? 0
  • +
  • -

#3 Waloo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-April 12

Re: Computer Assisted Instruction (CIA) Program

Posted 26 April 2012 - 02:48 PM

it 's work
but when i enter the number of operation
the prompt screen close and display this message
Posted Image

i think the error with {scanf ) statement =|
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Computer Assisted Instruction (CIA) Program

Posted 26 April 2012 - 03:00 PM

Well on line 12 you declared 2 variables, num1 and num2. And on line 21 you are trying to do mathematical operations with them, but you never assigned any values to those variables.

The problem is that the num1 and num2 that you declared in main() are not the same variables as the num1 and num2 that you declared in arithmetic(). When a variable is declared inside a function, it is usable only inside that function, unless you pass it or its address as the argument to another function. If your functions take no arguments, e.g. argument list (void), and you want to use the same variables in two functions, you have to make those variables global. Declare them before main(), outside of any function.
Was This Post Helpful? 1
  • +
  • -

#5 Waloo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-April 12

Re: Computer Assisted Instruction (CIA) Program

Posted 26 April 2012 - 03:11 PM

I do that, but it still appears the same problem ....
Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Computer Assisted Instruction (CIA) Program

Posted 26 April 2012 - 03:18 PM

Did you remove the declarations of num1 and num2 from main()?

Post your revised code.

You will also have to make result global.

And answer.
Was This Post Helpful? 1
  • +
  • -

#7 Waloo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-April 12

Re: Computer Assisted Instruction (CIA) Program

Posted 26 April 2012 - 03:19 PM

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
int menu (void);
void arithmetic (void);
void correctMessege (void);
void incorrectMessege (void);
	
int num1;
	int num2;

int main (void)
{
	int operation;
	int result;

	srand( (unsigned int)time( 0 ) );
	menu ();
	scanf("%d", &operation );
	arithmetic ();

	if ( num1*num2 !=result || num1+num2 !=result || num1-num2 !=result || num1/num2 !=result )
	{
		incorrectMessege ();
	}
	if ( num1*num2 ==result || num1+num2 ==result || num1-num2 ==result || num1/num2 ==result )
	{
		correctMessege ();
	}


	getch ();
	return 0;
}
int menu (void)
{
	
	printf("Enter: 1 for addition, 2 for subraction\n");
	printf("Enter: 3 for multiplication, 4 for division\n");
	printf("Enter: 5 for combination of 1 throgh 4\n");
	printf("?");
	
	

	return 0;
}

void arithmetic (void)
{
	
	int operation;
	int result;
	int answer;

	

	num1 = rand() % 10; 
    num2 = rand() % 10;


	if (operation == 1) {
	         result = num1 + num2;
	         printf("How much is %d + %d?\n", num1, num2);
			 scanf_s("%d",& answer);
	        } 

	else if (operation == 2) {
	            result = num1 - num2;
	         printf("How much is %d - %d?\n", num1, num2);
			 scanf_s("%d",& answer);
			  } 

	else if (operation == 3) {
	            result = num1 * num2;
                printf("How much is %d * %d?\n", num1, num2);
				scanf_s("%d",& answer);


	} else if (operation == 4) {
				 result = num1 / num2;
            printf("How much is %d / %d?\n", num1, num2);
			scanf_s("%d",& answer);
	}
			return ;


}
void correctMessege (void){

switch ( 1+rand()%4 )
{
case 1:
printf( "Very good!");
break;
case 2:
printf( "Excellent!");
break;
case 3:
printf( "Nice work!");
break;
case 4:
printf( "Keep up the good work!");
break;
} 


}
void incorrectMessege (void)
{

switch ( 1+rand()%4 )
{
case 0:
printf( "No. Please try again.");
break;
case 1:
printf( "Wrong. Try once more.");
break;
case 2:
printf( "Don't give up!");
break;
case 3:
printf( "No. Keep trying.");
break;
}
}



:code:

This post has been edited by r.stiltskin: 26 April 2012 - 03:21 PM
Reason for edit:: Added code tags. Please learn to use them.

Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Computer Assisted Instruction (CIA) Program

Posted 26 April 2012 - 03:27 PM

The same thing applies to all variables that you want to use in more than 1 function. So operation and result must be global. And I think you'll soon discover that you want answer to be global as well.
Was This Post Helpful? 0
  • +
  • -

#9 Waloo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-April 12

Re: Computer Assisted Instruction (CIA) Program

Posted 26 April 2012 - 03:29 PM

it work now i post (operation & result) variables to global too

Thanks alot for your help
Was This Post Helpful? 0
  • +
  • -

#10 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Computer Assisted Instruction (CIA) Program

Posted 26 April 2012 - 03:31 PM

It would also be nice if you would stop using proprietary functions like scanf_s (which only works for Microsoft compilers), and getch() (and everything in conio.h) which don't work on any Linux compilers.

You should use standard C functions like scanf or fscanf, and getchar() instead.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1