Trouble with my Computer Assistance Instruction Program

My output for my assignment is supposed to be something like this:
What is 10+5=? 7

Incorrect: 10+5=15

What is 9-4=? 5

Correct!

(etc)

Your total score is ?

I have to 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.

My program isn't doing that, I'm not sure what I'm doing wrong in my code.


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
Your swap function isn't doing what you want. If you pass in the numbers by value, they will not reflect the change when the function returns to main() You need to take 2 numbers (references, so they will remain after function return) and swap their values:

1
2
3
4
5
6
void swap(int &n1, int &n2)
{
  int tmp = n1;
  n1 = n2;
  n2 = tmp;
}


You also need to actually check if num1 is greater than num2 before you assign correctAns.

1
2
if(num1 < num2) swap(num1,num2);
correctAns=num1-num2; // because of the above num1 will always be > num2 

I placed the second code you sent me here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
          if(num1 < num2) 
          {
		    Swap(num1,num2);
            correctAns=num1-num2; // because of the above num1 will always be > num2
	      }

but im getting this error:
- undefined reference to `Swap(int, int)'
-[Error] ld returned 1 exit status
Topic archived. No new replies allowed.