Brothersoft.comWindows|Mac|Mobile|Games

|Message

Solve software problem quickly Share experience to help others Improve the ability of solving problem
how do I Write a Math Practice Program in C++?

08/24/2011 03:59 by justsharon

First answer posted by ArvindK at 08/24/2011 03:59
Add Your Answer
10~1000 characters in length CAPTCHA:
7 Answers
  • ArvindK
    1. Load up your complier or a source editor
    2. Create a new source file
    3. Apply the code that goes on top. This is:
      • #include <iostream>
      • int main()
      • using namespace std;{                                                                                                                           
    4. Declare Variables. This is so that the compiler knows what type [integer, string, etc.] that the variables will be:
      • int result;
      • int solution;
      • int score;                          
    5. Create some problems to use. These can be any problems, but C++ is a very operator rich language so make sure to use parenthesis if you want a certain order of operations.
    6. Write your intro statement. This should say something about how the program works, and how to play. An example:
      • std::cout << "This is math game" << std::endl;
      • std::cout << "To play, input your answer and press enter" << std::endl;
    7. Design how your problem system is going to work. This should use if and else statements to tell the user if they are wrong. Your code should look something like this (note: you need to define all the variables used in this example beforehand):
      • std::cout << "10 + 15 ";
      • std::cin >> result;
      • std::cin.ignore();
      • solution = 10 + 15;
      •  if ( result == solution ) {
      •    std::cout << "Correct! ";
      •    score = score + 1;
      •    std::cout << "Your current score is " << score <<"\n";
      •  } else {
      •    std::cout << "You're wrong\n";
      •  }
    8. Design a scoring system This is how score will be kept. The one in the example works by declaring score as an integer, then adding one to the score if the problem is correct. At the end, the scoring system should count up scores and tell the player if they failed or won. The code should look something like
      • std::cout << "Your total score: " << score <<"\n";
      •  if ( score < 4 ) {
      •       std::cout << "You failed!";
      •  } else if ( score == 5 ) {
      •       std::cout << "All correct! Nice job :-)";
      •  } else {
      •       std::cout << "Good job!";
      •  }
    9. Make the program wait for user to hit enter before it closes This is a very simple procedure:
      • std::cin.get();
      • std::cin.get();           
    10. Debug and compile the program. Name it something like "math_game.cpp"
    11. Run the program. You should be able to play through it without any problems, assuming the code was compiled correctly.
  • Was this answer helpful? 00 · 08/24/2011 03:59
  • JPhillippy
  • How to write a C++ program that outputs the multiplication table? can anyone help this:

    include <iostream>

    using namespace std;

    int table;

     

    int main()

    {

    cout << "Multiplication Table: ";

    cin >> table;

    cout << setw(8) << n;

    for (int i=1; i <= 100; ++i)

    cout << left << setw(9) << i;

    for (int i=1; i <= 10; ++i)

    {

    cout << setw(7);

    for (int j=1; j <= 10; ++i)

    cout << setw(9) << i*j;

    cout << endl;

    }

    return(0);

    }

  • Was this answer helpful? 10 · 12/23/2011 02:43
  • AlbertAzose
  • Here is some code commented. If you want the table to go past 8 and still look as it does, you may need to remove the tab spaces, as the console window will not display this correctly otherwise. But i just wrote this to give you an idea, you should be able to edit it without problems. Also, it is bad programming practice to have global variables.

    #include <iostream.h>
    using namespace std;

    int main()
    {
    cout<<"Multiplication Table: \n\n";
    int x, y;
    for(x = 0; x <= 8; x++) cout<<"\t"<<"["<<x<<"]";//this just enters 0-8 like so [8] on the top row
    cout<<"\n";
    for(x = 0; x <= 8; x++) {//loop through 0 - 8
    cout<<"["<<x<<"]"<<"\t";//same as above, only vertical number this time.
    for(y = 0; y <= 8; y++) {//go through 0 - 8 again.
    cout<<x * y<<"\t";//display the multiplication, will go 0(x) * 0(y), 0(x) * 1(y). etc.
    }
    cout<<"\n";
    }
    return 0;
    }
  • Was this answer helpful? 10 · 12/23/2011 02:44
  • KeithCorbin
  • Thanks very much. I hope more programmer share their experiences here.
  • Was this answer helpful? 00 · 12/23/2011 02:46
  • DanielSimon
  • Yeah. it's good to share. writing programs is funny for someone while boring for others. practices, sharing and discussing make programmers access a higher level.
  • Was this answer helpful? 00 · 12/23/2011 02:49
  • Anonymous user
  • s=1/2+2/3+3/4+...

  • Was this answer helpful? 00 · 04/13/2012 23:45
  • Anonymous user
  • s=1/2+2/3+3/4+...

  • Was this answer helpful? 00 · 04/13/2012 23:46
Add Your Answer
10~1000 characters in length CAPTCHA:

Added Successfully!

×

Are you sure to delete your answer?

NoYes

×

Are you sure to choose it as the best answer?

NoYes

×

Voted Successfully!

×

You can't vote for yourself

×

You can't choose your own answer

×