Thread: First Program need advice

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    Exclamation First Program need advice

    Well i am a complete and utter novice to programming. I know very little about c++ and what i do know i have learned from both the tutorials on this site and a book for beginners that i am borrowing from a friend.

    It's A program to calculate compound interest.

    If you have any advice or critisism i'm interested. I'm still learing.

    Also i've heard that you shouldn't use "goto" but i don't know a better way.

    It was all written by me except for the invalid input code. (Can someone break that down and explain to me how it works?)

    i can't seem to upload so you'll just have to compile the code yourself to see it.

    it might be a little hard to read
    Code:
    /* Compound interest calculator. For stuff like investment accounts
    (C)2006 Shane Loretz. 
    */
    #include <iostream>
    #include <ios>
    #include <limits>
    
    using namespace std;
    
    float intCalc(float apy, float smoney, float yrinc, float y);
     
    
    int main()
    { 
      cout<<"--------------------------------------------------------------------------------";//looks nice huh
      cout<<"\nCompound Interest Calculator \n (C)2006 Shane Loretz\n\n";
      cout<<"--------------------------------------------------------------------------------";
    restart:
      float errorCheck;
      float y;
      float apy;
      float smoney;
      float yrinc;
      
      //user input
    y:
      cout<<"\nPlease enter how many years till you collect the money (At least 2): ";
      cin>> y;
      if (y >= 150) cout<<"\n Please keep the years below 150\n\n"; //limits entry size
      cout<<"\nPlease enter your expected anual interest in decimal form.\n Example: 5% = 0.05: ";
      cin>> apy ;                
      if (apy > 100) cout<<"\n Please keep interest below 100.0 or %10,000\n\n"; // keeps idiots from making the program output huge numbers
      cout<<"\nPlease enter the initial money in the account: ";
      cin>> smoney ;
      cout<<"\nPlease enter how much you are going to add to your accout per year.\n \
    Can also be negative if you take money out each year: ";
      cin>> yrinc ;
      intCalc(apy, smoney, yrinc, y);
      //exit or redo question
    ques:
      int pend; //pend variable should be with other variables but i like it here
      cout<<"\nPress 1 to do another calculation or press 2 to exit: ";
      cin>> pend;
      if ((int)pend == 1) goto restart;// checks answer
      else if ((int)pend == 2) goto exit;
      else goto idiot;
    //If you enter another number besides 1 or 2.
      idiot:
      while (cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t        ..::Error Invalid Menu Option::.. \n\n\t\t\t..::Close And Re-open Program::..\n\n\n\n\n\n\n\n\n\n"){ 
      int input;
        //I took the invalid input code form cprogramming.com 
        //It seems that when the program freaks it goes to the last part of main()
        //so i leave it here. 
        while ( !( cin>> input ) ) {
        // Clear the error state
        cin.clear();
    
        // Remove the unrecognized characters
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
        // Try again
        cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t   ..::Error Invalid Input::..\n\n\t\tDon't use Characters other then the following:\n\t\t\t      0 1 2 3 4 5 6 7 8 9 . \n\n\t\t\t..::Close And Re-open Program::..\n\n\n\n\n\n\n\n\n\n";
        }
      
     exit:
         return 0;
    }}
    float intCalc(float apy, float smoney, float yrinc, float y)
    {
      int w = 1;
      long double result;
       
      apy += 1;//adds 1 to apy
      //first year calculation. this NEEDS to be here or smoney is impossible to use in the calculation
      result = smoney * apy;
      result += yrinc;
      cout<< " year: "<< w << " = " << result << " \n" ;
      //following years loop
      do{
      result += yrinc;
      result = result * apy;
      w++;
      cout<< " year: "<< w << " = " << result << " \n" ;
      }while ( w < y );
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Work on loops. Start with something small, then build up from that. Your gotos should all be replaced with loops. You have a few while loops in there, but I'm not sure you understand how they work. For example, instead of goto restart, just use a loop that runs the code between the restart label and the goto until the user selects exit. The code can get messy, but you break it down with functions.

    For example, you can write a function that gets a value from the user. Start by moving just the code that asks for years. Then write a function that just asks for the value for the apy variable. If you see a pattern, you might be able to make a generic function and call it more than once.

    Also, you should be using double instead of float, since double is the default floating-type type in C++, you have no reason for needing float, and you might want the extra precision in some cases.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    >you might want the extra precision in some cases.

    Just to help you on this what daved suggested, you can set the precision of the decimal when you use double.

    Code:
    cout << "My value is: " << setprecision( 2 ) << fixed << value << endl;
    setprecision is defined in the

    Code:
    #include <iomanip>
    header file, and fixed - which ensures the deimal is shown, is defined in the iostream library.

    If you have a large set of values to precision, you can skip having to do each one individually by simple entering

    Code:
    cout << fixed << setprecision( 2 );
    before you display them. This saves time and
    can greatly improve proram readability
    Double Helix STL

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I was actually referring to the precision of the type, double is more accurate than float as the number of significant digits increases. But the setprecision manipulator advice is good, too.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    Okay but now i have a bug

    algright here's my code now.

    My problem lies with fuction error. It works to stop invalid input from makeing
    hell on my screen but now the user has to enter data in twice.

    compile it and see what i mean

    oh and ty btw for the help.

    EDIT: i commented it for easier reading

    Code:
    /* Compound interest calculator. For stuff like investment accounts
    (C)2006 Shane Loretz. 
    */
    #include <iostream>
    #include <ios>
    #include <limits>
    #include <iomanip>
    
    using namespace std;
    
    double intCalc(double apy, double smoney, double yrinc, double y);
    double error(double); 
    double yAsk(double);
    double apyAsk(double);
    double smoney_yrinc(double);
    int ques(int);
    int idiot(int);
    
    
    int main()
    { 
      cout<<"--------------------------------------------------------------------------------";//looks nice huh
      cout<<"\nCompound Interest Calculator \n (C)2006 Shane Loretz\n\n";
      cout<<"--------------------------------------------------------------------------------";
      int restart;
    do{ //for when prompted to do another calc
      double errorCheck;
      double y;
      double apy = 101;
      double smoney;
      double yrinc;
      
      //user input these functions should be self explanatory
    
      y = yAsk(y);
      apy = apyAsk(apy);
        cout<<"\nPlease enter the initial money in the account: ";
      smoney = smoney_yrinc(smoney);  
        cout<<"\nPlease enter how much you are going to add to your accout per year.\n \
        Can also be negative if you take money out each year: ";
      yrinc = smoney_yrinc(yrinc);
      intCalc(apy, smoney, yrinc, y); //calculates recieved data
      //exit or redo question
    
      restart = ques(restart); //restart function
      
    } while (restart > 10); //for when prompted to do another calc
    return 0;
    //If you enter another number besides 1 or 2.
      
        int input;
        //I took the invalid input code form cprogramming.com 
        //It seems that when the program freaks it goes to the last part of main()
        //so i leave it here. 
        while ( !( cin>> input ) ) {
        // Clear the error state
        cin.clear();
    
        // Remove the unrecognized characters
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
        
        cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t   ..::Error Invalid Input::..\n\n\t\tDon't use Characters other then the following:\n\t\t\t      0 1 2 3 4 5 6 7 8 9 . \n\n\t\t\t..::Close And Re-open Program::..\n\n\n\n\n\n\n\n\n\n";
        }
      
     exit:
         return 0;
    }
    double intCalc(double apy, double smoney, double yrinc, double y)
    {
      int w = 1;
      long double result;
       
      apy += 1;//adds 1 to apy
      //first year calculation. this NEEDS to be here or smoney is impossible to use in the calculation
      result = smoney * apy;
      result += yrinc;
      cout<< fixed << setprecision( 2 ) << " year: "<< w << " = " << result << " \n" ;
      //following years loop
      do{
      result += yrinc;
      result = result * apy;
      w++;
      cout<< fixed << setprecision( 2 ) << " year: "<< w << " = " << result << " \n" ; //prints result fixed and setprecision ensure 2 decimal places will be used
      }while ( w < y );
      return 0;
    }
    
    double error(double input) //checks for invalid input
    {
        while ( !( cin>> input ) ) {
        // Clear the error state
        cin.clear();
    
        // Remove the unrecognized characters
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
        cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t   ..::Error Invalid Input::..\n\n\t\tDon't use Characters other then the following:\n\t\t\t      0 1 2 3 4 5 6 7 8 9 . \n\n\t\t\t..::Close And Re-open Program::..\n\n\n\n\n\n\n\n\n\n";
        }
        return input;
    } 
    double yAsk(double y) //gets year data from user
    {
        y = 151;
        while (y >= 150){ 
        cout<<"\nPlease enter how many years till you collect the money (At least 2): ";
        cin>> y;
        y = error(y); //checks for invalid input
        if (y >= 150) {cout<<"\n Please keep the years below 150\n\n";} //limits entry size
        return y;
        }
    }
    double apyAsk(double apy) //gets anual percent yeild data from user
    {
           while (apy >= 100){  
           cout<<"\nPlease enter your expected anual interest in decimal form.\n Example: 5% = 0.05: ";
           cin>> apy ;                
           apy = error(apy);//checks for invalid input
           {if (apy >= 100) cout<<"\n Please keep interest below 100.0 or %10,000\n\n";} // keeps idiots from making the program output huge numbers
           }  
    }
    double smoney_yrinc(double value) //used for both start money and yearly deposites
    {
           cin>> value;
           value = error(value);//checks for invalid input
           return value;
    }
    int ques(int restart)
    {
         int pend; //pend variable should be with other variables but i like it here
         cout<<"\nPress 1 to do another calculation or press 2 to exit: ";
         cin>> pend;
         if ((int)pend == 1) {
                    restart = 11; //anything more then ten restarts program
                    }// checks answer
         else if ((int)pend == 2) {
                    restart = 1; //anything less then 10 closes program
                    cout<<"Good bye";
                    }
         else 
         {
           idiot(pend); //anything else and the user goes to idiot
           cin>>pend; //i don't remember why i did this
         }
         return restart; //sends restart data back to mainto evaluae whether the program is closed or not
    }
    int idiot(int x) //for the idiots that can't press 1 or 2.
    {
        cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t        ..::Error Invalid Menu Option::.. \n\n\t\t     ..::Please Close and Reopen Program::..\n\n\n\n\n\n\n\n\n\n"; 
    }
    Last edited by A10; 11-29-2006 at 08:06 PM.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    *Bump*

    still haveing trouble with the user haveing to enter input twice. can anyone help?
    Last edited by A10; 11-29-2006 at 07:59 PM.

  7. #7
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    1) No, its not.
    2) Indetation.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    Quote Originally Posted by manutd
    1) No, its not.
    2) Indetation.
    what?

    oh you saw the post b4 i edited it.

    do you know why my function error is makeing the user input twice?

  9. #9
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Because this is here:
    Code:
     while ( !( cin>> input ) ) {
        // Clear the error state
        cin.clear();
    
        // Remove the unrecognized characters
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
        
        cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t   ..::Error Invalid Input::..\n\n\t\tDon't use Characters other then the following:\n\t\t\t      0 1 2 3 4 5 6 7 8 9 . \n\n\t\t\t..::Close And Re-open Program::..\n\n\n\n\n\n\n\n\n\n";
        }
    THis is identical to your error() function.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    Okay i removed that but the program still makes the user input data twice. It doesn't happed if i don't call the error() function.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Because you are asking for input, then calling the error function that asks for input again.

    Remove the calls to cin >> before the calls to the error function.

  12. #12
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    Okay that works thanks for that.

    here's my code. everything works. thanks guys

    Code:
    /* Compound interest calculator. For stuff like investment accounts
    (C)2006 Shane Loretz. 
    */
    #include <iostream>
    #include <ios>
    #include <limits>
    #include <iomanip>
    
    using namespace std;
    
    double intCalc(double apy, double smoney, double yrinc, double y);
    double error(double); 
    double yAsk(double);
    double apyAsk(double);
    double smoney_yrinc(double);
    int ques(int);
    int idiot(int);
    
    
    int main()
    { 
      cout<<"--------------------------------------------------------------------------------";//looks nice huh
      cout<<"\nCompound Interest Calculator \n (C)2006 Shane Loretz\n\n";
      cout<<"--------------------------------------------------------------------------------";
      int restart;
    do{ //for when prompted to do another calc
      double errorCheck;
      double y;
      double apy = 101;
      double smoney;
      double yrinc;
      
      //user input these functions should be self explanatory
    
      y = yAsk(y);
      apy = apyAsk(apy);
        cout<<"\nPlease enter the initial money in the account: ";
      smoney = smoney_yrinc(smoney);  
        cout<<"\nPlease enter how much you are going to add to your accout per year.\n \
        Can also be negative if you take money out each year: ";
      yrinc = smoney_yrinc(yrinc);
      intCalc(apy, smoney, yrinc, y); //calculates recieved data
      //exit or redo question
    
      restart = ques(restart); //restart function
      
    } while (restart > 10); //for when prompted to do another calc
    return 0;
    //If you enter another number besides 1 or 2.
      
        
        
    }
    
    double intCalc(double apy, double smoney, double yrinc, double y)
    {
      int w = 1;
      long double result;
       
      apy += 1;//adds 1 to apy
      //first year calculation. this NEEDS to be here or smoney is impossible to use in the calculation
      result = smoney * apy;
      result += yrinc;
      cout<< fixed << setprecision( 2 ) << " year: "<< w << " = " << result << " \n" ;
      //following years loop
      do{
      result += yrinc;
      result = result * apy;
      w++;
      cout<< fixed << setprecision( 2 ) << " year: "<< w << " = " << result << " \n" ; //prints result fixed and setprecision ensure 2 decimal places will be used
      }while ( w < y );
      return 0;
    }
    
    double error(double input) //checks for invalid input
    {
        while ( !( cin>> input ) ) {
        // Clear the error state
        cin.clear();
    
        // Remove the unrecognized characters
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
        cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t   ..::Error Invalid Input::..\n\n\t\tDon't use Characters other then the following:\n\t\t\t      0 1 2 3 4 5 6 7 8 9 . \n\n\t\t    ..::Press any number plus enter to retry::..\n\n\n\n\n\n\n\n\n\n";
        }
        return input;
    } 
    double yAsk(double y) //gets year data from user
    {
        y = 151;
        while (y >= 100){ 
        cout<<"\nPlease enter how many years till you collect the money (At least 2): ";
        //cin>> y;
        y = error(y); //checks for invalid input
        if (y >= 100) {cout<<"\n Please keep the years below 100\n\n";} //limits inputed data size
        return y;
        }
    }
    double apyAsk(double apy) //gets anual percent yeild data from user
    {
           while (apy >= 10){  
           cout<<"\nPlease enter your expected anual interest in decimal form.\n Example: 5% = 0.05: ";
           //cin>> apy ;                
           apy = error(apy);//checks for invalid input
           {if (apy >= 10) cout<<"\n Please keep interest below 10.0 or %1,000\n\n";} // keeps idiots from making the program output huge numbers
           }
           return apy;  
    }
    double smoney_yrinc(double value) //used for both start money and yearly deposites
    {
           //cin>> value;
           value = error(value);//checks for invalid input
           return value;
    }
    int ques(int restart)
    {
         int pend; //pend variable should be with other variables but i like it here
         cout<<"\nPress 1 to do another calculation or press 2 to exit: ";
         //cin>> pend;
         pend = error(pend);
         if ((int)pend == 1) {
                    restart = 11; //anything more then ten restarts program
                    }// checks answer
         else if ((int)pend == 2) {
                    restart = 1; //anything less then 10 closes program
                    cout<<"Good bye";
                    }
         else 
         {
           idiot(pend); //anything else and the user goes to idiot
           cin>>pend; //i don't remember why i did this
         }
         return restart; //sends restart data back to main to evaluate whether the program is closed or not
    }
    int idiot(int x) //for the idiots that can't press 1 or 2.
    {
        cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t        ..::Error Invalid Menu Option::.. \n\n\t      ..::Press any number plus enter to close program::..\n\n\n\n\n\n\n\n\n\n"; 
    }
    now i'm gonna work on other options like quarterly interest instead of anually. and maybe instananeous interest

  13. #13
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    >cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t

    You could put all those newline characters in one loop

    Code:
    for ( int i = 0; i < 16; i++ )
    {
       cout << "\n";
    }
    Double Helix STL

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    Wow i didn't realise i had written that many.


    I'll try that thanks

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Even better (at least when you learn it), use a string:
    Code:
    cout << std::string(16, '\n');

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Menu program advice
    By Ne0 in forum C Programming
    Replies: 4
    Last Post: 12-04-2004, 04:27 AM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM