# Rock's cpp truck.

Rockbomb
Dog fucker (but in a good way now)

2009 Nov 13 • 2045
Mate de Vita said:
Rockbomb said:
Ok, I'm working on this program that uses the rand function and the system clock to generate a random number, and one part of it is this line...
c++ code
int numberPicked = rand() % 100 + 1;

I don't really get what its doing. I know % is the modulus operator, but I just don't really understand this line.
The way I see it, it takes the value generated by rand (which hasn't been declared yet in the program, so that doesn't make much sense to me), then takes the remainder of that divided by 100, and adds one to that number. But that would make no sense at all, so I guess I'm reading it wrong...

Your statement (or whatever this is called) will choose a random number from 1 to 100.

The rand() will give a completely random number.
This number when divided by 100 will give you a modulus, which will be something between 0 and 99 (depending on what the last two digits of the random number were).
This modulo is used simply to find the last two digits of the number picked by rand().

Since you want a value from 1 to 100, not from 0 to 99, you add 1 at the end.

Note that this is only me guessing here, you should wait for someone who knows C++ to confirm this.
In pascal iirc the rand() would give you a random value between inclusive 0 and exclusive 1 so something like this would've been different.

Well see, thats what I don't get... rand gives a completely random number. So lets say it gives... 35182. So now it takes the remainder of that divided by 100, and you get... oh, 82

Ok, nevermind, now I get it. I was thinking that you'd be able to get numbers higher than 100, but now that I'm re-looking at it, I'm wrong
Thanks for the explenation
 ≡ 2010 May 1 at 06:50 PDT
Down Rodeo
Cap'n Moth of the Firehouse

2007 Oct 19 • 5486
57,583 ₧
Mate de Vita is right. Remember - modulo 100 will always return a number less than 100 but greater than or equal to 0. So you can get any number in any range by doing modulo x + y, you'll end up with [y,x+y).
 ≡ 2010 May 1 at 08:33 PDT
sprinkles

2009 Sep 6 • 2547
10 ₧
Did you know you could click the buttons using the scroll wheel?
 ≡ 2010 May 2 at 11:32 PDT
Rockbomb
Dog fucker (but in a good way now)

2009 Nov 13 • 2045
sprinkles said:
Did you know you could click the buttons using the scroll wheel?

Wrong truck maybe?
And yes, you can click buttons with the scroll wheel, and it will open pages in a new tab.
 ≡ 2010 May 2 at 11:34 PDT
sprinkles

2009 Sep 6 • 2547
10 ₧
Rockbomb said:
sprinkles said:
Did you know you could click the buttons using the scroll wheel?

Wrong truck maybe?
And yes, you can click buttons with the scroll wheel, and it will open pages in a new tab.

No.

No, the actual buttons, you click them with the scroll wheel (it doesn't do anything though).
 ≡ 2010 May 2 at 11:37 PDT
Down Rodeo
Cap'n Moth of the Firehouse

2007 Oct 19 • 5486
57,583 ₧
 ≡ 2010 May 2 at 11:48 PDT
sprinkles

2009 Sep 6 • 2547
10 ₧

<input type="Submit" value="I AM DONE TYPING NOW!" tabindex="2">

Or for you visual learners:

 ≡ 2010 May 2 at 11:52 PDT — Ed. 2010 May 2 at 11:52 PDT
Down Rodeo
Cap'n Moth of the Firehouse

2007 Oct 19 • 5486
57,583 ₧
Well, of course you can middle click on them. They are objects that accept inputs. And stuff.
 ≡ 2010 May 2 at 13:17 PDT
SRAW
Rocket Man

2007 Nov 6 • 2525
601 ₧
**SCRIPT TAGS NOT ALLOWED**

WHATT
 ≡ 2010 May 9 at 23:31 PDT — Ed. 2010 May 15 at 19:16 PDT
SuperJer
Websiteman

2005 Mar 20 • 6445
Depending on the make, model, and mood of your browser, middle click should mean "open this in a new tab."
 ≡ 2010 May 13 at 00:54 PDT
Rockbomb
Dog fucker (but in a good way now)

2009 Nov 13 • 2045
If I use
C++ code
#ifdef unix
#define CLEAR_CMD "clear"
#else
#define CLEAR_CMD "cls"
#endif

#define PAUSE() {cout<<"Press enter to continue\n"; cin.ignore(1000,'\n');}

Do I put that in the header section? I'm guessing so by the pound symbols, but I'm not sure.
 ≡ 2010 May 15 at 15:26 PDT
Down Rodeo
Cap'n Moth of the Firehouse

2007 Oct 19 • 5486
57,583 ₧
Put it before you ever intend to use them. It's like... the preprocessor basically substitutes whatever happens after

Screw it, example time:

#define 1 2

Means that wherever the preprocessor sees '1' in the source it replaces it with '2'. So, #defines should go at the top of the source. The #ifdef and #ifndef are like small if-else constructs but again are only followed by the preprocessor. It's like asking "what platform am I on?" One final thing to note is that #include is just the same - it is only used by the preprocessor. All it does is dump the entire contents of the header file you specify at the top of your file (at least it does in C, but what does #include <cstdlib> mean in that sense? Is it still a header? Jer, help me!)
 ≡ 2010 May 15 at 17:05 PDT
Rockbomb
Dog fucker (but in a good way now)

2009 Nov 13 • 2045
Down Rodeo said:
Put it before you ever intend to use them. It's like... the preprocessor basically substitutes whatever happens after

Screw it, example time:

#define 1 2

Means that wherever the preprocessor sees '1' in the source it replaces it with '2'. So, #defines should go at the top of the source. The #ifdef and #ifndef are like small if-else constructs but again are only followed by the preprocessor. It's like asking "what platform am I on?" One final thing to note is that #include is just the same - it is only used by the preprocessor. All it does is dump the entire contents of the header file you specify at the top of your file (at least it does in C, but what does #include <cstdlib> mean in that sense? Is it still a header? Jer, help me!)

Ok, so it'll work if I put it up where I put my "#include"s.
Also, another question... in the part I quoted from superjer, its cin.ignore(1000) , whats the 1000 do? I've just been doing cin.ignore() and it works fine.
 ≡ 2010 May 15 at 18:48 PDT
Down Rodeo
Cap'n Moth of the Firehouse

2007 Oct 19 • 5486
57,583 ₧
http://www.cplusplus.com/reference/iostream/istream/ignore/

It appears that it ignores 1000 characters. So that it doesn't mess with your actual input. I dunno.
 ≡ 2010 May 15 at 19:02 PDT
Rockbomb
Dog fucker (but in a good way now)

2009 Nov 13 • 2045
Hmmm, if I do cin.ignore(1000);, I have to hit enter 1000 times before I can continue with my program
going back to cin.ignore();
 ≡ 2010 May 15 at 21:33 PDT
Rockbomb
Dog fucker (but in a good way now)

2009 Nov 13 • 2045
Ok, got a question. When I was working in VB, I wouldn't do much coding in the main() fucntion, but rather create seperate functions and call them in the main()
Can I do that in C++ too?
If so how would I do it? Would I just do:
code
int nameoffunction(void)
{
blah blah blah
}

???
 ≡ 2010 May 16 at 09:14 PDT
Down Rodeo
Cap'n Moth of the Firehouse

2007 Oct 19 • 5486
57,583 ₧
Yes. As I'm sure you know the first bit is the return type, the second bit the name and the bit in the brackets is where you place the arguments. Or parameters if you will.
 ≡ 2010 May 16 at 10:03 PDT
Rockbomb
Dog fucker (but in a good way now)

2009 Nov 13 • 2045
Ok, so I messed around with this a little and I guess I'm not doing it right...
So basically I have my main() function, then after it I typed in
C++ code
int test(void)
{
using std::cout;
cout << "TESTING!!!";
return(0);
}

Then inside my main funtion I typed in
code
test();

But I'm getting an error that says
Quote:

So I'm probably setting this up wrong, but Idk.
 ≡ 2010 May 16 at 12:06 PDT
Down Rodeo
Cap'n Moth of the Firehouse

2007 Oct 19 • 5486
57,583 ₧
You don't need the brackets around the 0, you can do it like
return 0;

Also you have to declare the function before you use it, just like variables. So at the top of your file you can have one line like "int test()" then later on you can define it. This is what header files are mainly used for.
 ≡ 2010 May 16 at 13:34 PDT
Rockbomb
Dog fucker (but in a good way now)

2009 Nov 13 • 2045
Down Rodeo said:
You don't need the brackets around the 0, you can do it like
return 0;

Also you have to declare the function before you use it, just like variables. So at the top of your file you can have one line like "int test()" then later on you can define it. This is what header files are mainly used for.

Ah, ok.
So since I'm putting it up in the top before the main() function, do I need to use the pound symbol? (seems like everything up there has a pound symbol before it, but idk)
would it just be
int test()

Or would it be
#int test()
???
 ≡ 2010 May 16 at 13:48 PDT
Down Rodeo
Cap'n Moth of the Firehouse

2007 Oct 19 • 5486
57,583 ₧
Remember, lines with a hash in front of them are preprocessor directives. See my post about what #include does, for instance. It literally deposits all the text from the file "whatever.h" at the top of yours (which is how you are able to use the various builtin functions). So no, you don't need the hash sign. There are other tutorials that break functions in a bit more gently... perhaps it is still best to think of functions as being similar to variables - declare before use!
 ≡ 2010 May 16 at 14:12 PDT
Rockbomb
Dog fucker (but in a good way now)

2009 Nov 13 • 2045
Ah, ok. Its all startin' to slowly make sense

I'm glad I finally decided to stick with the language, even through hard times. This is the third or fourth time I've attempted to learn C++
 ≡ 2010 May 16 at 15:09 PDT
the_cloud_system
polly pushy pants

2008 Aug 1 • 3079
-6 ₧
Rockbomb said:
Rock's pcp truck.

I drink to forget but I always remember.
 ≡ 2010 May 16 at 16:23 PDT
Rockbomb
Dog fucker (but in a good way now)

2009 Nov 13 • 2045
the_cloud_system said:
Rockbomb said:
Rock's pcp truck.

I wish I had a truck full of pcp. That'd be like a tirllion dollars!
 ≡ 2010 May 16 at 16:41 PDT
SuperJer
Websiteman

2005 Mar 20 • 6445
Down Rodeo said:
All it does is dump the entire contents of the header file you specify at the top of your file (at least it does in C, but what does #include <cstdlib> mean in that sense? Is it still a header? Jer, help me!)

There is a header called stdlib.h that gets included by that. It contains declarations for all the standard library functions and stuff.
 ≡ 2010 May 16 at 23:50 PDT
Page 1 2 [3] 4