C++ Quickie Question
OttoDestruct
Join Date: 2002-11-08 Member: 7790Members

in Off-Topic
Ok heres teh code
<!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
void PatternOne (void)
{
// Prototype Declaration
int getSize (int);
// Local Declaration
int size;
// Call
getSize (size);
for (x=1; x <= size; x++)
{
int y;
y = size - x;
cout << setfill($) << setw(x) << size << setw(y) << "\n";
}
return;
}
void PatternTwo (void)
{
// Prototype Declaration
int getSize (int);
// Local Declaration
int size;
// Call
getSize (size);
for (x=1; x <= size; x++)
{
int y;
y = size - x;
cout << setfill($) << setw(y) << size << setw(x) << "\n";
}
return;
}
void PatternThree (void)
{
// Prototype Declaration
int getSize (int);
// Local Declaration
int size;
// Call
getSize (size);
return;
}
void PatternFour (void)
{
// Prototype Declaration
int getSize (int);
// Local Declaration
int size;
int z;
// Call
getSize (size);
for (x = 1; x <= size; x++)
{
for (y = 0; y <= size; y++)
{
if (y == 0)
cout << setfill($) << setw(size) << "\n"
else if (size >= y)
cout << size;
else
{
z = size - y
cout << setfill('$') << setw(z) << "\n"
}
}
}
return;
}
int getSize (int size)
{
cout << "Enter an option between 2 and 9: ";
cin >> size;
if (size > 9)
cout << "Incorrect option. Please choose between 2 and 9.";
else if (size < 2)
cout << "Incorrect option. Please choose between 2 and 9.";
else return size;
return size;
}
<!--c2--></td></tr></table><span class='postcolor'><!--ec2-->
Yes I know PatternThree isnt filled in all the way, its a "group" program and the guy hasnt given it to me yet (I say group in quotes because the other two people's code was HORRIBLE and I had to redo theirs completely). The question is, since teacher doesnt let us use global prototype declarations, after the PatternOne do I have to keep declaring getSize or can I just declare it once.
<!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
void PatternOne (void)
{
// Prototype Declaration
int getSize (int);
// Local Declaration
int size;
// Call
getSize (size);
for (x=1; x <= size; x++)
{
int y;
y = size - x;
cout << setfill($) << setw(x) << size << setw(y) << "\n";
}
return;
}
void PatternTwo (void)
{
// Prototype Declaration
int getSize (int);
// Local Declaration
int size;
// Call
getSize (size);
for (x=1; x <= size; x++)
{
int y;
y = size - x;
cout << setfill($) << setw(y) << size << setw(x) << "\n";
}
return;
}
void PatternThree (void)
{
// Prototype Declaration
int getSize (int);
// Local Declaration
int size;
// Call
getSize (size);
return;
}
void PatternFour (void)
{
// Prototype Declaration
int getSize (int);
// Local Declaration
int size;
int z;
// Call
getSize (size);
for (x = 1; x <= size; x++)
{
for (y = 0; y <= size; y++)
{
if (y == 0)
cout << setfill($) << setw(size) << "\n"
else if (size >= y)
cout << size;
else
{
z = size - y
cout << setfill('$') << setw(z) << "\n"
}
}
}
return;
}
int getSize (int size)
{
cout << "Enter an option between 2 and 9: ";
cin >> size;
if (size > 9)
cout << "Incorrect option. Please choose between 2 and 9.";
else if (size < 2)
cout << "Incorrect option. Please choose between 2 and 9.";
else return size;
return size;
}
<!--c2--></td></tr></table><span class='postcolor'><!--ec2-->
Yes I know PatternThree isnt filled in all the way, its a "group" program and the guy hasnt given it to me yet (I say group in quotes because the other two people's code was HORRIBLE and I had to redo theirs completely). The question is, since teacher doesnt let us use global prototype declarations, after the PatternOne do I have to keep declaring getSize or can I just declare it once.
Comments
Also, that last else in GetSize doesn't need to exist.
The getSize function tries to modify its parameter, but that doesn't work because it's a value parameter.
If you aren't allowed to use global prototype declarations (<b>why, for the love of god, WHY?!!?!</b> *sob*) then arrange the functions in order so that they're defined before they're used.
<!--QuoteBegin--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->(I say group in quotes because the other two people's code was HORRIBLE and I had to redo theirs completely)<!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
No offense, but I shudder to think of what their code looked like if it's horrible by comparison to this <!--emo&:(--><img src='http://www.unknownworlds.com/forums/html/emoticons/sad.gif' border='0' style='vertical-align:middle' alt='sad.gif'><!--endemo-->
The getSize function tries to modify its parameter, but that doesn't work because it's a value parameter. <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
Yea I saw that and fixed it
If you aren't allowed to use global prototype declarations (<b>why, for the love of god, WHY?!!?!</b> *sob*) then arrange the functions in order so that they're defined before they're used.
<!--QuoteBegin--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->(I say group in quotes because the other two people's code was HORRIBLE and I had to redo theirs completely)<!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
No offense, but I shudder to think of what their code looked like if it's horrible by comparison to this <!--emo&:(--><img src='http://www.unknownworlds.com/forums/html/emoticons/sad.gif' border='0' style='vertical-align:middle' alt='sad.gif'><!--endemo--> <!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
Here an example
<!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
void paternFour(int pattern, int size)
{
? int x, y;
? cout<<setfill('$')<<setw(size);
? for(x = 1; x <= size; x++)
? ? {
? ? ?y = 1;
? ? ?cout<<setfill('5')<<setw(y)<<setfill('$')<<setw(size--)<<"\n";
? ? ?y--;
? ? }
}
<!--c2--></td></tr></table><span class='postcolor'><!--ec2-->
I know it doesnt really look THAT bad but when you see what the pattern is supposed to be its HORRIBLE.
Er.. here what it sposed to be
$$$$$
5$$$$
55$$$
555$$
5555$
55555
The 5 is supposed to be what the person enters for the width of the pattern (i.e. width of 3 would display 3)
Because if that's the only problem, the solution is VERY simple, and has already been pointed out, I think. Move the getSize function from the bottom of the file to the top, and get rid of all the local prototypes.
Heck, until this moment, I wasn't even aware you COULD locally prototype functions like that... learn something new every day, and I've been coding C++ for 3 years now... and teach it for a (crappy) living.
Even if you CAN locally prototype, please never do this. As I said, I teach C/C++. I would mark a student down SO HARD if I ever saw them trying this. Oooh, the very thought makes me steamed. It's UGLY! U-G-L-Y! HIDEOUS! ALMOST AS BAD AS USING A GOTO STATEMENT (which would equal an instant fail if I ever saw it in any C/C++ code)