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, but here is what I wrote:
Code:
//Computer Assisted Instruction 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;
}
}
}