• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

argh need help bad in C++.

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

-=HN=- Wild9

Member
Joined
Sep 10, 2001
Location
Dayton, Ohio
nm after awhile of beating my head against the wall, i figured it out. hehe but here is the program if anyone wants to take a peek at the source. of course, this is just copied and pasted here so its not indented at all. and yeah my professor is a real stickler about commenting every line to clarify stuff.

/* James Martin
* CS 240
* Section 1
* Programming Project 3, Displaying simple graphic shapes using text symbols,
* based off of user input.
*/




#include <iostream>



using namespace std;

void box (); // The Box shape

void right (); // The Right Triangle shape

void isos (); // The Isosceles Triangle shape

void diamond (); // The Diamond shape

char getCommand ( char cmd); // To select the shape




void main()
{
char cmd;

do
{
cmd = getCommand (cmd);

switch( cmd )
{
case 'b': box (); break; // Calls void box ()

case 'B': box (); break; // Calls void box ()

case 'r': right(); break; // Calls void right ()

case 'R': right(); break; // Calls void right ()

case 'i': isos(); break; // Calls void isos ()

case 'I': isos(); break; // Calls void isos ()

case 'd': diamond(); break; // Calls void diamond ()

case 'D': diamond(); break; // Calls void diamond ()

case 'q': break; // Quits program

case 'Q': break; // Quits program

default: cout << "Invalid command " << cmd << " -- try again" << endl;
}
} while( cmd != 'q' );
}


void box () // calls the function to display the box
{
int r = 0; // number of rows in the box

int c = 0; // number of columns in the box

int rCount = 0; // number of iterations for rows

int cCount = 0; // number of iterations for columns

int i = 0; // number of iterations for loop

while ( r <= 0 || c <= 0 || c >= 80 ) // limitations of the rows and columns
{
i++;

if ( i >= 4 )
{
cout << "Too many invalid entries." << endl; // checks 3 times for invalid input

break; // drops user back to main menu
}

cout << "Enter the number of rows and columns (r,c): " << endl;

cin >> r >> c ; // Gets rows and columns

system ("cls"); // clears output screen
}

if ( i < 4) // If error check does not exceed 3, the box shape will be displayed
{
while ( rCount < r ) // loop to display box shape of desired rows and columns

{

while ( cCount < c )

{

cout << "* ";

cCount++;

}
cout << endl;

cCount = 0;

rCount++;


}

}




system ("pause"); //prompts user to strike a key to continue

system ("cls"); // clears the output screen

}


void right () // calls the function to display the right triangle
{
int b=0; // Base of the Right Triangle

int bCount=0; // Number of iterations for triangle loop

int rCount = 0; // number of iterations for columns

int i=0; // Number of iterations for error check loop

while ( b <=0 || b >= 80 ) //limitations of right triangle
{
i++;

if (i >=4 )
{
cout << "Too many invalid entries." << endl; // error checking

break; // drops user back to main menu
}
cout << "Enter the length of the base: " << endl;

cin >> b ; // length of the base of right triangle

system ("cls"); // clears output screen
}
if (i < 4)
{
while ( bCount < b)
{

while (rCount <= bCount)
{
cout << "* " ;


rCount++;

}
rCount = 0;

bCount++;

cout << endl;

}


}


system ("pause");

system ("cls");

}

void isos () // calls the function to display the isosceles triangle
{
int b = 0; // legth of the base of the isosceles triangle

int cCount = 0; // number of iterations for columns

int rCount = 1; // number of iterations for rows

int sCount = 0; // number of iterations for triangles

int i = 0 ; // number of iterations for error check loop

while ( b <= 0 || b >= 80 || (b % 2) == 0 )

{
i++;

if (i >=4)

{
cout << "Too many invalid entries." << endl;

break; // drops user back to main menu

}

cout << "Enter the length of the base: " ;

cin >> b;

system ("cls");



}

if (i < 4)
{

int z = (b - 1) / 2;

int q = (b + 1) / 2;


while (rCount <= q)

{

while (sCount < z)

{
cout << " ";

sCount++;
}

int nDex = ((rCount * 2) - 1);

int yDex = 0;

while(yDex < nDex)
{
cout << "*";

yDex++;
}

sCount = 0;

while (sCount < z)
{
cout << " ";

sCount++;
}


z--;

sCount = 0;

rCount++;

cout << endl;
}

}



system ("pause");

system ("cls");





}

void diamond () //calls function to display diamond shape
{

int b = 0; // width of the widest part of diamond

int cCount = 0; //number of iterations for columns

int rCount = 1; // number of iterations for rows

int sCount = 0; // number of iterations for triangles

int i=0; // number of iterations for error check loop

while ( b <= 0 || b >= 80 || (b % 2) == 0 )

{
i++;

if (i >=4)

{
cout << "Too many invalid entries." << endl;

break; // drops user back to main menu

}

cout << "Enter the width of the widest part of the diamond: " ;

cin >> b; // width of the widest part of diamond

system ("cls"); // clears the output screen



}
if (i < 4)
{



int z = ( b - 1 ) / 2;

int q = ( b + 1 ) / 2;


while ( rCount <= q )
{
while (sCount < z)
{
cout << " ";

sCount++;
}

int nDex = ( ( rCount * 2 ) - 1 );

int yDex = 0;



while(yDex < nDex)
{
cout << "*";

yDex++;


}

sCount = 0;

while (sCount < z)
{
cout << " ";

sCount++;
}






z--;

sCount = 0;

rCount++;

cout << endl;
}


z = ((b - 1) / 2);

q = (b + 1) / 2;

cCount = 0;

rCount = 1;

sCount = 0;

while (rCount <= z)
{
sCount = 0;

while (sCount < rCount)
{
cout << " ";

sCount++;
}

int nDex = (b - (rCount * 2));

int yDex = 0;

while(yDex < nDex)
{
cout << "*";

yDex++;
}


sCount = 0;

while (sCount < rCount)
{
cout << " ";

sCount++;
}

rCount++;

cout << endl;
}


}
system ("pause");

system ("cls");


}

char getCommand (char cmd) // prompts user to input which menu to go to
{

cout << "Enter a command (b, r, i, d, q): " << endl;

cin >> cmd; // which menu to take user to





return cmd; // passes command to main ()
}
 
Last edited:
The only thing I noticed(with a quick glance) is that you may want to do a ucase around your switch condition. This would pretty much cut your switch statement in half as you would only have to test capitals(or lowercase if you cose to go that route).
 
Also, you may want to look at using some constants instead of literals.

Commenting return statements such as "return cmd; // passes command to main ()" is fairly useless. For someone who doesn't know C++ it might help but for anyone else its superflous. Unecessary comments make the code harder to read. But hey, who cares if it gets good marks hey?
 
I used to lose tons of points for commenting like a professional, instead of commenting every line. I took the point loss as proof that I was right... I never got a program back that the professor didn;t undertand.
 
yeah i know what ya mean, the project before this that i had to do was about calculating the surface area of a wierd shaped building. here is a sample of my variable names

radius
sabox
sadome
area
length
width
height

and i had points deducted for not commenting what the variable names meant :rolleyes:
 
Sometimes I wonder about "professors" who insist on this. I wonder if they have ever coded in industry. I mean, if I declare this:

class c
{

public:

double ca();

.....

private:

int r;

}

then you would expect coments but using appropriate naming conventions such as:

class circle
{

public:

double CalculateArea();

.....

private:

int radius;

}

to me is self explanatory. If you comment every line then it will take alot longer to code, which matters in industry, but not to the Prof coz he's not paying you for it!!!
 
VeryFirstSMP, that was exactly my point. I've always used a standardized naming convention. lol... but I still lots points. I once asked the professor if he had problems reading the code... that didn't go over very well at all.
 
Back