Spacing?

This is a discussion on Spacing? within the C++ Programming forums, part of the General Programming Boards category; Sorry for being a newbie, but I want to ask two questions, the first is the spacing... I want some ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    43

    Spacing?

    Sorry for being a newbie, but I want to ask two questions, the first is the spacing...

    I want some spacing to show, such as:

    Code:
    Blah                           You see the spacing?
    Spacing                       This is spacing
    Space              Blah
    How do I do this? Or is there a way to center it to the screen?


    And how do I make command prompt/ms-dos prompt run fullscreen?

  2. #2
    .........
    Join Date
    Nov 2002
    Posts
    303
    You could do it like this for the spacing, I'm sure there is another way I just don't know how.

    Code:
    std::cout << "Blah                           You see the spacing?";
    std::cout << "Spacing                       This is spacing";
    std:: cout <<"Space              Blah";
    For centering text go here

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    43
    Well I couldn't get the centering part? It was too confusing... And how do I make a MS-DOS prompt/command prompt window run fullscreen? Another question:


    How do I make a variable that makes itself random from lets say, 1-100? I want something that picks random numbers from 1-100.
    Last edited by trenzterra; 11-28-2002 at 07:12 PM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I want some spacing to show, such as:
    Code:
    #include <iostream>
    #include <iomanip>
    
    int main()
    {
      std::cout<<"Blah"<< std::setw ( 40 ) <<"You see the spacing?"<<std::endl;
      std::cout<<"Spacing"<< std::setw ( 31 ) <<"This is spacing"<<std::endl;
      std::cout<<"Space"<< std::setw ( 15 ) <<"Blah"<<std::endl;
    }
    >And how do I make command prompt/ms-dos prompt run fullscreen?
    Code:
    #include <windows.h>
    
    int main()
    {
      keybd_event ( VK_MENU, 0x38, 0, 0 );
      keybd_event ( VK_RETURN, 0x1c, 0, 0 );
      keybd_event ( VK_RETURN, 0x1c, KEYEVENTF_KEYUP, 0 );
      keybd_event ( VK_MENU, 0x38, KEYEVENTF_KEYUP, 0 );
    }
    >I want something that picks random numbers from 1-100.
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    int main()
    {
      srand ( (unsigned)time ( 0 ) );
    
      for ( int i = 0; i < 10; i++ ) {
        int r = (int)( (double)rand() / ( (double)RAND_MAX + 1 ) * 100 );
    
        std::cout<< r <<std::endl;
      }
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    43
    thanks.


    I wonder whether you can explain this:

    int main()
    {
    srand ( (unsigned)time ( 0 ) );

    for ( int i = 0; i < 10; i++ ) {
    int r = (int)( (double)rand() / ( (double)RAND_MAX + 1 ) * 100 );

    std::cout<< r <<std::endl;
    }



    So that I know what it is all about... I'm getting abit confused here.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    srand ( (unsigned)time ( 0 ) );

    srand means "seed random". rand() alone without seeding it you will likely get the "same" random pattern each time through the program. The "seed" is what's being passed in. time is function that reads from the CPUs clock. Since the clock is never the same value the "seed" is never the same. 0 is another name for NULL as time expects to be passed a pointer to type t, t*, and NULL is a valid pointer, and 0 is another expression of NULL, or something like that, if I remember correctly. unsigned is a cast. I believe time returns a integer value of some sort, so the cast just makes sure it is a positive value.


    for ( int i = 0; i < 10; i++ ) {
    int r = (int)( (double)rand() / ( (double)RAND_MAX + 1 ) * 100 );

    rand() returns an int of value between zero and RAND_MAX inclusive. Therefore division of the return value of rand() by RAND_MAX + 1 gaurantees that the result is between zero and 1. But division of integers in C++ discards any remainder less than 1 so both the return value rand() and the divisor are casted to type double so the actual result will remain a value between zero and 1. This value is then multiplied by one hundred and then that value is casted back to an int, ignoring any decimal portion.

    It seems a bit complicated, but I'm sure Prelude has her reasons. Be glad Prelude didn't use "up to date" casting syntax; it would look even more "geekie".
    ________________________________________
    I've seen something like this more often.

    int r = (rand() % 100) + 1;


    The remainder of any integer divided by 100 will be a number between zero and 99, inclusive. You want a number between 1 and 100, inclusive, however. So add one to the value.
    __________________________________________

    std::cout<< r <<std::endl;

    this is the recommended syntax. It takes into account use of namespaces by way of the scope operator, ::, rather than assuming everything is in namespace std or "polluting" the namespace with everything in namespace std if you had the line

    using namespace std;

    in the program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Better spacing issues
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2008, 03:46 PM
  2. lowest spacing between numbers
    By Ene Dene in forum C++ Programming
    Replies: 8
    Last Post: 12-05-2007, 03:08 PM
  3. Dev C++ Spacing Problem
    By Darklighter137 in forum C++ Programming
    Replies: 7
    Last Post: 11-13-2006, 09:48 AM
  4. Spacing between printf statements
    By HAssan in forum C Programming
    Replies: 8
    Last Post: 08-03-2006, 10:02 AM
  5. Spacing out!!!!
    By Olland in forum C Programming
    Replies: 4
    Last Post: 01-25-2002, 03:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21