Math Game Help :)

This is the code I have so far. I am suppose to create a math game that will allow users to practice there times tables. The only problem is the instructor wants it to start out with 1*2=.. then go all the way to 10, and the next set to be 2*1= etc. I have no clue how to make the 'ones' the only one that has to start out by multiplying itself by 2.
EX:
1*2 =
1*3 =
1*4 =
1*5 =
1*6 =
1*7 =
1*8 =
1*9 =
1*10 =
2*1 = etc..
3*1 = etc..

The code I have written starts all the first numbers out by 2.


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
/* 
Math Game! 
1 * 2 = users input
2 *1...
all the way through 10
*/

#include <iostream>
using namespace std;


int main ()
{
	int secondNumber, inputNumber, result, firstNumber = 1;
	int right = 0, wrong = 0;
	
	for(firstNumber = 1; firstNumber <=10; firstNumber++)
	{
	   for(secondNumber = 2; secondNumber <= 10; secondNumber++)
		{
      		  do
	            {
		      cout << firstNumber << " * " << secondNumber << " = ";
		      cin >> inputNumber;
		      result= firstNumber * secondNumber;
						
						

			if(inputNumber == result)
	     		  {
		  	    cout << "Correct!!\n";
   			    //right++;
			  }
			 else
		  	  {
			    cout << "Try again!\n";				
			    //wrong++;
	                    }
		    } while(inputNumber != result);
			      
	         }
	 }
	
	//cout << "Right: " << right << "\nWrong:" << wrong <<endl;
	system("pause");
	return 0;
}
Last edited on
No, your code is fine. Perhaps you need to rebuild your project, or change your prefetch settings (if you're running Vista, the prefetch "feature" can have adverse effects on debugging and rerunning your program).
I have no idea what you are talking about. There is something wrong with my code, or else it would run correctly?!
closed account (D80DSL3A)
You wish to skip the single case 1x1?
Let both loops go from 1 to 10 then insert this just before the do loop:
1
2
if( firstNumber==1 && secondNumber==1 )
    continue;
It's pretty easy. You've already defined firstNumber & secondNumber outside of the loops, so you can access them whenever you want.

In this case, a simple if conditional will be sufficient. Just make it check whether firstNumber is equal to 1. If so, assign the value of 2 to secondNumber; if not assign the value of 1 to secondNumber.
fun2code (646) Thanks! That worked just perfectly! Could you help with a break statement?
I need it to jump out of the outer loop if a negative number is inputted and exit the complete program if 0 is inputted. I have tried putting in an "if" loop with a break statement but it doesn't work. Could I use to goto or jump statement?
closed account (D80DSL3A)
You're welcome for the help.
About your new questions...
I need it to jump out of the outer loop if a negative number is inputted and exit the complete program if 0 is inputted.

Do you mean jump out of the inner loop (the for(secondNumber...) loop)? Jumping out of the outer loop ( for(firstNumber) ) would be leaving the program.
To jump out of the inner loop do this after line 24:
if( inputNumber < 0 )break;
To quit the program from within the inner loop you can go 2 ways.

1) Return directly from there:
if( inputNumber==0 )
{
system("pause");
return 0;
}
Some may frown on this method though.
2) Exit the inner loop with:
if( inputNumber <= 0 )break;
Then exit the outer loop with if( inputNumber == 0 )break; placed after line 41.
Topic archived. No new replies allowed.