Thread: help

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    9

    Question help

    how do you align text?

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    In which context ?

    C program ? Operating system ? Compiler ?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    9
    sorry,
    in c++

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    DOS ? Windows ? Come on, we need information... There is about a million ways to allign text in different contexts. The more information you give, the more accurate and helpful will we be able to answer.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    9
    sorry again,
    dos c++ borland

    and what problems may exist in multiple inheritances?

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    As far as I know, there is no way to align text without knowing the width of your window/screen. When using output routines like printf/cout, you can normally give parameters for aligning.

    Example:

    The text "Test" should be aligned to a 80 rows screen.

    printf( "%80s", "Test" ); // right aligned, no minus flag given
    printf( "%-80s","Test" ); // left aligned, minus flag present
    printf( "%*s", (80+strlen("Test"))/2, "Test" ); // aligned in the middle by calculating that position

    This is somewhat tricky and you can read about all the different flags in your helpfile or in the MSDN ( www.msdn.microsoft.com ) if Borlands Helpfile isn't helpful.

    There might be similar mechanisms for cout, but as formatting with cout is a pain in the ###, I rarely use it.



    There are various problems that can exist with multiple inheritance. Just imagine both baseclasses have members or functions of the same name.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    9

    i made a not so good game
    battle ship. could you check this out...


    #include <iostream.h>
    #include <conio.h>
    #include <time.h>
    #include <stdlib.h>

    class advertise {
    unsigned int choice;
    public:
    void ads();
    void logo();
    void menu();
    unsigned int get_choice()
    {return choice;}
    };

    class base_class {
    protected:
    unsigned int level;
    public:
    void set_level (int lvl)
    {level=lvl;}
    unsigned int get_level ()
    {return level;}
    };

    class matrix: public base_class {
    protected:
    int num_x, num_y;
    int *(*mtrx_array);
    public:
    void set_matrix();
    void set_tip();
    unsigned int check_num();
    void set_ship();
    void draw_ship();
    };

    class play: public matrix {
    public:
    static int unsigned flag;
    void check_xy(int x, int y);
    };

    void advertise::ads()
    {
    clrscr();
    cout<<"World Pineapple, Inc.\n"<<endl;
    cout<<"Welcome to the Star Battleship game!"<<endl;
    cout<<"\nPress any key to continue..."<<endl;
    logo();
    getch();
    }

    void advertise::logo()
    {
    gotoxy(1,24);
    cout<<"World Pineapple, Inc.";
    }

    void advertise::menu()
    {
    clrscr();
    cout<<"STAR BATTLESHIP\n"<<endl;
    cout<<"Your mission is to find and destroy a battleship called 'Love Boat'";
    cout<<"located some where in your screen, by entering suspected target ";
    cout<<"coordinates. If the ship is hit by using your 'Laser' keyboard three times, you win the game\n"<<endl;
    cout<<"Difficulty:"<<endl;
    cout<<"\t1: Easy"<<endl;
    cout<<"\t2: Medium"<<endl;
    cout<<"\t3: Difficult\n"<<endl;
    do
    {
    cout<<"Level of Play: ";
    cin>>choice;
    switch(choice)
    {
    case 1: cout<<"You selected Easy mode"<<endl; break;
    case 2: cout<<"You selected Medium mode"<<endl; break;
    case 3: cout<<"You selected Difficult mode"<<endl; break;
    default: cout<<"Invalid entry! Please try again!"<<endl;
    }
    }while (choice>=4);
    cout<<"\nPress any key to continue..."<<endl;
    logo();
    getch();
    }

    void matrix::set_matrix()
    {
    unsigned int x_index, y_index, end_num;

    if (level==1)
    {
    mtrx_array[25]=new int[25];
    end_num=25;
    }
    else if (level==2)
    {
    mtrx_array[50]=new int[50];
    end_num=50;
    }
    else if (level==3)
    {
    mtrx_array[100]=new int[100];
    end_num=100;
    }
    for(y_index=0; y_index<end_num; y_index++)
    {
    for(x_index=0; x_index<end_num; x_index++)
    {
    mtrx_array[x_index][y_index]=0;
    }
    }
    }

    void matrix::set_tip()
    {
    unsigned int lev, chk_no=0;
    randomize();
    do
    {
    lev=get_level();
    if (lev==1)
    {
    num_x=random(23);
    num_y=random(23);
    }
    else if (lev==2)
    {
    num_x=random(48);
    num_y=random(48);
    }
    else if (level==3)
    {
    num_x=random(98);
    num_y=random(98);
    }
    chk_no=check_num();
    }while (chk_no==0);
    }

    unsigned int matrix::check_num()
    {
    if (num_x<=3)
    return 0;
    return 1;
    }

    void matrix::set_ship()
    {
    mtrx_array[num_x][num_y]=1;
    mtrx_array[num_x-1][num_y+1]=1;
    mtrx_array[num_x][num_y+1]=1;
    mtrx_array[num_x-1][num_y+1]=1;
    mtrx_array[num_x-2][num_y+2]=1;
    mtrx_array[num_x-1][num_y+2]=1;
    mtrx_array[num_x][num_y+2]=1;
    mtrx_array[num_x+1][num_y+2]=1;
    mtrx_array[num_x+2][num_y+2]=1;
    // cout<<num_x<<" "<<num_y<<endl; cheat
    }

    void matrix::draw_ship()
    {
    cout<<"Shape of the enemy battleship:\n"<<endl;
    gotoxy(5,5);
    cout<<"*"<<endl;
    gotoxy(4,6);
    cout<<"*"<<endl;
    gotoxy(5,6);
    cout<<"*"<<endl;
    gotoxy(6,6);
    cout<<"*"<<endl;
    gotoxy(3,7);
    cout<<"*"<<endl;
    gotoxy(4,7);
    cout<<"*"<<endl;
    gotoxy(5,7);
    cout<<"*"<<endl;
    gotoxy(6,7);
    cout<<"*"<<endl;
    gotoxy(7,7);
    cout<<"*"<<endl;
    }

    void play::check_xy(int x, int y)
    {
    if (mtrx_array[x][y]==1)
    {
    mtrx_array[x][y]=2;
    cout<<"Nice shot!"<<endl;
    cout<<"You hit a portion of the ship!"<<endl;
    flag++;
    }
    else if (mtrx_array[x][y]==0)
    {
    cout<<"Sorry! You missed the target"<<endl;
    cout<<"Try again!"<<endl;
    }
    else if (mtrx_array[x][y]==2)
    cout<<"That part has already been hit!"<<endl;
    }

    unsigned int play::flag=0;

    void main()
    {
    advertise a;
    matrix m;
    play p;
    unsigned int x, y, chk;
    char yn;

    a.ads();
    a.menu();
    clrscr();
    m.set_level(a.get_choice());
    m.set_matrix();
    m.set_tip();
    m.set_ship();
    m.draw_ship();
    cout<<endl;
    do
    {
    cout<<"Enter target x coordinate: ";
    cin>>x;
    cout<<"Enter target y coordinate: ";
    cin>>y;
    p.check_xy(x,y);
    if (p.flag==3) break;
    do
    {
    cout<<"Enemy battleship not yet destroyed."<<endl;
    cout<<"Continue firing? y/n:";
    cin>>yn;
    if (yn=='n')
    {
    cout<<"You chose to cease firing."<<endl;
    cout<<"Until then..."<<endl;
    cout<<"You lose!"<<endl;
    break;
    }
    else if (yn!='y')
    cout<<"Invalid answer!"<<endl;
    }while ((yn!='y') && (yn!='n'));
    }while ((p.flag<3) && (yn=='y'));
    if(p.flag==3)
    {
    cout<<"Congratulations!"<<endl;
    cout<<"You are successful in annihilating the enemy!"<<endl;
    }
    cout<<"Thank you for playing!"<<endl;
    a.logo();
    getch();
    }


    some parts of it loop
    when inputing invalid
    strings instead of a
    single error message

    and howdyu got
    super moderator title?

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    9
    and what does
    the icon 'envelop w/dot'
    means?

  9. #9
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    There might be similar mechanisms for cout
    The equivalent -

    Code:
    	cout << setw(80)<< "Test";
    	cout.unsetf(ios::right);
    	cout << setw(80)<< setiosflags(ios::left)<<  "Test";
    	cout.unsetf(ios::left);
    	cout << setw((80+strlen("Test"))/2)<< "Test;

  10. #10
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    >some parts of it loop when inputing invalid
    >strings instead of a single error message

    Sorry, I don't have a compiler, nor enough time at the moment.
    Find out where they loop. Put in cout statements and see where they repeat.

    When posting code, use the CODE or PHP tags, available by clicking the # or php buttons on the posting screen. This way, the spaces and tabs are preserved and it's way easier to read.

    >and howdyu got super moderator title?

    I'm a moderator here. Moderators of single boards get the 'Moderator' title, moderators of all boards get the 'super-moderator' title. You might have seen other moderators around, most of them changed their title. I was simply to lazy to change it
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed