C++ Quickie Question

OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
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.

Comments

  • SkulkBaitSkulkBait Join Date: 2003-02-11 Member: 13423Members
    darnit dude, for the sake of humanity, kill your braindead teacher. Please. I'm pretty sure that function prototypes are only valid for the scope they're declaired in, so they should go at the highest scope they're needed (in your case, global), so I think you'd need to declare it in each function since your doing it the brain dead way. If you move the GetSize definition to the top you don't need to prototype it at all though.

    Also, that last else in GetSize doesn't need to exist.
  • CrystalSnakeCrystalSnake Join Date: 2002-01-27 Member: 110Members
    edited December 2003
    The getSize function returns an int, but that int is never used.
    The getSize function tries to modify its parameter, but that doesn't work because it's a value parameter.
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    edited December 2003
    [edit]oops[/edit]

    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-->
  • taboofirestaboofires Join Date: 2002-11-24 Member: 9853Members
    When forced to not use globals, unroll your code. Just insert the body of the function into where it's called. It's contrived, but it will work, and you wouldn't likely have that contsraint in the real world.
  • GwahirGwahir Join Date: 2002-04-24 Member: 513Members, Constellation
    perhaps an inline, or definition would be allowed?
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    <!--QuoteBegin--CrystalSnake+Dec 3 2003, 09:27 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (CrystalSnake @ Dec 3 2003, 09:27 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> The getSize function returns an int, but that int is never used.
    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
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    edited December 2003
    <!--QuoteBegin--SoulSkorpion+Dec 3 2003, 10:35 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (SoulSkorpion @ Dec 3 2003, 10:35 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> [edit]oops[/edit]

    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)
  • FieariFieari Join Date: 2002-10-22 Member: 1566Members, Constellation
    Just asking for clarification, your problem is that you're not allowed to prototype (why not? Even professionals do, although they tend to stick all the prototypes in a seperate file) and you have one function you want to call in a bunch of other functions?

    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)
Sign In or Register to comment.