Having problems w/ a math quiz program

math program for elementry school kids

Page 1 of 1

4 Replies - 3912 Views - Last Post: 27 April 2009 - 07:27 AM Rate Topic: -----

#1 smu5014  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-April 09

Having problems w/ a math quiz program

Post icon  Posted 27 April 2009 - 06:32 AM

I am having alot of problems getting my code for my program to run. I have tried several different ways to get it to run correctly but I can not get it to run the way that it is suppose to. This is a program that will quiz elementry aged kids with their math facts of +, - and mult. It is suppose to randomly display 5 math problems but the difficulties I am having is the following:

1. I can not get it to stop at the 5 problems. it goes 10-12 times instead. also with the 5 problems, they are suppose to be randomized as far as the math problems. but it is running in the order that i placed them. such as i have in the code first addition then subtraction and then multiplication. it is suppose to output the problems in a random order.

2. in the subtraction problems, i can not get the problem to show the first number as the greater number to avoid negative numbers

3. at the end of the program, it is suppose to tell you your score out of the 5 problems displaying a randomized message depending on the score. But it keeps that all were correct even if i put in B.S answers that are no way correct.

Can anyone help me PLEASE!! I have included a copy of the cpp file as a .doc file due to it being long.. So hopefully someone can help me

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Having problems w/ a math quiz program

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Having problems w/ a math quiz program

Posted 27 April 2009 - 06:40 AM

Don't use attachments.

They are rarely opened and reduce your chance of getting help

This is homework isn't it?

Here is your code.
/* Source file name:  project4-5.cpp
Written by Shannon Umstead on 27 APRIL 2009
This program has been designed to run a math quiz game for elementary school children to test their addition, subtraction and multiplication facts.   
*/

#include <iostream>
#include <ctime>
#include <cmath>
#include <cstdlib>

using namespace std;

int top, bottom;							// for subtraction comparison

int main ()
{

int count;									// the problem number
int correct;								// the correct answer passed by reference
int answer;									// the users answer

cout << "WELCOME TO MATH QUIZZER" << endl;
cout << "You will be tested with 5 different math problems." <<endl;
cout << "Answer each problem the best that you can and then hit the ENTER button on your keyboard." << endl;
cout << "Let's begin."<<endl;

// start of math problem functions

for(count = 1; count < 5; count = count++)	
{

//addition function
int num1, num2;		
srand(static_cast<int>(time(0)));

// Initialize num1 and num2 to two random numbers
num1 = rand() % 21;
num2 = rand() % 21;
correct = num1 + num2;

cout << "   " << num1 <<endl;
cout << "+ " << num2 << endl;
cout << "-------" << endl;
cin >> answer;

if (answer == correct)
	{
		cout << "CORRECT, YOU KNOW YOUR STUFF" << endl;
	correct = (correct + 1);
	}
	else
	{
		cout <<  "SORRY BUT GOOD TRY. THE CORRECT ANSWER WAS "<< correct << endl;
}

//subtraction function
int num3, num4;		
srand(static_cast<int>(time(0)));
int top, bottom;							// for subtraction comparison 

// Initialize num3 and num4 to two random numbers
num3 = rand() % 21;
num4 = rand() % 21;

cout << "   " << num3 << endl;
cout << "- " << num4 << endl;
cout << "-------" << endl;

if (num3 > num4) 
{ 
  correct = num3 - num4; 
  top = num3; 
  bottom = num4; 
} 
else 
{ 
  correct = num4 - num3; 
  top = num4; 
  bottom = num3; 
} 

cin >> answer;

if (answer == correct)
	{
		cout << "THAT'S IT, GREAT JOB!" << endl;
	correct = (correct + 1);
}
else
	{
		cout << "GOOD TRY BUT THAT IS INCORRECT. THE CORRECT ANSWER WAS "<< correct << endl;
}


//multiplication function
int num5, num6;		
srand(static_cast<int>(time(0)));

// Initialize num5 and num6 to two random numbers
num5 = rand() % 11;
num6 = rand() % 11;
correct = num5 * num6;


cout << "   " << num5 << endl;
cout << "* " << num6 << endl;
cout << "-------" << endl;
cin >> answer;

if (answer == correct)
	{
		cout << "THAT IS THE CORRECT ANSWER! TERRIFIC WORK" << endl;
		correct = (correct + 1);
}
else
	{
		cout << "THAT WAS A GREAT TRY. THE CORRECT ANSWER WAS "<< correct << endl;

}

//result of the users score

if (correct == 5)
	{
		cout << "SMARTIE PANTS!! YOU GOT THEM ALL RIGHT" << endl;
}
else if (correct == 4)
	{
		cout << "ONLY ONE WRONG, GREAT JOB!" << endl;
}
else if (correct == 3)
	{
		cout << "I THINK THAT YOU CAN DO BETTER, PLAY THE GAME AGAIN SOON." << endl;
}
else
	{	
		cout << "GOOD TRY! KEEP WORKING HARDER KIDDO AND YOU WILL DO BETTER NEXT TIME." << endl;
}

return 0;
}
}



I have added a missing bracket to the end of the code so it will compile and have this compiler warning to share with you
DIC.cpp:29: warning: operation on ‘count’ may be undefined
This is telling you to change this:
for(count = 1; count < 5; count = count++)


to this
for( int count = 1; count < 5; count = count++ )	



I'm not looking any deeper because your code formatting is too awful for me to look at while I am this tired.
There is value in using correct and consistent brace placement and indentation.
It makes your code easier for you to understand and increases the chances others will help you with it.

Good luck
Hope others are fresher and provide you more help.

This post has been edited by janotte: 27 April 2009 - 06:50 AM

Was This Post Helpful? 0
  • +
  • -

#3 smu5014  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-April 09

Re: Having problems w/ a math quiz program

Posted 27 April 2009 - 06:49 AM

Thank you. I was not sure


View Postjanotte, on 27 Apr, 2009 - 05:40 AM, said:

Don't use attachments.

They are rarely opened and reduce your chance of getting help

Here is your code.
/* Source file name:  project4-5.cpp
Written by Shannon Umstead on 27 APRIL 2009
This program has been designed to run a math quiz game for elementary school children to test their addition, subtraction and multiplication facts.   
*/

#include <iostream>
#include <ctime>
#include <cmath>
#include <cstdlib>

using namespace std;

int top, bottom;							// for subtraction comparison

int main ()
{

int count;									// the problem number
int correct;								// the correct answer passed by reference
int answer;									// the users answer

cout << "WELCOME TO MATH QUIZZER" << endl;
cout << "You will be tested with 5 different math problems." <<endl;
cout << "Answer each problem the best that you can and then hit the ENTER button on your keyboard." << endl;
cout << "Let's begin."<<endl;

// start of math problem functions

for(count = 1; count < 5; count = count++)	
{

//addition function
int num1, num2;		
srand(static_cast<int>(time(0)));

// Initialize num1 and num2 to two random numbers
num1 = rand() % 21;
num2 = rand() % 21;
correct = num1 + num2;

cout << "   " << num1 <<endl;
cout << "+ " << num2 << endl;
cout << "-------" << endl;
cin >> answer;

if (answer == correct)
	{
		cout << "CORRECT, YOU KNOW YOUR STUFF" << endl;
	correct = (correct + 1);
	}
	else
	{
		cout <<  "SORRY BUT GOOD TRY. THE CORRECT ANSWER WAS "<< correct << endl;
}

//subtraction function
int num3, num4;		
srand(static_cast<int>(time(0)));
int top, bottom;							// for subtraction comparison 

// Initialize num3 and num4 to two random numbers
num3 = rand() % 21;
num4 = rand() % 21;

cout << "   " << num3 << endl;
cout << "- " << num4 << endl;
cout << "-------" << endl;

if (num3 > num4) 
{ 
  correct = num3 - num4; 
  top = num3; 
  bottom = num4; 
} 
else 
{ 
  correct = num4 - num3; 
  top = num4; 
  bottom = num3; 
} 

cin >> answer;

if (answer == correct)
	{
		cout << "THAT'S IT, GREAT JOB!" << endl;
	correct = (correct + 1);
}
else
	{
		cout << "GOOD TRY BUT THAT IS INCORRECT. THE CORRECT ANSWER WAS "<< correct << endl;
}


//multiplication function
int num5, num6;		
srand(static_cast<int>(time(0)));

// Initialize num5 and num6 to two random numbers
num5 = rand() % 11;
num6 = rand() % 11;
correct = num5 * num6;


cout << "   " << num5 << endl;
cout << "* " << num6 << endl;
cout << "-------" << endl;
cin >> answer;

if (answer == correct)
	{
		cout << "THAT IS THE CORRECT ANSWER! TERRIFIC WORK" << endl;
		correct = (correct + 1);
}
else
	{
		cout << "THAT WAS A GREAT TRY. THE CORRECT ANSWER WAS "<< correct << endl;

}

//result of the users score

if (correct == 5)
	{
		cout << "SMARTIE PANTS!! YOU GOT THEM ALL RIGHT" << endl;
}
else if (correct == 4)
	{
		cout << "ONLY ONE WRONG, GREAT JOB!" << endl;
}
else if (correct == 3)
	{
		cout << "I THINK THAT YOU CAN DO BETTER, PLAY THE GAME AGAIN SOON." << endl;
}
else
	{	
		cout << "GOOD TRY! KEEP WORKING HARDER KIDDO AND YOU WILL DO BETTER NEXT TIME." << endl;
}

return 0;
}
}



I have added a missing bracket to the end of the code so it will compile and have this compiler warning to share with you
DIC.cpp:29: warning: operation on ‘count’ may be undefined
This is telling you to change this:
for(count = 1; count < 5; count = count++)


to this
for( int count = 1; count < 5; count = count++ )	



I'm not looking any deeper because your code formatting is too awful for me to look at while I am this tired.
There is value in using correct and consistent brace placement and indentation.
It makes your code easier for you to understand and increases the chances others will help you with it.

Good luck
Hope others are fresher and provide you more help.

Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Having problems w/ a math quiz program

Posted 27 April 2009 - 07:12 AM

I would suggest that you fix the problem I pointed to and go through your code and fix the formatting and bracing to make the formatting as good as you can manage and then reposting your code.
:code:

Not only are you likely to find mistakes yourself while giving your code a good scrub, but the easier you make it for others to read your code the easier it is for people to help you.

This post has been edited by janotte: 27 April 2009 - 07:40 AM

Was This Post Helpful? 0
  • +
  • -

#5 stefaan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-November 08

Re: Having problems w/ a math quiz program

Posted 27 April 2009 - 07:27 AM

If you want the problems to be randomized place them in do{} while() loop and use case. Validate the random number so they dont repeat, ie. if you used case 3 dont allow the program to use it again. Or you could just place 1-5 in an array and shuffle it each time the program runs.
To display the larger number in subtraction, use an if statement to choose between the larger number
If num1>num2, output this order, else output next order.
Hope this helps.
And please take what Janotte said seriously, it will improve your debugging skills and help others to help you more.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1