You are here:

C++/not sure how to do it

Advertisement


Question
dear Zlatko,

My teacher has given us a program to write involving void functions, I have the program 90% complete however she wants a void function for the equations, her exact words when I asked what exactly she wanted were.
"One function that will work for all 4 situations.  The parts of the equation that change (title, symbol, numbers, etc...) should be sent to the function as arguments."
with out any direction or even an example I am lost, here is the code I have and hope fully you can at least give me a push in the right direction. Not looking for someone to do my homework, just some help.

/*
 Programer: David P. Early
  Activity: Program 5    
  
  INPUTS:
         options (a-e), numerical answer( variable = number)
         
  OUTPUTS:
         message stating which opion was selected and whether it was a correct option, message telling whether or not the answer
         entered was correct or incorrect.
         multiple counter for types of problems and correct to incorrect ratio
         
         
*/
// header files

#include <iostream>
#include <iomanip> // needed to format output
#include <ctime>
#include <cstdlib>
// welcome screen mains
#define who " David P. Early               "
#define when " 14 june 2010                 "
#define assiegnment " Program 5                    "

// the following is the quote on the flash screen
#define line1P "\"Why do you say these things to me,"
#define line2P "\ when you know I will kill you for it\""
#define line3P "     ~General Zod  (superman 2) "

// the following will be the discription
#define line1W " This program will let you practice mathmatical      "
#define line2W " equations.                                          "

//function prototype's
void splashscreen ();
void welcome ();
void menu();
void error(char);
void right();
void wrong(double);
void summary(int, int, int, int, int, int, int);
using namespace std;


int main(int argc, char *argv[])
{
   // variables
 char option;
 double number, answer;
 int num1, num2;
 bool flag = true;
 unsigned seed = time(0);
 srand(seed);
 int addition, subtraction, multiplication, modulus, totalProblems, correct, incorrect;

 
  // call statement's
 splashscreen ();  
 welcome ();
 
 addition = subtraction = multiplication = modulus = 0;
 correct = incorrect =0;
 do
 {
     flag = true;
     num1 = 1 + rand() % 10;
     num2 = 1 + rand() % 10;
     menu();
     cin  >> option ;
     cout << "\n\n\n\n\n\n\n\n";
     
     system("PAUSE");
     system("cls");
           
     cout << " \n\n\n\n\n\n\n";
           
   switch (toupper(option))
   {
         case 'A':   addition++;
                      cout << "\t\t\t\t  Addition.\n"
                           <<"\t\t\t\t__________\n\n"
                           <<"\t\t\t\t" << setw(6) << num1 <<"\n"
                           <<"\t\t\t\t +" << setw(4) << num2 << "\n"
                           <<"\t\t\t\t__________\n\t\t\t\t" << setw(4) <<"";
                      cin  >> number;
                      answer = num1 + num2;
               break;
                 
          case 'B':   subtraction++;
                      cout << "\t\t\t\t Subtraction. \n"
                           <<"\t\t\t\t____________ \n\n"
                           <<"\t\t\t\t"<< setw(6) << num1 <<"\n"
                           <<"\t\t\t\t -" << setw(4) << num2 << "\n"
                           <<"\t\t\t\t____________ \n\t\t\t\t" << setw(5) <<"";
                      cin  >> number;
                      answer = num1 - num2;
               break;
               
          case 'C':   multiplication++;
                      cout << "\t\t\t\t Mutiplication. \n"
                           <<"\t\t\t\t______________ \n\n"
                           <<"\t\t\t\t"<< setw(7) << num1 <<"\n"
                           <<"\t\t\t\t *" << setw(5) << num2 << "\n"
                           <<"\t\t\t\t______________ \n\t\t\t\t" << setw(6)<<"";
                       cin >> number;
                       answer = num1 * num2;
               break;
               
          case 'D':    modulus++;
                       cout << "\t\t\t\t Modulus. \n"
                            <<"\t\t\t\t___________ \n\n"
                            <<"\t\t\t\t"<< setw(6) << num1 <<"\n"
                            <<"\t\t\t\t %" << setw(4) << num2 << "\n"
                            <<"\t\t\t\t___________\n\t\t\t\t"<< setw(5) << "";
                       cin  >> number;
                       answer = num1 % num2;
              break;
         case 'E':
         case 'X':     flag = false;
              break;
         default:  
                       flag = false;
                       error(option);
   }
   if (flag == true)
    
      if (number == answer)
      {      
             correct++;
             right ();
      }                                                                       
   else
   {
             incorrect++;
             wrong(number);
   }       
 }while (toupper(option) != 'E' && toupper(option) != 'X');
 summary(addition, subtraction, multiplication, modulus, totalProblems, correct, incorrect);
 
 system("PAUSE");
 return EXIT_SUCCESS;
   
}
void splashscreen ()//function definition
{
   cout << "              _____________________________________________  \n "
        << "           //:::::::::::::::::::::::::::::::::::::::::::::\\\\ \n"  
        << "          //:::_______:::::::::________::::::::::_____:::::::\\\\ \n"
        << "        //:::_/   _-\"\":::_--\"\"\"        \"\"\"--_::::\\_  ):::::::::\\\\ \n"  
        << "       //:::/    /:::::_\"                    \"-_:::\\/:::::|^\\:::\\\\ \n "
        << "     //:::/   /~::::::I__                      \\:::::::::|  \\:::\\\\ \n "  
        << "     \\\\:::\\   (::::::::::\"\"\"\"---___________     \"--------\"  /:::// \n "
        << "      \\\\:::\\  |::::::::::::::::::::::::::::\"\"\"\"==____      /:::// \n "
        << "       \\\\:::\"\\/::::::::::::::::::::::::::::::::::::::\\   /~:::// \n "
        << "         \\\\:::::::::::::::::::::::::::::::::::::::::::)/~:::// \n "
        << "           \\\\::::\\\"\"\"\"\"\"------_____:::::::::::::::::::::::// \n "
        << "             \\\\:::\"\\               \"\"\"\"\"-----_____::::::// \n "
        << "               \\\\:::\"\\    __----__                ):::// \n "
        << "                 \\\\:::\"\\/~::::::::~\\_         __/~::// \n "
        << "                   \\\\::::::::::::::::\"\"----\"\"\"::::// \n "
        << "                     \\\\:::::::::::::::::::::::::// \n "
        << "                       \\\\:::\\^\"\"--._.--\"\"^/:::// \n "
        << "                         \\\\::\"\\         /\"::// \n "
        << "                           \\\\::\"\\     /\"::// \n "
        << "                             \\\\::\"\\_/\":://  \n "
        << "                               \\\\:::::// " << line1P << "  \n "
        << "                                 \\\\_// " << line2P << " \n "
        << "                                   \"  \t\t " << line3P <<" \n " ;
   system("PAUSE");
   system ("cls");
}
void welcome ()//function definition
{   
   cout << " \n\n\n\n\n "
        << " \n\t/--------------------------------------------------------------\\ "   
       << " \n\t|                      About This Program                      |   "
       << " \n\t+--------------------------------------------------------------+   "
          << " \n\t|                                                              |   "
       << " \n\t|             Programmer's Name:" who                        " |   "
       << " \n\t|                          Date:" when                       " |   "  
       << " \n\t|               Name of program:" assiegnment                " |   "   
       << " \n\t|    /-------------------- Description --------------------\\   |  "  
          << " \n\t|    |" line1W                                             "|   |   "   
       << " \n\t|    |" line2W                                             "|   |   "  
       << " \n\t|    \\-----------------------------------------------------/   |  "  
       << " \n\t\\--------------------------------------------------------------/  "
        << " \n\n\n\n\n\n\n ";  
   system("PAUSE");
   system("cls");
}
void menu()
{
     system("CLS");    
     cout   << "\n\n\n\n\n\n\n";
     cout   << "\t\t\t\t  Math practice \n";
     cout   << "\t\t\t\t___________________ \n";
     cout   << "\t\t\t\t  A: Addition \n";
     cout   << "\t\t\t\t  B: Subtraction \n";
     cout   << "\t\t\t\t  C: Mutiplication \n";
     cout   << "\t\t\t\t  D: Modulus \n";
     cout   << "\t\t\t\t  E: Exit \n";
     cout   << "\n\t\t\t\t  option : ";
}
void error(char option)
{
     system("CLS");
     cout << "\n\n\n\n\n\n\n\n\n\n\n"
          << "\t\t\t" << option << " - is not a correct option!\n\n"
          << "\t\t\t Please Try Again!!"
          << "\n\n\n\n\n\n\n\n\n\n\n";
     system("PAUSE");
}
void right()
{
    cout << "\n\n\t\t\t congratulations  that is the correct answer!\n"
         << "\n\n\n\n\n\n\n";
    system("PAUSE");
}                
void wrong(double number)
{
    cout << "\n\n\t\t\t"<< number << " is not the correct answer!\n"
         << "\n\n\n\n\n\n\n";
    system("PAUSE");
}
void summary(int addition, int subtraction, int multiplication, int modulus, int totalProblems, int correct, int incorrect)
{
    system("CLS");      
    cout << "\n\n\n"
         << "\n\t\t\t       attempted problems"
         << "\n\t\t\t      -------------------" << endl
         << "\n\t   Addition\tSubtraction\t   Multiplication\tModulus"
         << "\n\t   --------\t-----------\t   -------------\t--------"
         << "\n\t     " << addition << "\t\t    " << subtraction << "\t\t\t  " << multiplication << "\t\t   " << modulus << endl;
    totalProblems = addition + subtraction + multiplication + modulus;
    cout << "\n\t\t\t\t      Total "
         << "\n\t\t\t\t   problems "
         << "\n\t\t\t\t  attempted " << totalProblems
         << "\n\n\n\t\t\t  Numbr of correct and incorrect"
         << "\n\t\t\t  ------------------------------"
         << "\n\t\t\tCorrect   \t\t   Incorrect"
         << "\n\t\t\t-------   \t\t   ---------"
         << "\n\t\t\t   "<< correct <<"\t\t\t       "<< incorrect << "\n"
         << "\n\n\n";
}

I tried moving the entire switch statement into a void but it didn't work and I just cant figure out how to make one all inclusive void function for the information in the switch statement.

Thank You for any help you can provide.

Answer
Hello David.

Wow! A beautiful program.

I think what your teacher wants is for you to move all the problem presentation code into a single function so that all the formatting of the problem does not have to be repeated in each switch statement. I've done that for you. I've made some other comments in your code. They all start with // ZM. After you read them, be sure to delete them. I appreciate all the work you did on the program.

/*
Programer: David P. Early
Activity: Program 5

INPUTS:
options (a-e), numerical answer( variable = number)

OUTPUTS:
message stating which opion was selected and whether it was a correct option, message telling whether or not the answer
entered was correct or incorrect.
multiple counter for types of problems and correct to incorrect ratio


*/
// header files

#include <iostream>
#include <iomanip> // needed to format output
#include <ctime>
#include <cstdlib>
#include <math.h> // ZM for fabs() function
// welcome screen mains
#define who " David P. Early               "
#define when " 14 june 2010                 "
#define assiegnment " Program 5                    "

// the following is the quote on the flash screen
#define line1P "\"Why do you say these things to me,"
#define line2P "\"when you know I will kill you for it\"" // ZM changed '\ ' to '\"' at start of line
#define line3P "     ~General Zod  (superman 2) "

// the following will be the discription
#define line1W " This program will let you practice mathmatical      "
#define line2W " equations.                                          "

//function prototype's
void splashscreen ();
void welcome ();
void menu();
void error(char);
void right();
void wrong(double);
void summary(int, int, int, int, int, int, int);
using namespace std;

// ZM added this function
void presentProblem(char* title, int num1, int num2, char opSymbol, double answer)
{
   // ZM the answer is used to set the width of the last print field
   // This lets the answer line up with the other numbers
   int answerWidth = fabs(answer) >= 10 ? 4 : 5;
   if (answer < 0) --answerWidth;

   cout <<"\t\t\t\t  " << title << ".\n"
       <<"\t\t\t\t__________\n\n"
       <<"\t\t\t\t" << setw(6) << num1 <<"\n"
       <<"\t\t\t\t " << opSymbol << setw(4) << num2 << "\n"
       <<"\t\t\t\t__________\n\t\t\t\t" << setw(answerWidth) << " ";
}

int main(int argc, char *argv[])
{
   // variables
   char option;
   double number, answer;
   int num1, num2;
   bool flag = true;
   unsigned seed = (unsigned)time(0); // ZM added cast to stop warning
   srand(seed);
   int addition, subtraction, multiplication, modulus, totalProblems, correct, incorrect;


   // call statement's
   splashscreen ();
   welcome ();

   addition = subtraction = multiplication = modulus = 0;
   totalProblems = 0; // ZM added
   correct = incorrect =0;
   do
   {
       flag = true;
       num1 = 1 + rand() % 10;
       num2 = 1 + rand() % 10;
       menu();
       cin  >> option ;
       cout << "\n\n\n\n\n\n\n\n";

       system("PAUSE");
       system("cls");

       cout << " \n\n\n\n\n\n\n";

       switch (toupper(option))
       {
       case 'A':   addition++;
           answer = num1 + num2;
           presentProblem("Addition", num1, num2, '+', answer);
           cin  >> number;
           break;

       case 'B':   subtraction++;
           answer = num1 - num2;
           presentProblem("Subtraction", num1, num2, '-', answer);
           cin  >> number;
           break;

       case 'C':   multiplication++;
           answer = num1 * num2;
           presentProblem("Mutiplication", num1, num2, '*', answer);
           cin >> number;
           break;

       case 'D':    modulus++;
           answer = num1 % num2;
           presentProblem("Modulus", num1, num2, '%', answer);
           cin  >> number;
           break;
       case 'E':
       case 'X':     flag = false;
           break;
       default:
           flag = false;
           error(option);
       }
       if (flag == true)

           if (number == answer)
           {
               correct++;
               right ();
           }
           else
           {
               incorrect++;
               wrong(number);
           }
   }while (toupper(option) != 'E' && toupper(option) != 'X');
   summary(addition, subtraction, multiplication, modulus, totalProblems, correct, incorrect);

   system("PAUSE");
   return EXIT_SUCCESS;

}
void splashscreen ()//function definition
{
   cout << "              _____________________________________________  \n "
       << "           //:::::::::::::::::::::::::::::::::::::::::::::\\\\ \n"
       << "          //:::_______:::::::::________::::::::::_____:::::::\\\\ \n"
       << "        //:::_/   _-\"\":::_--\"\"\"        \"\"\"--_::::\\_  ):::::::::\\\\ \n"
       << "       //:::/    /:::::_\"                    \"-_:::\\/:::::|^\\:::\\\\ \n "
       << "     //:::/   /~::::::I__                      \\:::::::::|  \\:::\\\\ \n "
       << "     \\\\:::\\   (::::::::::\"\"\"\"---___________     \"--------\"  /:::// \n "
       << "      \\\\:::\\  |::::::::::::::::::::::::::::\"\"\"\"==____      /:::// \n "
       << "       \\\\:::\"\\/::::::::::::::::::::::::::::::::::::::\\   /~:::// \n "
       << "         \\\\:::::::::::::::::::::::::::::::::::::::::::)/~:::// \n "
       << "           \\\\::::\\\"\"\"\"\"\"------_____:::::::::::::::::::::::// \n "
       << "             \\\\:::\"\\               \"\"\"\"\"-----_____::::::// \n "
       << "               \\\\:::\"\\    __----__                ):::// \n "
       << "                 \\\\:::\"\\/~::::::::~\\_         __/~::// \n "
       << "                   \\\\::::::::::::::::\"\"----\"\"\"::::// \n "
       << "                     \\\\:::::::::::::::::::::::::// \n "
       << "                       \\\\:::\\^\"\"--._.--\"\"^/:::// \n "
       << "                         \\\\::\"\\         /\"::// \n "
       << "                           \\\\::\"\\     /\"::// \n "
       << "                             \\\\::\"\\_/\":://  \n "
       << "                               \\\\:::::// " << line1P << "  \n "
       << "                                 \\\\_// " << line2P << " \n "
       << "                                   \"  \t\t " << line3P <<" \n " ;
   system("PAUSE");
   system ("cls");
}
void welcome ()//function definition
{
   cout << " \n\n\n\n\n "
       << " \n\t/--------------------------------------------------------------\\ "
       << " \n\t|                      About This Program                      |   "
       << " \n\t+--------------------------------------------------------------+   "
       << " \n\t|                                                              |   "
       << " \n\t|             Programmer's Name:" who                        " |   "
       << " \n\t|                          Date:" when                       " |   "
       << " \n\t|               Name of program:" assiegnment                " |   "
       << " \n\t|    /-------------------- Description --------------------\\   |  "
       << " \n\t|    |" line1W                                             "|   |   "
       << " \n\t|    |" line2W                                             "|   |   "
       << " \n\t|    \\-----------------------------------------------------/   |  "
       << " \n\t\\--------------------------------------------------------------/  "
       << " \n\n\n\n\n\n\n ";
   system("PAUSE");
   system("cls");
}
void menu()
{
   system("CLS");
   cout   << "\n\n\n\n\n\n\n";
   cout   << "\t\t\t\t  Math practice \n";
   cout   << "\t\t\t\t___________________ \n";
   cout   << "\t\t\t\t  A: Addition \n";
   cout   << "\t\t\t\t  B: Subtraction \n";
   cout   << "\t\t\t\t  C: Mutiplication \n";
   cout   << "\t\t\t\t  D: Modulus \n";
   cout   << "\t\t\t\t  E: Exit \n";
   cout   << "\n\t\t\t\t  option : ";
}
void error(char option)
{
   system("CLS");
   cout << "\n\n\n\n\n\n\n\n\n\n\n"
       << "\t\t\t" << option << " - is not a correct option!\n\n"
       << "\t\t\t Please Try Again!!"
       << "\n\n\n\n\n\n\n\n\n\n\n";
   system("PAUSE");
}
void right()
{
   cout << "\n\n\t\t\t congratulations  that is the correct answer!\n"
       << "\n\n\n\n\n\n\n";
   system("PAUSE");
}
void wrong(double number)
{
   cout << "\n\n\t\t\t"<< number << " is not the correct answer!\n"
       << "\n\n\n\n\n\n\n";
   system("PAUSE");
}
void summary(int addition, int subtraction, int multiplication, int modulus, int totalProblems, int correct, int incorrect)
{
   system("CLS");
   cout << "\n\n\n"
       << "\n\t\t\t       attempted problems"
       << "\n\t\t\t      -------------------" << endl
       << "\n\t   Addition\tSubtraction\t   Multiplication\tModulus"
       << "\n\t   --------\t-----------\t   -------------\t--------"
       << "\n\t     " << addition << "\t\t    " << subtraction << "\t\t\t  " << multiplication << "\t\t   " << modulus << endl;
   totalProblems = addition + subtraction + multiplication + modulus;
   cout << "\n\t\t\t\t      Total "
       << "\n\t\t\t\t   problems "
       << "\n\t\t\t\t  attempted " << totalProblems
       << "\n\n\n\t\t\t  Numbr of correct and incorrect"
       << "\n\t\t\t  ------------------------------"
       << "\n\t\t\tCorrect   \t\t   Incorrect"
       << "\n\t\t\t-------   \t\t   ---------"
       << "\n\t\t\t   "<< correct <<"\t\t\t       "<< incorrect << "\n"
       << "\n\n\n";
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.