0

Hey guys I'm having trouble on placing my loops I actually want to make a square and I dont know how to manipulate my own placement because I am used to gotoxy(); and I think it only works in TurboC++... As of now I am using DevC++ .. Can you tell me what to do ??

    for(int q=15;q>0;q--){
        cout<<"*"<<endl;
    }
    for(int q=15;q>0;q--){
        cout.width(25);cout<<"*"<<endl<<setprecision(8)<<right;
    }
    for(int i=1; i<=50; i++){
         cout<<"*";
    }

Edited by Ma Nicole Ydralyn

5
Contributors
4
Replies
37
Views
2 Years
Discussion Span
Last Post by vegaseat
1

You're correct that gotoxy() is specific to the Borland compilers; there's no standard equivalent for it, exactly, but for this purpose, the code you have is good beginning. I would recommend making a few changes to get it working:

void draw_box(int width, char top, char sides, char bottom)
{
    cout << top 
         << setw(width - 2) << setfill(top)
         << top << endl;

    cout.fill(' ');
    for(int i = 0; i < width; i++)
    {
        cout << side
             << setw(width - 1) << right << side
             << endl;
    }

    cout << bottom
         << setw(width - 2) << setfill(bottom)
         << bottom << endl;
}

Edited by Schol-R-LEA

0

gotoxy let's you move unconditionally, but with standard output you need to print whitespace:

#include <iomanip>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int n = 15;

    cout << setfill('*') << setw(n + 1) << "\n";

    for (int i = 0; i < n - 2; i++)
    {
        cout << '*' + string(n - 2, ' ') + '*' << '\n';
    }

    cout << setfill('*') << setw(n + 1) << "\n";
}

If anything goes inside the box, care must be taken to align it properly as well as exclude whitespace characters that would otherwise be filled with non-whitespace characters. It can get kind of hairy:

#include <iomanip>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string data = "testy";
    int n = 15;

    cout << setfill('*') << setw(n + 1) << "\n";

    for (int i = 0; i < n - 2; i++)
    {
        string::size_type halfie = n / 2 - data.size() / 2 - 1;
        string x = '*' + string(halfie, ' ');

        x += data;
        x += string(halfie + (data.size() % 2 == 0), ' ') + '*';

        cout << x << '\n';
    }

    cout << setfill('*') << setw(n + 1) << "\n";
}

It can get even trickier if the data in the box could exceed the size of the box, in which you need to either make the box larger overall or truncate the data.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.