PDA

View Full Version : First Solo Program: Lame



RoD
09-27-2002, 12:37 PM
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.



/* 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;
}

Prelude
09-27-2002, 01:19 PM
>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.


/*
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

glowstick
09-27-2002, 01:19 PM
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 !!

salvelinus
09-27-2002, 01:29 PM
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.