How are things looking

This is a discussion on How are things looking within the C++ Programming forums, part of the General Programming Boards category; Yes yes, I know you haven't heard from me in a while. Holidays are brilliant hey! Hope everyone enjoyed theirs! ...

  1. #1
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124

    How are things looking

    Yes yes, I know you haven't heard from me in a while. Holidays are brilliant hey! Hope everyone enjoyed theirs!

    Anyhow, I've just written this little program. I'll be adding more things to it during the course of tomorrow, but for now I was wondering if there are any comments or crits that people could give me on the code in it's current stage.

    Code:
    /*
    ##############################################
    ## Addition & Subtraction Assistance Program
    ##############################################
    ##
    ## Written by    : CC
    ## Last modified : 15/01/2004
    ## Comments      : A program designed to help children practise simple addition
    ##                 and subtraction sums. 
    ##############################################
    */
    
    #include <cstdlib>
    using std::rand;
    using std::srand;
    
    #include <iomanip>
    using std::setw;
    
    #include <iostream> 
    using std::cout;
    using std::cin;
    using std::setw;
    
    // Prototypes
    char addition();
    char subtraction();
    char repeat( int total1, int total2 );
    inline void screen_clear();
    
    int main()
    {
       int pickAddSub;
       char again;
       again = 'y';
    
       cout << "Addition & Subtraction Assistance Program" << '\n';
       
       srand( time( 0 ) );
       
       while ( again == 'y' )
       {
          // Giving pickAddSub a random value of either 1 or 2
          pickAddSub = ( 1 + rand() % 2 );
          
          if ( pickAddSub == 1 )
             again = addition();
          else if ( pickAddSub == 2 )
             again = subtraction();
          
          screen_clear();
       }
    
       return 0;
    }
    
    char addition()
    {
       int number1, number2, usertotal, total;
       char again;
    
       srand( time( 0 ) );
    
       number1 = ( 0 + rand() % 15 );
       number2 = ( 0 + rand() % 15 );
       
       total = number1 + number2;
    
       cout << "\nHere is the sum...\n"
            << setw( 6 ) << number1 << "  +  " << number2 << "  =  ";
       cin >> usertotal;
       
       again = repeat( usertotal, total );
       return again;
    }
    
    char subtraction()
    {
       int number1, number2, usertotal, total;
       char again;
       
       srand( time( 0 ) );
    
       number1 = ( 0 + rand() % 15 );
       number2 = ( 0 + rand() % 15 );
       
       // We cannot have negative numbers   
       if ( number1 > number2 )
       {
          total = number1 - number2;
          
          cout << "\nHere is the sum...\n"
               << setw( 6 ) << number1 << "  -  " << number2 << "  =  ";
          cin >> usertotal;      
       }
       else
       {
          total = number2 - number1;
       
          cout << "\nHere is the sum...\n"
               << setw( 6 ) << number2 << "  -  " << number1 << "  =  ";
          cin >> usertotal;
       }
    
       again = repeat( usertotal, total );
       return again;
    }
    
    char repeat( int total1, int total2 )
    {
       char again;
       
       if ( total1 == total2 )
       {
          cout << "Very Good!\n"
               << "Would you like to try again? [y/n] ";
          cin >> again;
          return again;
       }      
       else
       {
          cout << "No.\n"
               << "Would you like to try again? [y/n] ";
          cin >> again;
          return again;
       }
    }
    
    inline void screen_clear()
    {
        system ( "CLS" );
    }
    Much appreciated as always

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char addition()
    {
       int number1, number2, usertotal, total;
     
       number1 = ( 0 + rand() % 15 );
       number2 = ( 0 + rand() % 15 );
       
       total = number1 + number2;
    
       cout << "\nHere is the sum...\n"
            << setw( 6 ) << number1 << "  +  " << number2 << "  =  ";
       cin >> usertotal;
    
       return repeat( usertotal, total );   
    }
    For starters, you only ever call srand once per execution of your program. Don't call it more than once. I mean, I suppose you can, but there is no point to doing so, unless you're specificly seeding rand to give you a specific set of values.

    Next, you don't really need the 'again' varible here, since all it's doing is holding the return value of another function, and then immediately returning it.

    Code:
    char repeat( int total1, int total2 )
    {
       char again;
       
       if ( total1 == total2 )
       {
          cout << "Very Good!" << endl << "Would you like to try again? [y/n] ";
       }      
       else
       {
          cout << "No." << endl << "Would you like to try again? [y/n] ";
       }
       cin >> again;
       return again;
    }
    In either case you're going to be reading into 'again' and returning it, so just do it once at the bottom of the function instead of twice in each branch.

    You could of course compact your cout lines to a single string with a newline, instead of three chunks, but either way works.

    Also, you could just call 'repeat' in the first while loop, rather than calling once in both the add and subtract function. It would take a minor rewrite, but it would look cleaner.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    These, to me, just don't seem to make any sense
    again = addition();
    again = subtraction();
    again = repeat( usertotal, total );
    
    And I would, preferably make these functions take some arguments and return the result
    
    int addition(int firstNum, int secondNum)
    int subtraction(int firstNum, int secNum)
    
    A sum is the amount obtained as a result of adding numbers, so the following could be confusing. 
    
    total = number2 - number1;
       
    cout << "\nHere is the sum...\n"
            << setw( 6 ) << number2 << "  -  " << number1 << "  =  ";
    cin >> usertotal;
    Of course above can be seen as the sum of a positive and a negative number, but children will miss it.
    
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    I've been working at this app for a while today and this is what I've come up with. Hope it's improved

    Code:
    /*
    ##############################################
    ## Addition & Subtraction Assistance Program
    ##############################################
    ##
    ## Written by    : CC
    ## Last modified : 15/01/2004
    ## Comments      : A program designed to help children practise simple addition
    ##                 and subtraction sums. 
    ##############################################
    */
    
    #include <cstdlib>
    using std::rand;
    using std::srand;
    
    #include <iomanip>
    using std::setw;
    
    #include <iostream> 
    using namespace std;
    
    
    // Prototypes
    void addition();
    void subtraction();
    char repeat();
    void numeric_check( int y );
    void response( int total1, int total2 );
    inline void screen_clear();
    
    int main()
    {
       int pickAddSub;
       char again;
       again = 'y';
    
       cout << "Addition & Subtraction Assistance Program\n"
            << "-----------------------------------------\n";
    
       srand( time( 0 ) );
       
       while ( again == 'y' )
       {
          // Giving pickAddSub a random value of either 1 or 2
          pickAddSub = ( 1 + rand() % 2 );
          
          if ( pickAddSub == 1 )
             addition();
          else if ( pickAddSub == 2 )
             subtraction();
          
          // Asks the user if they want to carry on playing
          again = repeat();
          screen_clear();
       }
    
       return 0;
    }
    
    void addition()
    {
       int number1, number2, usertotal, total;
       char again;
    
       number1 = ( 0 + rand() % 15 ); // Gives number1 and number2 random values each time function runs
       number2 = ( 0 + rand() % 15 );
       
       total = number1 + number2;
    
       cout << '\n' << setw( 6 ) << number1 << "  +  " << number2 << "  =  ";
          
       while ( !(cin >> usertotal) ) // Incase something non-valid is entered the program won't die
       {
          numeric_check( usertotal );
       }
       
       response( usertotal, total ); // Random responses to correct or incorrect answers
    }
    
    void subtraction()
    {
       int number1, number2, usertotal, total;
       char again;
       
       number1 = ( 0 + rand() % 15 ); // Gives number1 and number2 random values each time function runs
       number2 = ( 0 + rand() % 15 );
       
       // We cannot have negative numbers subtracted by negative numbers, the poor children
       if ( number1 > number2 )
       {
          total = number1 - number2;
          
          cout << '\n' << setw( 6 ) << number1 << "  -  " << number2 << "  =  ";
          
          while ( !(cin >> usertotal) ) // Incase something non-valid is entered the program won't die
          {
             numeric_check( usertotal );
          }     
       }
       else
       {
          total = number2 - number1;
       
          cout << '\n' << setw( 6 ) << number2 << "  -  " << number1 << "  =  ";
          
          while ( !(cin >> usertotal) ) // Incase something non-valid is entered the program won't die
          {
             numeric_check( usertotal );
          }
       }
       
       response( usertotal, total ); // Random responses to correct or incorrect answers
    }
    
    char repeat()
    {
       char again;
       
       cout << "Would you like to try again? [y/n] ";
       cin >> again;
       return again;
    }
    
    inline void screen_clear()
    {
       system ( "cls" );
       cout << "Addition & Subtraction Assistance Program\n"
            << "-----------------------------------------\n";
    }
    
    void numeric_check( int y )
    {
        cin.clear();
        cin.ignore( numeric_limits<streamsize>::max(), '\n' );
        cerr << "\nWoops! Try again: ";
    }
    
    void response( int total1, int total2 )
    {
       int pick;
       
       pick = ( 1 + rand() % 4 );
       
       if ( total1 == total2 )
       { 
          switch( pick ) // if correct
          {
             case 1: 
                      cout << "\n  /\n\\/\n\nVery Good!\n";   
                      break;
             case 2:    
                      cout << "\n  /\n\\/\n\nExcellent!\n";
                      break;
             case 3:
                      cout << "\n  /\n\\/\n\nNice Work!\n";
                      break;
             case 4:
                      cout << "\n  /\n\\/\n\nKeep up the good work!\n";
                      break;
          }
       }
       else
       {
          switch( pick ) // if incorrect
          {
             case 1: 
                      cout << "\n\\/\n/\\\n\nNo.\n";   
                      break;
             case 2:    
                      cout << "\n\\/\n/\\\n\nWrong. Try once more.\n";
                      break;
             case 3:
                      cout << "\n\\/\n/\\\n\nDon't give up!\n";
                      break;
             case 4:
                      cout << "\n\\/\n/\\\n\nNo. Keep trying.\n";
                      break;
          }
       }
    }
    As always, comments are appreciated as I'm still new to this programming world.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Originally posted by cyberCLoWn
    Code:
    /*
    ##############################################
    ## Addition & Subtraction Assistance Program
    ##############################################
    ##
    ## Written by    : CC
    ## Last modified : 15/01/2004
    ## Comments      : A program designed to help children practise simple addition
    ##                 and subtraction sums. 
    ##############################################
    */
    
    #include <cstdlib>
    Nice
    using std::rand;
    using std::srand;
    
    #include <iomanip>
    Nice
    using std::setw;
    
    #include <iostream> 
    ... but then... :(
    using namespace std;
    
    
    // Prototypes
    void addition();
    void subtraction();
    char repeat();
    Misleading name. It seems like this function would check y for validity.
    Actually it just resets cin to a ready state and writes an error message. It doesn't even use the argument.
    void numeric_check( int y );
    It would make more sense to make the argument a bool called correct and do the comparison outside.
    void response( int total1, int total2 );
    inline void screen_clear();
    
    int main()
    {
       int pickAddSub;
    Make the next two one line. Initialization is better than definition followed by assignment.
       char again;
       again = 'y';
    
       cout << "Addition & Subtraction Assistance Program\n"
            << "-----------------------------------------\n";
    
       srand( time( 0 ) );
       
       while ( again == 'y' )
       {
          // Giving pickAddSub a random value of either 1 or 2
          pickAddSub = ( 1 + rand() % 2 );
          
          if ( pickAddSub == 1 )
             addition();
    Given that the rand guarantees that pickAddSub is 1 or 2, the else here isn't really necessary.
          else if ( pickAddSub == 2 )
             subtraction();
          
          // Asks the user if they want to carry on playing
          again = repeat();
          screen_clear();
       }
    
       return 0;
    }
    
    void addition()
    {
       int number1, number2, usertotal, total;
    Unused as far as I can see.
       char again;
    
       number1 = ( 0 + rand() % 15 ); // Gives number1 and number2 random values each time function runs
       number2 = ( 0 + rand() % 15 );
       
       total = number1 + number2;
    
       cout << '\n' << setw( 6 ) << number1 << "  +  " << number2 << "  =  ";
          
       while ( !(cin >> usertotal) ) // Incase something non-valid is entered the program won't die
       {
          numeric_check( usertotal );
       }
       
       response( usertotal, total ); // Random responses to correct or incorrect answers
    }
    
    void subtraction()
    {
       int number1, number2, usertotal, total;
    Here, too.
       char again;
       
       number1 = ( 0 + rand() % 15 ); // Gives number1 and number2 random values each time function runs
       number2 = ( 0 + rand() % 15 );
       
       // We cannot have negative numbers subtracted by negative numbers, the poor children
       if ( number1 > number2 )
       {
          total = number1 - number2;
          
          cout << '\n' << setw( 6 ) << number1 << "  -  " << number2 << "  =  ";
          
          while ( !(cin >> usertotal) ) // Incase something non-valid is entered the program won't die
          {
             numeric_check( usertotal );
          }     
       }
       else
       {
          total = number2 - number1;
       
          cout << '\n' << setw( 6 ) << number2 << "  -  " << number1 << "  =  ";
          
          while ( !(cin >> usertotal) ) // Incase something non-valid is entered the program won't die
          {
             numeric_check( usertotal );
          }
       }
       
       response( usertotal, total ); // Random responses to correct or incorrect answers
    }
    
    char repeat()
    {
       char again;
       
       cout << "Would you like to try again? [y/n] ";
       cin >> again;
       return again;
    }
    
    inline void screen_clear()
    {
       system ( "cls" );
       cout << "Addition & Subtraction Assistance Program\n"
            << "-----------------------------------------\n";
    }
    
    void numeric_check( int y )
    {
        cin.clear();
        cin.ignore( numeric_limits<streamsize>::max(), '\n' );
        cerr << "\nWoops! Try again: ";
    }
    
    void response( int total1, int total2 )
    {
       int pick;
       
       pick = ( 1 + rand() % 4 );
       
       if ( total1 == total2 )
       { 
          switch( pick ) // if correct
          {
             case 1: 
                      cout << "\n  /\n\\/\n\nVery Good!\n";   
                      break;
             case 2:    
                      cout << "\n  /\n\\/\n\nExcellent!\n";
                      break;
             case 3:
                      cout << "\n  /\n\\/\n\nNice Work!\n";
                      break;
             case 4:
                      cout << "\n  /\n\\/\n\nKeep up the good work!\n";
                      break;
          }
       }
       else
       {
          switch( pick ) // if incorrect
          {
             case 1: 
                      cout << "\n\\/\n/\\\n\nNo.\n";   
                      break;
             case 2:    
                      cout << "\n\\/\n/\\\n\nWrong. Try once more.\n";
                      break;
             case 3:
                      cout << "\n\\/\n/\\\n\nDon't give up!\n";
                      break;
             case 4:
                      cout << "\n\\/\n/\\\n\nNo. Keep trying.\n";
                      break;
          }
       }
    }
    Last edited by CornedBee; 01-15-2004 at 10:11 AM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I know it's not a big deal but there is no need to store the random value for when you are deciding to do addition or subtraction. You could do it like:

    Code:
    if( rand() % 2 ) // If 0 then evaluates to false, if 1 true
    {
      // addition..
    }
    else
    {
      // subtraction..
    }
    Also, what's with the 0 + ?

    Code:
    number1 = ( 0 + rand() % 15 );
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  7. #7
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    You know, you buy all these fancy books to teach you stuff. Yes they do accomplish it, but this is how you really learn. Wow! Thanks to this community again. A really valuable resource to which one day I hope I can be an asset to. I'll get there!

    I have changed the code as what you guys have said makes sense and looks better than the way I had implemented it.

    Here's the code as it stands now:

    Code:
    /*
    ##############################################
    ## Addition & Subtraction Assistance Program
    ##############################################
    ##
    ## Written by    : CC
    ## Last modified : 15/01/2004
    ## Comments      : A program designed to help children practise simple addition
    ##                 and subtraction sums. 
    ##############################################
    */
    
    #include <cstdlib>
    using std::rand;
    using std::srand;
    
    #include <iomanip>
    using std::setw;
    
    #include <iostream> 
    using std::cout;
    using std::cin;
    using std::cerr;
    using std::streamsize;
    using std::max;
    using std::numeric_limits;
    
    // Prototypes
    void addition();
    void subtraction();
    char repeat();
    void correct();
    void response( int total1, int total2 );
    inline void screen_clear();
    
    int main()
    {
       char again = 'y';
    
       cout << "Addition & Subtraction Assistance Program\n"
            << "-----------------------------------------\n";
    
       srand( time( 0 ) );
       
       while ( again == 'y' )
       {
          if ( rand() % 2 )
             addition();
          else 
             subtraction();
          
          // Asks the user if they want to carry on playing
          again = repeat();
          screen_clear();
       }
    
       return 0;
    }
    
    void addition()
    {
       int number1, number2, usertotal, total;
    
       number1 = ( rand() % 15 ); // Gives number1 and number2 random values each time function runs
       number2 = ( rand() % 15 );
       
       total = number1 + number2;
    
       cout << '\n' << setw( 6 ) << number1 << "  +  " << number2 << "  =  ";
          
       while ( !(cin >> usertotal) ) // Incase something non-valid is entered the program won't die
       {
          correct();
       }
       
       response( usertotal, total ); // Random responses to correct or incorrect answers
    }
    
    void subtraction()
    {
       int number1, number2, usertotal, total;
       
       number1 = ( rand() % 15 ); // Gives number1 and number2 random values each time function runs
       number2 = ( rand() % 15 );
       
       // We cannot have negative numbers subtracted by negative numbers, the poor children
       if ( number1 > number2 )
       {
          total = number1 - number2;
          
          cout << '\n' << setw( 6 ) << number1 << "  -  " << number2 << "  =  ";
          
          while ( !(cin >> usertotal) ) // Incase something non-valid is entered the program won't die
          {
             correct();
          }     
       }
       else
       {
          total = number2 - number1;
       
          cout << '\n' << setw( 6 ) << number2 << "  -  " << number1 << "  =  ";
          
          while ( !(cin >> usertotal) ) // Incase something non-valid is entered the program won't die
          {
             correct();
          }
       }
       
       response( usertotal, total ); // Random responses to correct or incorrect answers
    }
    
    char repeat()
    {
       char again;
       
       cout << "Would you like to try again? [y/n] ";
       cin >> again;
       return again;
    }
    
    inline void screen_clear()
    {
       system ( "cls" );
       cout << "Addition & Subtraction Assistance Program\n"
            << "-----------------------------------------\n";
    }
    
    void correct()
    {
        cin.clear();
        cin.ignore( numeric_limits<streamsize>::max(), '\n' );
        cerr << "\nWoops! Try again: ";
    }
    
    void response( int total1, int total2 )
    {
       int pick;
       
       pick = ( 1 + rand() % 4 );
       
       if ( total1 == total2 )
       { 
          switch( pick ) // if correct
          {
             case 1: 
                      cout << "\n  /\n\\/\n\nVery Good!\n";   
                      break;
             case 2:    
                      cout << "\n  /\n\\/\n\nExcellent!\n";
                      break;
             case 3:
                      cout << "\n  /\n\\/\n\nNice Work!\n";
                      break;
             case 4:
                      cout << "\n  /\n\\/\n\nKeep up the good work!\n";
                      break;
          }
       }
       else
       {
          switch( pick ) // if incorrect
          {
             case 1: 
                      cout << "\n\\/\n/\\\n\nNo.\n";   
                      break;
             case 2:    
                      cout << "\n\\/\n/\\\n\nWrong. Try once more.\n";
                      break;
             case 3:
                      cout << "\n\\/\n/\\\n\nDon't give up!\n";
                      break;
             case 4:
                      cout << "\n\\/\n/\\\n\nNo. Keep trying.\n";
                      break;
          }
       }
    }
    It would make more sense to make the argument a bool called correct and do the comparison outside.
    Not too sure what you mean about that

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Instead of
    Code:
    response( usertotal, total );
    
    
    void response( int total1, int total2 )
    {
       int pick;
       
       pick = ( 1 + rand() % 4 );
       
       if ( total1 == total2 )
    Do
    Code:
    response( usertotal == total );
    
    
    void response( bool correct )
    {
       int pick;
       
       pick = ( 1 + rand() % 4 );
       
       if ( correct )
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    response would also work better with a string lookup table.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Suggestions for things to study
    By Mastadex in forum Windows Programming
    Replies: 5
    Last Post: 08-18-2008, 09:23 AM
  2. Doing two things at once
    By eam in forum Linux Programming
    Replies: 7
    Last Post: 08-04-2004, 10:10 PM
  3. Plants that eat things
    By StinkyRyan in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 07-05-2004, 03:41 PM
  4. Selecting things on the canvas
    By Barjor in forum Windows Programming
    Replies: 0
    Last Post: 08-30-2001, 02:10 PM
  5. Help with these three things...
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 08-26-2001, 07:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21