Hi, my name is Anita. I'm new to this forum and I'm here because I am utterly frustrated with this program that I have been working on for days. This program is supposed to ask the user a series of arithmetic problems and report on how the user performs. The way we are supposed to go about doing this is broken into phases.(I will put exactly what the assignment says at the end) Anyway, I've got two major problems right now. I got up to phase III, and then could NOT figure out how to change the parameters of my doOneSet function to do addition, subtraction and multiplication. My second problem is that up until that point my program worked when I used the g++ compiler on the Mac computers at school. But when I tried to compile the same exact .cpp file using DevC++ at home, it's giving me weird long numbers. The problem with this assignment is how she wants it done.(see end of post)

//***************************************************************************
//This program asks a series of arithmetic problems
//and reports the user's performance
//***************************************************************************
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
using namespace std;

void doOneSet();
void doOneProblem();
void generateOperands(int &num1, int &num2);
void calcCorrectAnswer(int &result, int &userNum1);
void checkAnswer(int result, int userNum1);


int main()
{
    srand(time(0));
    doOneSet();


system("pause");
return 0;
}
//***************************************************************************
void doOneSet()
{
	
	doOneProblem();
	doOneProblem();
	doOneProblem();
	doOneProblem();
	doOneProblem();
}
//***************************************************************************
void doOneProblem()
{
	int num1, num2, result, userNum1;
	
	generateOperands(num1, num2);
	calcCorrectAnswer(result, userNum1);
	checkAnswer(result, userNum1);
	
}
//***************************************************************************
void generateOperands(int &num1, int &num2)
{
	
	
    num1=rand()%101;
    num2=rand()%101;
}
//***************************************************************************

void calcCorrectAnswer(int &result, int &userNum1)
{
	int num1, num2;
	
		cout<<num1<<"+"<<num2<<"=";                   //To make arithmetic problem
		  cin>>userNum1;
		  result=num1+num2;

}
//**************************************************************************
void checkAnswer(int result, int userNum1)
{
	
			if(userNum1==result)
			{
			 cout<<"correct"<<endl;
			}
			else
			{
			 cout<<"incorrect"<<endl;
			}
}

Here is a link to my assignment. It's a .pdf file so I couldn't just copy and paste it here.
http://docs.google.com/fileview?id=0B05IOh-iexZwYTY0NWRmNzMtOGYyZi00NTA4LWE4YTgtYzNkN2E5M2ZmMGY5&hl=en

Thanks for any help!

Recommended Answers

All 7 Replies

For the first problem you could put some if cases inside doOneSet( ) to catch the character passed into it, '+', '-' or '*'.
Then depending on what the character was do the appropriate calculation.

line 61 in the code you posted: That function displays random/wrong values because it is using uninitialized variables.

and then could NOT figure out how to change the parameters of my doOneSet function to do addition, subtraction and multiplication

There are many ways to go about this. Since you seem interested in changing the parameter list of your doOneSet() function... maybe we could do something like this (ok, I changed the parameter list of your doOneProblem() function instead.. it was easier for me)

void doOneSet()
{
     //simple loop will call the doOneProblem() function 5 times
     //instead of having to write out each individual function call
     for(int i=0; i<5; i++)
     {
      //Here we will supply a random number:  0,1 or 2
      //we'll make 0 = add, 1 = subtract, and 2 = multiply       
      doOneProblem(rand%3);    
     } 
}

Now when you get down to calcCorrectAnswer(), you could have something like this:

void calcCorrectAnswer(int &result, int &userNum1, int& operation)
{

     int num1, num2;

     switch(operation)
     {
          //addition
          case 0:  cout << num1 << " + " << num2 << " = ";
                   cin >> userNum1;
                   result = num1 + num2;
                   break;
          //subraction
          case 1:  cout << num1 << " - " << num2 << " = "; 
                   cin >> userNum1;
                   result = num1 - num2;
                   break;
          //multiplication
          case 2:  cout << num1 << " * " << num2 << " = ";
                   cin >> userNum1;
                   result = num1 * num2;
                   break;

          default:  cout << "Error..!";
     }
    
     checkAnswer(result, userNum1);
}

Even if this isn't exactly what you want, feel free to modify it as you like.. it's mainly here just to give you an idea of what you can do in efforts to resolve your problem.

This is all untested/uncompiled code and may contain easy to fix errors. If anyone has a different approach to this problem please feel free to share with the crowd.

@ Ancient Dragon: I fixed what you said and some other similar issues. Thanks. So now my code works and I'm at the same point.
@ Clinton Portis: the way my professor wants the main function to look is this:

int main()
{
    srand(time(0));
    doOneSet('+');
    doOneSet('-');
    doOneSet('*');
}

The thing is, she went on maternity leave and my class went from being a regular class to an online, we gotta teach ourselves kind of class. My question at this point is by putting the +,-,* inside the () in my main function, do I need to add some kind of char parameter to doOneSet? I want to eventually put a switch in my calcCorrectAnswer function. My ultimate goal at this point is to create 5 random addition problems, then 5 random subtraction problems and 5 random multiplication problems. Below is my fixed code that only writes out the 5 addition problems. I am going to keep working on it to see what approach I would take to go forward. Any pointers? Thanks!

//***************************************************************************
//This program asks a series of arithmetic problems
//and reports the user's performance
//***************************************************************************
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
using namespace std;

void doOneSet();
void doOneProblem();
void generateOperands(int &num1, int &num2);
void calcCorrectAnswer(int num1, int num2, int &result, int &userNum1);
void checkAnswer(int result, int userNum1);


int main()
{
    srand(time(0));
    doOneSet();



return 0;
}
//***************************************************************************
void doOneSet()
{
    
    doOneProblem();
    doOneProblem();
    doOneProblem();
    doOneProblem();
    doOneProblem();
}
//***************************************************************************
void doOneProblem()
{
    int num1, num2, result, userNum1;
    
    generateOperands(num1, num2);
    calcCorrectAnswer(num1, num2, result, userNum1);
    checkAnswer(result, userNum1);
    
}
//***************************************************************************
void generateOperands(int &num1, int &num2)
{
    
    
    num1=rand()%101;
    num2=rand()%101;
}
//***************************************************************************

void calcCorrectAnswer(int num1, int num2, int &result, int &userNum1)
{

    
        cout<<num1<<"+"<<num2<<"=";                   //To make arithmetic problem
          cin>>userNum1;
          result=num1+num2;

}
//**************************************************************************
void checkAnswer(int result, int userNum1)
{
    
            if(userNum1==result)
            {
             cout<<"correct"<<endl;
            }
            else
            {
             cout<<"incorrect"<<endl;
            }
}

There are many ways to go about this. Since you seem interested in changing the parameter list of your doOneSet() function... maybe we could do something like this (ok, I changed the parameter list of your doOneProblem() function instead.. it was easier for me)

void doOneSet()
{
     //simple loop will call the doOneProblem() function 5 times
     //instead of having to write out each individual function call
     for(int i=0; i<5; i++)
     {
      //Here we will supply a random number:  0,1 or 2
      //we'll make 0 = add, 1 = subtract, and 2 = multiply       
      doOneProblem(rand%3);    
     } 
}

Now when you get down to calcCorrectAnswer(), you could have something like this:

void calcCorrectAnswer(int &result, int &userNum1, int& operation)
{

     int num1, num2;

     switch(operation)
     {
          //addition
          case 0:  cout << num1 << " + " << num2 << " = ";
                   cin >> userNum1;
                   result = num1 + num2;
                   break;
          //subraction
          case 1:  cout << num1 << " - " << num2 << " = "; 
                   cin >> userNum1;
                   result = num1 - num2;
                   break;
          //multiplication
          case 2:  cout << num1 << " * " << num2 << " = ";
                   cin >> userNum1;
                   result = num1 * num2;
                   break;

          default:  cout << "Error..!";
     }
    
     checkAnswer(result, userNum1);
}

Even if this isn't exactly what you want, feel free to modify it as you like.. it's mainly here just to give you an idea of what you can do in efforts to resolve your problem.

This is all untested/uncompiled code and may contain easy to fix errors. If anyone has a different approach to this problem please feel free to share with the crowd.

What you are suggesting is very possible, just change the desired function to accept a char parameter. Then at some point, you can use if/else logic based on that char argument to determine what type of problem ye' wish to produce.

Thanks to everyone who has input help so far! I really appreciate it. Anyway, I'm almost finished but this last part is not working. It is my printReport function that is supposed to give a report of the user's overall performance. However, my set1Correct, set2Correct, and set3Correct variables always come out to be 0. It's like they aren't getting the value from my checkAnswer function.
This is my main function

int main()
{
    int probsPerSet;
    int set1Correct=0;
    int set2Correct=0;
    int set3Correct=0;
    
    srand(time(0));
    getProbsPerSet(probsPerSet);
    doOneSet('+',probsPerSet);
    doOneSet('-',probsPerSet);
    doOneSet('*',probsPerSet);
    printReport(probsPerSet, set1Correct, set2Correct, set3Correct);


system("pause");
return 0;
}

This is doOneSet function. I think I need to add another parameter here...

void doOneSet(char problemType, int probsPerSet)
{
    int maxNum;
     
    printHeader(problemType);
    getMaxNum(maxNum);
	for (int i=0;i<probsPerSet;i++)
	{
	 doOneProblem(problemType,probsPerSet,maxNum);
    }

}

This is my doOneProblem function which may be where the problem is.

void doOneProblem(char problemType, int probsPerSet, int maxNum)
{
	int num1, num2, result, userNum1, set1Correct, set2Correct, set3Correct;
	
	switch (problemType)
	{
     case '+':generateOperands(num1, num2, maxNum);
              calcCorrectAnswer(num1, num2, result, userNum1, problemType);
	          checkAnswer(result, userNum1, problemType,set1Correct,set2Correct,set3Correct,num1,num2);
	          break;
     case '-':generateOperands(num1, num2, maxNum);
              calcCorrectAnswer(num1, num2, result, userNum1, problemType);
	          checkAnswer(result, userNum1, problemType,set1Correct,set2Correct,set3Correct,num1,num2);
	          break;
     case '*':generateOperands(num1, num2, maxNum);
              calcCorrectAnswer(num1, num2, result, userNum1, problemType);
	          checkAnswer(result, userNum1, problemType,set1Correct,set2Correct,set3Correct,num1,num2);
	          break;
     }

	
}

My checkAnswer function: or maybe this is the problem. lol!

void checkAnswer(int result, int userNum1, char problemType, int &set1Correct, int &set2Correct, int &set3Correct, int num1, int num2)
{	
			if(userNum1==result)
			{
			 cout<<"correct"<<endl;	 
			}
			else
			{
			 cout<<"incorrect"<<endl;
			}
			
	        switch (problemType)
	        {
            case '+': if (result=num1+num2)
                      {
                       set1Correct=set1Correct+1;
                       }
	                   break;
            case '-':if (result=num1-num2)
                      {
                       set2Correct=set2Correct+1;
                       }
	                   break;
            case '*':if (result=num1*num2)
                      {
                       set3Correct=set3Correct+1;
                       }
	                   break;
            }

}

And last but not least, the printReport function:

void printReport(int getProbsPerSet, int set1Correct, int set2Correct, int set3Correct)
{
     int totalProbs=getProbsPerSet*3;
     int totalProbsCorrect=set1Correct+set2Correct+set3Correct;
     float set1Percentage=set1Correct/getProbsPerSet*100;
     float set2Percentage=set2Correct/getProbsPerSet*100;
     float set3Percentage=set3Correct/getProbsPerSet*100;
     float totalPercentage=totalProbsCorrect/getProbsPerSet*3;
     
     cout<<endl;
     cout<<"Set #1: You got "<<set1Correct<<" out of "<<getProbsPerSet<<"for ";
     cout<<set1Percentage<<"%"<<endl;
     
     cout<<"Set #2: You got "<<set2Correct<<" out of "<<getProbsPerSet<<"for ";
     cout<<set2Percentage<<"%"<<endl;
     
     cout<<"Set #3: You got "<<set3Correct<<" out of "<<getProbsPerSet<<"for ";
     cout<<set3Percentage<<"%"<<endl;
}

Once again, thanks for any pointers.

Hi NitaB.
I think you should pass the variables set1Correct, set2Correct and set3Correct by reference from the very beginning of the program in main, that is in function printReport.

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.