How can i make this code better?

Discussion in 'C++' started by eli m, Mar 9, 2013.

  1. eli m

    eli m Guest

    Any idea on how i could make this code better?

    My code:

    #include <iostream>
    #include <string>
    using namespace std;

    int main()
    {
    int run = 0;
    string function, mathop;
    cout << "Type in help for a list of functions\n";
    //Start main loop
    while (run == 0)
    {
    cout << "Type in a function:";
    cin >> function;
    //Math function
    if (function == "math")
    {
    int mathloop;
    mathloop = 0;
    //Start while loop for math function
    while (mathloop == 0)
    {
    cout << "What math operation do you want to use?";
    cin >> mathop;
    // Multiplication Math Function
    if (mathop == "multiplication")
    {
    int mfirstnum, msecondnum, ans;
    cout << "Type in your first number:";
    cin >> mfirstnum;
    cout << "Multiply your first number by:";
    cin >> msecondnum;
    ans = mfirstnum * msecondnum;
    cout << ans << "\n";
    }
    //Break out of mathloop if main is entered
    else if (mathop == "main")
    {
    mathloop = 1;
    }
    }
    }
    }
    }
     
    eli m, Mar 9, 2013
    #1
    1. Advertising

  2. eli m

    Stefan Ram Guest

    eli m <> writes:
    >Any idea on how i could make this code better?


    Splitting sensibly into several smaller functions, each of
    which has a small documentation of its interface in front of
    its definition and maybe adding a small user manual at the
    start of the source code for the user of the program
    describing the overall purpose of the program.
     
    Stefan Ram, Mar 9, 2013
    #2
    1. Advertising

  3. eli m

    Stefan Ram Guest

    eli m <> writes:
    >int main()
    >{
    >int run = 0;


    Add indentation corresponding with nesting depth.
    »boolean running = false;«

    >string function, mathop;
    >cout << "Type in help for a list of functions\n";


    "Type in \"help\" for a list of functions.\n"

    >//Start main loop
    >while (run == 0)


    while( running )

    >{
    >cout << "Type in a function:";
    >cin >> function;
    >//Math function
    >if (function == "math")


    I am not aware of a function named »math«.

    You might add some code to report errors,
    e.g., unintelligible user input.
     
    Stefan Ram, Mar 9, 2013
    #3
  4. eli m

    Jorgen Grahn Guest

    On Sat, 2013-03-09, eli m wrote:
    > Any idea on how i could make this code better?

    ....
    > cout << "Type in help for a list of functions\n";


    A meta-answer: these "please enter ..." exercises have been popular
    since I was a kid, but in reality they don't make anyone happy. Noone
    uses programs like that, not on Unix and not on Windows. And they are
    hard to write.

    How about this one instead?

    The program reads lines of text from standard input (std::cin) and
    writes to standard output. Lines which look like multiplication

    number * number

    will be evaluated and printed. Other lines will be silently ignored.

    Then you can add a lot of things ...
    - define more exactly what a "number" is
    - what to do about numerical overflow
    - add more operators
    - add full expressions
    - write the user manual
    - create test suites
    - improve error handling
    - ...
    In the end you will have the Unix bc(1) utility.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Mar 10, 2013
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    3,779
    Frank Birbacher
    Jun 14, 2008
  2. Diwa
    Replies:
    0
    Views:
    1,022
  3. Oltmans
    Replies:
    1
    Views:
    244
    J Kenneth King
    Apr 19, 2010
  4. Jay Bornhoft
    Replies:
    13
    Views:
    213
  5. Joshua Muheim

    Newbie: Make this code better?

    Joshua Muheim, Mar 27, 2007, in forum: Ruby
    Replies:
    3
    Views:
    105
    eden li
    Mar 27, 2007
Loading...

Share This Page