Jump to content
  • Advertisement
Sign in to follow this  
cj270608

I've wiped the slate clean...

This topic is 3671 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've decided to throw my current speedy addition in the Recycle Bin and start a fresh one. This time, all headers will contain functions, while the source contains the variables and array.

Share this post


Link to post
Share on other sites
Advertisement
I've said this twice I think, but for this project you should only need a single source file. You will not need to generate any headers yourself (though you will use headers provided by the Standard C++ Library).

You are only making unnecessary work for yourself with any other approach. For example, only function declarations should go in headers, not functions definitions. Until you know which is which you should stick to a single file.

Share this post


Link to post
Share on other sites
My source so far is as follows...


#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int main(){

srand ( time(NULL) );

int numbers [10] = { rand() % 10, rand() % 10,rand() % 10,rand() % 10,rand() % 10,rand() % 10,
rand() % 10,rand() % 10,rand() % 10,rand() % 10,};
}




I've tested it by adding
cout << numbers;
and that old enemy "system("PAUSE")"
Talk about random, it always shows the hexadecimal number 0012FF3C

Why is it not random?

And what is best to replace system("PAUSE")?

Share this post


Link to post
Share on other sites
numbers is an array. If you try to print an array with cout, it will just print the memory address of the array (it's not very smart).

To print the numbers *in* the array, use a for loop:

for(int i=0; i<10; i++)
{
cout << numbers << " ";
}


If you haven't seen this before, this code first sets up a variable i to keep track of which number we're currently on. It starts at 0, goes until it's equal to 10, and adds one each time. This causes it to take on the values 0..9 in order and execute the body of the loop with each value.

The line cout << numbers << " "; prints the number in the array with a space after it.

HTH

Share this post


Link to post
Share on other sites
Quote:

I've tested it by adding
cout << numbers;
and that old enemy "system("PAUSE")"
Talk about random, it always shows the hexadecimal number 0012FF3C

Because the statement cout << numbers prints the value of numbers, an int[]. Since the "<<" is really a function call, this decays the int[] to an int*. cout prints the value of this int*, which is the address of the first element in your array, not the value of the first element in the array. To see your array elements, use a debugger and step through the initialization of the array -- you'll see them be filled with random values.

Alternatively, use

for(int i = 0; i < 10; ++i) {
std::cout << numbers << std::endl;
}



Quote:

And what is best to replace system("PAUSE")?

Tell the IDE to invoke your program correctly. For Visual Studio, this means you should select "Start Without Debugging" from the debug menu (Ctrl-F5 usually). This will cause the IDE to keep the window open for you.

Share this post


Link to post
Share on other sites
The most simple way to replace the system("pause"); functionality is just to call cin.get();

It's not perfect, but it works while being standard.

Share this post


Link to post
Share on other sites
it is not random because numbers is the address in memory of your array. Not the values in the array. cout << numbers[0]; will give you the value of the first element in your array.

Please go and find a good tutorial and try and work through it - if you keep posting questions on here asking us to help you with everything, without starting to learn something by yourself you will never become a good programmer. Hell, even if you do go and learn something by yourself you may never be a good programmer...

I already suggested http://www.cplusplus.com/doc/tutorial/ which is a good, solid tutorial. Once you have worked through all of that tutorial, compiling, testing and fiddling with all of the code, then hopefully you will start to have an understanding of how some of c++ works, and then you will be able to start making simple games. It really is the best way to learn, and that tutorial really covers the basics you need quite well. It will probably take you a couple of weeks at least to get through it, but trust me it is the best way to get into programming.

Also, given that you are a beginner, what makes you want to learn c++? It isn't the most intuitive language for most people, although it suits me just fine. A lot of people on here would recommend c# as a better language for first time programmers. I have been coding in c++ for 8 months now, and there are still quirks of the language that catch me out almost every time I hit compile.

Share this post


Link to post
Share on other sites
Consider using a loop to populate the array. This will reduce the amount of repetition in your program, and make it more extensible. For example, should you want to change the random numbers from 0..9 to 1..20 you would only need to change a single line. Or if you wished to change it to 100 elements instead of 10, you only need to change the loops control constant.

Better still, you could declare a named constant for the array size and the loops, so you could do something like this:

int main(){

srand ( time(NULL) );

const int ELEMENT_COUNT = 10;

int numbers[ELEMENT_COUNT];

// loop for random number generation

for(int i = 0; i < ELEMENT_COUNT; ++i) {
std::cout << numbers << std::endl;
}
}

Share this post


Link to post
Share on other sites
I've copied rip-off's code in, but all the command prompt says now is
-858993460 many times, then the welcome text.
Once again, non random numbers?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!