How to fix my Computer Assistance Instruction Program

For an assignment, I had to write a program in which it has to do the following:
Ask a student for the answer to 10 random arithmetic problems consisting of addition and subtraction using integers from 1 to 10 only. Pick two random integers from 1 to 10 and assign to num1 and num2 and pick a random integer 0 and 1 for the the operation OP. If OP is 0 it is addition, 1 is subtraction. If it is a subtraction you must make sure that the first number Num1 is bigger than the second number Num2. If it is not then swap the numbers by calling a function Swap that you will define. Each correct problem is awarded 10 points. Display the total score at the end of the problems.

I have written most of the code, but I'm not sure I'm doing it right and I need assistance/tips on how to finish the program since I've been having a bit of trouble doing so.

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
  //Computer Assisted Instruciton Program
#include<iostream>
#include<ctime>    
#include<cstdlib> 
using namespace std;
void Swap(int num1, int num2,int temp) 
{
	for(int Problem=1;Problem<=10;Problem++)
	{
		if(num1<num2)
		  {
		  	int temp;
		  	temp=num1;
		  	num1=num2;
		  	num2=temp;
		  }
	}
}
int main()
{
	int num1,num2,correctAns,OP,youAns,score=0;
	for(int Problem=1;Problem<=10;Problem++)
	   num1=1+rand()%10; //get first random number
	   num2=1+rand()%10; //get second random number

	   correctAns=0;
	   OP=rand()%2;
	   if(OP==0) //Addition
	   {
	      cout<<"What is"<<num1<<"+"<<num2<<"=";
	      cin>>youAns;
	      correctAns=num1+num2;
	      if(youAns=correctAns)
	      {
	      	cout<<"Correct!"<<endl;
	      	score+=10; //Add 10 pts to score 
	      }
	      else
	      cout<<"Incorrect"<<num1<<"+"<<num2<<"="<<youAns<<endl;
	      
	   if(OP==1) ///Subtraction)
	   {
	      cout<<"What is"<<num1<<"-"<<num2<<"=";
	      cin>>youAns;
	      correctAns=num1-num2;
		  
		  if(youAns=correctAns)
		  {
		  	cout<<"Correct!"<<endl;
		  	score+=10; //Add 10 pts to score
		  }
		  else
		  cout<<"Incorrect"<<num1<<"-"<<num2<<"="<<youAns<<endl;
		 	
	   }
You are on the right track, but there are a few mistakes.

(1) Fix your braces. Make sure you close all opened braces

(2) The program does not loop around ten times like it should. How can you fix this?

(3) if(youAns=correctAns) <---- thats an assignment statement

You want to ask if the two are equal. Therefore, you use a double equal sign, like you do in lines 28 and 41.

(4) If answer is incorrect, you should display the correct answer. So:
1
2
 else
cout<<"Incorrect"<<num1<<"-"<<num2<<"="<<correctAns<<endl;



Lets fix these issues first and we'll move on from there.
Last edited on
Hello, I have fixed my code, but I'm having trouble fixing my program so that it can loop around 10 times like it should. This is what I have been able to fix according to what you told me:

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
//Computer Assisted Instruciton Program
#include<iostream>
#include<ctime>    
#include<cstdlib> 
using namespace std;
void Swap(int num1, int num2,int temp); 
int main()
{
	int num1,num2,correctAns,OP,youAns,score=0;
	for(int Problem=1;Problem<=10;Problem++)
	{
		 num1=1+rand()%10; //get first random number
	     num2=1+rand()%10; //get second random number
		 correctAns=0;
	     OP=rand()%2;
    }
	   if(OP==0) //Addition
	   {
	      cout<<"What is"<<num1<<"+"<<num2<<"=";
	      cin>>youAns;
	      correctAns=num1+num2;
	   }
	      if(youAns==correctAns)
	      {
	      	cout<<"Correct!"<<endl;
	      	score+=10; //Add 10 pts to score 
	      }
	      else
	        cout<<"Incorrect"<<num1<<"+"<<num2<<"="<<correctAns<<endl;
	      
	      if(OP==1) ///Subtraction)
	      {
	        cout<<"What is"<<num1<<"-"<<num2<<"=";
		    cin>>youAns;
		    correctAns=num1-num2;
		  }
		  
		  if(youAns==correctAns)
		  {
		  	cout<<"Correct!"<<endl;
		  	score+=10; //Add 10 pts to score
		  }
		  else
		   cout<<"Incorrect"<<num1<<"-"<<num2<<"="<<correctAns<<endl;
		 	
 }
 
 void Swap(int num1, int num2, int temp)
{
	for(int Problem=1;Problem<=10;Problem++)
	{
		if(num1<num2)
		  {
		  	int temp;
		  	temp=num1;
		  	num1=num2;
		  	num2=temp;
		  }
	}
}
Last edited on
on this line:
if(youAns==correctAns)

"youAns" only gets set only if you enter your if(OP==0) condition. If it does not, the value of this variable will be garbage.

etc


edit:
i've just formatted your code:

1
2
3
4
5
6
7
8
9
10
11
12
13
        if(OP==0) //Addition
	{
		cout<<"What is"<<num1<<"+"<<num2<<"=";
		cin>>youAns;
		correctAns=num1+num2;
	}
	if(youAns==correctAns)
	{
		cout<<"Correct!"<<endl;
		score+=10; //Add 10 pts to score 
	}
	else
		cout<<"Incorrect"<<num1<<"+"<<num2<<"="<<correctAns<<endl;


it now looks evident you might want to nest the 2nd if and else inside your first 'if' maybe?
Last edited on
Topic archived. No new replies allowed.