I am learning c++ now and have encountered a problem in trying to create single digit addition, multiplication and subtraction math questions. The numbers used in the questions are randomly generated from 1 to 9 and when I try to print the answer for the corresponding question, I get a 0 for it which is not correct. The following are my codes. I would be very grateful if someone can give me some pointers on my code. If you have any suggestion in how I can improve my coding, I am very open to it. Thank you.

#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;

class onedQuestion
{protected:
 int ifirstOperand;
 int isecondOperand;
 int ianswer;
 public:
 //Constructor
 onedQuestion(int first=0, int second=0, int answer=0)
 :ifirstOperand(first),isecondOperand(second),ianswer(answer)
 {//cout<<"I am a onedQuestion"<<endl;
 }

 //Mutator
 void setOperands();

 //Accessor
 int getFirst(){return ifirstOperand;}
 int getSecond(){return isecondOperand;}
 virtual int getAnswer(){return (0);}

 //Display
 virtual void printQuestion()
 {cout<<"Please choose your operator!!"<<endl;}
 virtual void printAnswer()
 {cout<<"Ans: "<<ianswer;}
};
/*---------------------------------------------------------*/
void onedQuestion::setOperands()
{ ifirstOperand=1+rand()%9;
  isecondOperand=1+rand()%9;
}
/***********************************************************/
class onedAddition: public onedQuestion
{public:
 //Constructor
 onedAddition(int first=0, int second=0, int answer=0)
 :onedQuestion(first, second, answer)
 {//cout<<"I am a onedAddition"<<endl;
 } 

 //Accessor
 int getAnswer(){return ifirstOperand+isecondOperand;}

 //Display
 void printQuestion()
 {cout<<ifirstOperand<<" + "<<isecondOperand<<" = ";
 }
  
  void printAnswer()
 {cout<<"Ans: "<<ianswer;}

};
/***********************************************************/
class onedMultiplication: public onedQuestion
{public:
 //Constructor
 onedMultiplication(int first=0, int second=0, int answer=0)
 :onedQuestion(first, second, answer)
 {//cout<<"I am a onedMultiplication"<<endl;
 } 

 //Accessor
 int getAnswer(){return ifirstOperand*isecondOperand;}

 //Display 
 void printQuestion()
 {cout<<ifirstOperand<<" x "<<isecondOperand<<" = ";
 }
 
  void printAnswer()
 {cout<<"Ans: "<<ianswer;}
};

/***********************************************************/
class onedSubtraction: public onedQuestion
{public:
 //Constructor
 onedSubtraction(int first=0, int second=0, int answer=0)
 :onedQuestion(first, second, answer)
 {//cout<<"I am a onedSubtraction"<<endl;
 } 

 //Accessor
 int getResult(){return ifirstOperand-isecondOperand;}

 //Display 
 void printQuestion()
 {cout<<ifirstOperand<<" - "<<isecondOperand<<" = ";
 }
 
  void printAnswer()
 {cout<<"Ans: "<<ianswer;}
};
/***********************************************************/
//Functions
 void genQuestion(onedQuestion &question)
{question.setOperands();
 question.printQuestion();
}

 void genAnswer(onedQuestion &inanswer)
{ inanswer.getAnswer();
  inanswer.printAnswer();
}
/***********************************************************/
int main()
{srand((unsigned)time(0));
 
 onedAddition add;
 onedSubtraction minus;
 onedMultiplication times;

 cout<<"Generating 20 addition problems"<<endl;
 cout<<endl; 
 for(int i=0;i<20;i++)
 { cout<<i+1<<") ";
   genQuestion(add);
   cout<<setw(30);
   genAnswer(add);
   cout<<endl;
   }

 cout<<"Generating 20 subtraction problems"<<endl; 
 cout<<endl;
 for(int i=0;i<20;i++)
 {cout<<i+1<<") "; 
  genQuestion(minus);
   cout<<setw(30);
   genAnswer(minus);
   cout<<endl;
   }

 cout<<"Generating 20 multiplication problems"<<endl; 
 cout<<endl;
 for(int i=0;i<20;i++)
 {cout<<i+1<<") "; 
  genQuestion(times);
  cout<<setw(30);
   genAnswer(times);
   cout<<endl;
   }

return 0;
}

Output:
Generating 20 addition problems

1) 9 + 6 = Ans: 0
2) 8 + 9 = Ans: 0
3) 8 + 5 = Ans: 0
4) 7 + 1 = Ans: 0
5) 4 + 3 = Ans: 0
6) 4 + 7 = Ans: 0
7) 1 + 6 = Ans: 0
8) 2 + 8 = Ans: 0
9) 3 + 8 = Ans: 0
10) 9 + 5 = Ans: 0
11) 9 + 5 = Ans: 0
12) 1 + 2 = Ans: 0
13) 7 + 9 = Ans: 0
14) 7 + 8 = Ans: 0
15) 4 + 7 = Ans: 0
16) 1 + 4 = Ans: 0
17) 1 + 6 = Ans: 0
18) 1 + 9 = Ans: 0
19) 8 + 7 = Ans: 0
20) 7 + 9 = Ans: 0
Generating 20 subtraction problems

1) 1 - 1 = Ans: 0
2) 6 - 8 = Ans: 0
3) 7 - 7 = Ans: 0
4) 6 - 7 = Ans: 0
5) 3 - 4 = Ans: 0
6) 1 - 1 = Ans: 0
7) 6 - 8 = Ans: 0
8) 2 - 1 = Ans: 0
9) 8 - 9 = Ans: 0
10) 9 - 2 = Ans: 0
11) 6 - 7 = Ans: 0
12) 3 - 6 = Ans: 0
13) 3 - 4 = Ans: 0
14) 3 - 1 = Ans: 0
15) 8 - 1 = Ans: 0
16) 7 - 8 = Ans: 0
17) 8 - 2 = Ans: 0
18) 4 - 5 = Ans: 0
19) 8 - 1 = Ans: 0
20) 3 - 9 = Ans: 0
Generating 20 multiplication problems

1) 2 x 3 = Ans: 0
2) 9 x 7 = Ans: 0
3) 1 x 1 = Ans: 0
4) 7 x 6 = Ans: 0
5) 7 x 6 = Ans: 0
6) 6 x 1 = Ans: 0
7) 3 x 8 = Ans: 0
8) 7 x 3 = Ans: 0
9) 9 x 7 = Ans: 0
10) 1 x 8 = Ans: 0
11) 7 x 8 = Ans: 0
12) 4 x 6 = Ans: 0
13) 9 x 8 = Ans: 0
14) 8 x 5 = Ans: 0
15) 6 x 8 = Ans: 0
16) 4 x 7 = Ans: 0
17) 1 x 1 = Ans: 0
18) 2 x 9 = Ans: 0
19) 9 x 8 = Ans: 0
20) 3 x 6 = Ans: 0

void genAnswer(onedQuestion &inanswer)
{ inanswer.getAnswer();
  inanswer.printAnswer();
}

getAnswer() function return the value ay adding the operand but you did't store into the ianswer variable and secondly use same name for member variable as well as object which is not good try to eliminate this first

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.