-
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?
-
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
-
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.
-
>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
-
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.
-
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.