Thread: new to Void functions and loops

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    2

    Question new to Void functions and loops

    I need to print a box out on the screen.
    I am learning void functions (and have not learned about for yet)
    I am able to print the top and bottom lines, but I just can not figure out how to get the 2 middle lines to print. I am giving this information in my instructions:
    the interior dimensions are (numSigns-2) * (numSigns /2-2)
    and the program promts for the number of $ for the top of the box. That number /2-2 lines are printed with $ on the sides;
    Here is what i have so far:
    void Print(int numSigns);


    // FILL IN documentation.

    int main ()
    {
    int number;

    cout << "Enter the number of dollar signs for the top; "
    << "press return. " << endl;
    cout << "Enter end-of-file characer to quit." << endl;
    cin >> number;
    while (cin)
    {
    /* FILL IN call to Print */

    Print(number);
    cout << endl;
    Print(number);
    cout << endl;

    cout << "Enter the number of dollar signs for the top; "
    << "press return. " << endl;
    cout << "Enter end-of-file characer to quit." << endl;
    cin >> number;
    }
    return 0;
    }

    //*******************************************

    void Print(int numSigns)
    // FILL IN documentation.
    {
    /* FILL IN code to print numSigns $'s */

    int count;
    count = 0;

    while (count <= numSigns)
    {
    cout << "$";
    count++;
    }
    //********************************************

    /* FILL IN code to print (numSigns / 2)-2 lines with */
    /* $'s lining up under the left-most and right-most */
    /* $ ones on the top line. */

    /* FILL IN code to print numSigns $'s */

    }

    Please help....I have been at this all day!
    Last edited by Loraine13; 12-02-2001 at 01:02 AM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Is this what you need?
    Code:
    void printBox(void){
        int n;
        cout << "How many lines? (1-40): ";
        cin >> n;
        for(int i = 0; i < n; i++){
            for(int j = 1; j <= 2*n; j++)
                cout << (i > 0 && i < n - 1 && j > 1 && j < 2 * n ? ' ' : '$');
            cout << endl;
        }
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    2

    too advanced for this beginner

    Woah....that is way to advanced for me. We have not done FOR.....
    is there a way to do the function with a loop? if-else? or while?
    Thanks, in advance for ANY help with this!

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    46
    FOR is a loop, it just looks different than a while, these two loops are equivalent.
    Code:
    // a for loop
    for(int i=0; i<10; i++){
        cout << i << endl;
    }
    
    // a while loop
    int i = 0;
    while(i<10){
        cout << i << endl;
        i++;
    }
    Here's the same program simplified a bit
    Code:
    void printBox(){
        int n;
        cout << "How many lines? (1-40): ";
        cin >> n;
        int i = 0;
        while(i < n){
            int j = 1;
            while(j <= 2*n){
                // && means AND. This if has 4 conditions to be met
                if(i > 0 && i < n - 1 && j > 1 && j < 2 * n){
                    cout << ' ';
                }else{
                    cout << '$';
                }
            }
            cout << endl;
        }
    }
    C code. C code run. Run code, run...please!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with functions and for loops
    By slava in forum C Programming
    Replies: 1
    Last Post: 11-08-2008, 01:30 PM
  2. Loops or recursive functions?
    By mariano_donati in forum C Programming
    Replies: 5
    Last Post: 05-12-2008, 12:41 PM
  3. Loops in functions?
    By AmbliKai in forum C Programming
    Replies: 16
    Last Post: 11-29-2007, 06:33 AM
  4. help with functions and array please
    By rebel in forum C++ Programming
    Replies: 4
    Last Post: 11-24-2005, 01:09 PM
  5. i dunno how to do loops and functions together
    By Noobie in forum C++ Programming
    Replies: 30
    Last Post: 02-03-2003, 06:05 PM