Any improvments that i could make to this program?

This is a discussion on Any improvments that i could make to this program? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; int main() { cout<<"Software Version 1.0"<<endl; system("color "); system("title Average number finder"); double dnumber1 ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    5

    Any improvments that i could make to this program?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
        cout<<"Software Version 1.0"<<endl;
        
        system("color ");
        
        system("title Average number finder");
        
        double dnumber1 = 0.0;
    
        double dnumber2 = 0.0;
    
        double dnumber3 = 0.0;
    
        double daverage = 0.0;
    
       cout<<"Please enter the number you would like averaged"<<endl;
    
       cin>>dnumber1;
       cin>>dnumber2;
       cin>>dnumber3;
    
          daverage =(dnumber1 + dnumber2 + dnumber3)/3;
          
          system("pause");
          
          system("cls");
      
          cout<<"The average:  "<< daverage<<endl;
    
          system("pause");
    
          system("cls");
          cout<<"Software Designer Mach1723"<<endl;
    
          cout<<"Software Version 1.0"<<endl;
    
          cout<<"Software Name Average Number Finder"<<endl;
    
          system("pause");
          system("cls");
          double dnumber4 = 0.0;
    
          double dnumber5 = 0.0;
    
          double dnumber6 = 0.0;
    
          double daverage1 = 0.0;
          cout<<"Please enter the number you would like averaged."<<endl;
    
          cout<<"if you dont wish to have number averaged again just exit the program"<<endl;
    
          cin>>dnumber4;
          cin>>dnumber5;
          cin>>dnumber6;
    
          daverage1 =(dnumber4 + dnumber5 + dnumber6)/3;
    
          system("pause");
          system("cls");
    
          cout<<"The average:  "<<daverage1<<endl;
          
          
    system("pause");
    system("cls");
    
    }
    any improvments anyone could think of?
    Last edited by Mach1723; 01-05-2008 at 01:26 PM. Reason: Spacing code for ease of reading

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Obviously you could use a loop to do the job more than one (or two) times.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    5
    Quote Originally Posted by anon View Post
    Obviously you could use a loop to do the job more than one (or two) times.
    i have tried a loop but i cant get it to work right with my code it either just makes the code system pause repeat or just says the average over and over again

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Then may-be post the code that you tried?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    5
    Heres the code i tried i dont even think i wrote it right
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
           int x = 0;
        while ( x < 10 ); 
      
        cout<<"Software Version 1.0"<<endl;
        
        system("color ");
        
        system("title Average number finder");
        
        double dnumber1 = 0.0;
        double dnumber2 = 0.0;
        double dnumber3 = 0.0;
        double daverage = 0.0;
       cout<<"Please enter the number you would like averaged"<<x<<endl;
       cin>>dnumber1;
       cin>>dnumber2;
       cin>>dnumber3;
          daverage =(dnumber1 + dnumber2 + dnumber3)/3;
          
          system("pause");
          
          system("cls");
      
          cout<<"The average:  "<< daverage<<x<<endl;
    
    
          system("pause");
          system("cls");
    }
    Last edited by Mach1723; 01-05-2008 at 02:00 PM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    Code:
    while (condition)
    {
    	// Code here
    	// More code here
    }
    Or
    Code:
    while (condition)
    	// Code here
    This is wrong:
    Code:
    while (condition);
    Will result is an endless loop.

    You should probably think of keeping your indentation consistent too. You're just indenting a little at will everywhere. Define a number of spaces (preferably 4) to serve as one indentation (or use a tab) and indent once for every block and only using the specified number of spaces and not more or less at some places.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Code:
    while (condition);
    Will result is an endless loop.
    not necessarily true.

    Code:
    int i = 10;
    while (i--);

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    Oh, right, right! I was thinking of while (x > 10) or something like that!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You could define your software version and name information as constants at the top of the program.

    You could comment your code.

    You could not clear the screen so the user could see the numbers they entered to get the average.

    You could tell the user that your program is looking for 3 numbers instead of one.

    You could reuse variables instead of creating new ones.

    You could tell the user HOW to cancel the program.

    You could clear the screen before you start couting information.

    You could capitalize your sentence and use an apostrophe in a proper contraction.

    You could code "return 0;" since you're not but you say you are.

    Todd

  10. #10
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    use functions or classes to have more managable code
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  11. #11
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    Quote Originally Posted by Hussain Hani View Post
    use functions or classes to have more managable code
    That's an overkill for something this simple

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Would you write the code in the same way if you were averaging 10 numbers? How about 50?

    Learn arrays. If you ever have more than 2 of the same thing that undergoes the same processing then it should be an array.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    + a loop will turn 50 variables into one.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with this program I'm trying to make
    By Sshakey6791 in forum C++ Programming
    Replies: 14
    Last Post: 12-01-2008, 04:03 PM
  2. Replies: 9
    Last Post: 06-17-2008, 12:38 PM
  3. want to make this small program...
    By psycho88 in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2005, 02:05 AM
  4. How To Make The Program Installed In Program Files Folder?
    By javacvb in forum Windows Programming
    Replies: 4
    Last Post: 11-05-2003, 05:33 PM
  5. any suggestions on how to make this program smaller?
    By bajan_elf in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2003, 04:24 AM

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