Thread: First Solo Program: Lame

  1. #1
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

    First Solo Program: Lame

    Hey i know this program is killer lame i just want opinions on my structure, methods, habits etc. Maybe a better way i could have done something ya know. The comments are pretty explanatory, but i don't think anyone will have trouble figuring this one out.

    Code:
    /*	Steve Billington
    	September 26, 2002
    	DB.cpp
    	Demonstrates two basic functions to user.
    */
    
    /* Declare preprocessor directives*/
    #include <iostream.h>
    #include <iomanip.h>
    
    
    /* Constant used in if/for example initiated to 5*/
    const int MAIN_CONST = 5;
    
    /* Constant for first for loop in nest for example*/
    const int FIRST_LOOP = 5;
    
    /* Constant for nested loop*/
    const int SECOND_LOOP = 3;
    
    
    int main ()
    
    {
    
    	/* Declare a character variable to hold users input*/
    	char getinput;
    
    	/* Declare double variables for nested for example*/
    	double input,final;
    
    	/* As user for a choice of which to demonstrate and store in
    	variable "getinput"*/
    	cout <<"To see an example of one of the following type the appropriate letter and press enter :"<<endl;
    
    	cout <<"Manipualting input numbers (A) and for Nested for (B): ";
    
    	cin >>getinput;
    
    
    		/* If statements to decide what example is to be done*/
    		if (getinput == 'A') 
    		{
    			
    			int j;
    
    			for (j=1; j <= MAIN_CONST; ++j)
    			{
    			
    				cout <<"Enter a number and press enter: ";
    
    				cin >>input;
    
    				cout <<"Input was "<<input;
    
    				final += input;
    
    				cout <<"Final is "<<final;
    
    			}
    
    						
    		}
    
    		if (getinput == 'a')
    		{
    			
    			int j;
    
    			for (j=1; j <= MAIN_CONST; ++j)
    			{
    			
    				cout <<"Enter a number and press enter: ";
    
    				cin >>input;
    
    				cout <<"Input was "<<input<<endl;
    
    				final += input;
    
    				cout <<"Final is "<<final<<endl;
    
    			}
    
    			
    		}
    
    
    		if (getinput == 'B') 
    		{
    			double output;
    
    			int j2,j3;
    
    			for (j2=1; j2 <=FIRST_LOOP; ++j2)
    			{
    				for (j3=1; j3 <=SECOND_LOOP; ++j3)
    				
    					cout <<j2<<endl;
    
    					cout <<j3<<endl;
    
    					output = j2 * j3;
    
    					cout <<endl;
    
    					cout <<output;
    				
    			}
    		}
    
    		if (getinput == 'b') 
    		{
    			double output;
    
    			int j2,j3;
    
    			for (j2=1; j2 <=FIRST_LOOP; ++j2)
    			{
    				for (j3=1; j3 <=SECOND_LOOP; ++j3)
    				
    					cout <<j2<<endl;
    
    					cout <<j3<<endl;
    
    					output = j2 * j3;
    
    					cout <<endl;
    
    					cout <<output;
    				
    			}
    		}
    
    		/* Make nice amount of room so screen is easily read*/	
    		cout <<endl;
    		cout <<endl;
    		cout <<endl;
    		cout <<endl;
    
    
    		/* Thank the user for trying the program*/
    		cout <<"Thanks for useing my program!"<<endl;
    
    		/* Return value for main*/
    		return 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i just want opinions on my structure
    Not bad, but you could use more whitespace in between tokens for readability purposes. Most people find this

    a = b + c;

    easier to read than this

    a=b+c

    >methods
    You need to be more consistent as well as aware of how control structures can be used to decrease the amount of code you use. The biggest problem with this program is you duplicate both operations twice, only because you want to handle the user entering a lower case option. There are better ways.

    >habits etc
    Nothing terrible, just choose a style that you find readable and use it consistently. I also suggest that you put more thought into your variable names, j2 and j3 just won't cut it in any non-trivial program.

    >The comments are pretty explanatory
    Yes, too explanatory. Don't comment code that documents itself.
    Code:
    /*
    Steve Billington
    Edited by Julienne Walker
    September 26, 2002
    DB.cpp
    Demonstrates two basic functions to user.
    */
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    /* Arbitrary loop limits */
    const int MAIN_CONST = 5;
    const int FIRST_LOOP = 5;
    const int SECOND_LOOP = 3;
    
    int main ()
    {
      char getinput;
      
      cout<<"(A) Manipulating input numbers\n"
            "(B) Nested for loop ";
      cout<<"Choose an example to run: ";
      cin>>getinput;
      
      /* Perform the chosen action*/
      if (getinput == 'A' || getinput == 'a') 
      {
        double input, final = 0;
        
        for (int j = 1; j <= MAIN_CONST; ++j)
        {
          cout<<"Enter a number and press enter: ";
          cin>>input;
          final += input;
          cout<<"Input was "<< input <<". Final is "<< final <<endl;
        }			
      }
      else if (getinput == 'B' || getinput == 'b') 
      {
        int j3;
    
        for (int j2 = 1; j2 <= FIRST_LOOP; ++j2)
        {
          for (j3 = 1; j3 <= SECOND_LOOP; ++j3)
            cout<< j2 <<' ';
    
          cout<< endl << j3 <<endl;
          cout<<"Output is: "<< j2 * j3 << endl <<endl;
        }
      }	
      else
        cerr<<"Invalid choice"<<endl;
    
      cout<<"\nThanks for using my program!"<<endl;
      
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    68
    u dont need to double space everything. that may be a side effect of u psting code into a word processor or int the "your reply" here. other than that, spiffy diffy lets get DRUNK !!
    "with a gun barrel between your teeth, you speak only in vowels."
    - tyler durden

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    One, you'll need to use \n or endl unless you want all your cout statements to run together.
    Two, since the code for both upper and lowercase is the same, you could use
    if (getinput == 'A' || getinput == 'a')
    and not have to code twice.
    Three, you should test for invalid entries. What if the user enters G, or 8, etc?
    Four, along with the above, use if...else if statements rather than consecutive ifs, or look at the case statement if you've gotten that far.
    Five, you don't use any functions from iomanip.h, you don't need it.
    Six, while it's nice of you to give some space to the user at the end and thank them, this program will just end after flashing the last cout statement. A user won't have time to read that. Lots of threads about that on the C++ programming board, where this post really should be.

    But, with the exception of one and six, possibly three, your program will run just the way you're probably expecting.
    Last edited by salvelinus; 09-27-2002 at 01:32 PM.
    Truth is a malleable commodity - Dick Cheney

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM