• Love God, and love one another!

    Share your heart for Christ and others in Godly Love

    https://christianforums.net/forums/god_love/

  • Wake up and smell the coffee!

    Join us for a little humor in Joy of the Lord

    https://christianforums.net/forums/humor_and_jokes/

  • Want to discuss private matters, or make a few friends?

    Ask for membership to the Men's or Lady's Locker Rooms

    For access, please contact a member of staff and they can add you in!

  • Need prayer and encouragement?

    Come share your heart's concerns in the Prayer Forum

    https://christianforums.net/forums/prayer/

  • Desire to be a vessel of honor unto the Lord Jesus Christ?

    Join Hidden in Him and For His Glory for discussions on how

    https://christianforums.net/threads/become-a-vessel-of-honor-part-2.112306/

  • Have questions about the Christian faith?

    Come ask us what's on your mind in Questions and Answers

    https://christianforums.net/forums/questions-and-answers/

  • CFN has a new look and a new theme

    "I bore you on eagle's wings, and brought you to Myself" (Exodus 19:4)

    More new themes coming in the future!

  • Read the Gospel of our Lord Jesus Christ?

    Read through this brief blog, and receive eternal salvation as the free gift of God

    /blog/the-gospel

  • Focus on the Family

    Strengthening families through biblical principles.

    Focus on the Family addresses the use of biblical principles in parenting and marriage to strengthen the family.

Random C++ Question

TOG

Member
Joined
Jul 9, 2013
Messages
4,169
Reaction score
793
I'm trying to learn C++. I'm using the book C++ Without Fear by Brian Overland and I have Code::Blocks as my programming environment. Here are some parts of the program that isn't working. It starts out fairly normally...

#include<iostream>
#include<cmath>
#include<ctime>
using namespace std;​

So far so good. The program is supposed to pick random numbers. It starts by setting a seed.

srand(time(null));​

Later in the program, it has a function that picks the number. The function contains just one line, which picks a random number from 0 to 9 and returns it to the function that called it.

return rand() % 10;
When I try running this, both lines give me errors saying that the srand and rand functions haven't been declard and also that null had not been declared. I imagine that the version of C++ I have is slightly different from what the author of the book had. I've tried googling it, but the sites I found said the same as the book. Are there any C++ geniuses here that see what I'm missing?

The TOG​
 
I am not all that familiar with C++, however, if it is anything like VBA you do have to declare what your variables are. In this case srand and rand are variables that are not already declared as variables in C++. There should be a section in your book (in not, you can find an answer online somewhere) that describes how to declare a new variable.
 
I am not all that familiar with C++, however, if it is anything like VBA you do have to declare what your variables are. In this case srand and rand are variables that are not already declared as variables in C++. There should be a section in your book (in not, you can find an answer online somewhere) that describes how to declare a new variable.

srand and rand are functions, not variables, but functions in C++ that return a value can be used in statements as if they were variables.

The TOG​
 
But do you need to declare it specifically as a variable if that is how you are using it?
 
But do you need to declare it specifically as a variable if that is how you are using it?

No. You just declare it as a function. The two types of declarations are really pretty much the same.

Integer variable:
int variablename;

Function that returns an integer:
int functionname();

Then, after defining the function, you can do this:
variablename=functionname();

The TOG​
 
You might try looking into the cmath.h file and look for the srand function. At least I'm thinking it would be in the cmath library.

I thought null was a constant?.. Gad, it's been over 15 years since I've touched C++

BTW, I don't see you #include <stdlib.h> or <stddef.h> libraries.
 
Ok, I just found some old code from 1999 that I wrote:
Here is what I have.
include <time.h>
include <stdlib.h>

/* Generates random numbers, accept Lb and Ub integers */
int randnumber (randone, int randtwo)
{
int random;
random = randone + rand() % (randone - randtwo) +1;
return random
}

Here was another more on you lines:

int rand (void);
{
int randnumber;
srand(time(null));
randnumber = 1 + rand() % (1 - 1000) + 1;
printf ("%d", randnumber);
return randnumber
}

edit to add that the srand function is in the time library :-)
 
I finally found the answer. It turns out that even though the book is only 3 years old, it's using outdated syntax. According to a site I found, null is outdated and you're supposed to use 0 (zero) instead. I eventually found the rand and srand functions by going through the include files on my computer. I had to add #include<random>. Who would have ever thought of that? The header files you mention ,such as stdlib.h are no longer used. That was in C, but C++ skips the ".h" part.

The TOG​
 
Ahhh, ok! You know, when I saw the prinf, I thought for a minute that may have been C!
I did 3 semesters in C and one in C++.

The null being replaced by a 0 is good information!
time.h used to be where rand and srand lived. I had it noted in my code lol! My how things change!

I've recently been thinking about brushing up on my programming skills since there are new opportunities at my place of employment. I've got a diploma in programming with an emphasis on Oracle and C and I've never really used it.

I'm going to assume your programming in Linux with their default C++ libraries and compiler?
 
I have OpenSuSE on my computer at home. I wouldn't recommend it. It has all kinds of problems, one of which is that if a program isn't included, it can be a major pain t set it up. When I took a short course (5 evenings) in elementary programming last spring, they were using CodeBlocks as their IDE. It's available for Windows, Mac and Linux, but it's not in the SuSE repository. If I was going to use it on Linux, It would have taken me forever to figure out which files I had to compile in what order to get it working. I ended up using it on a virtual Win7 machine instead. I'm planning on getting rid of SuSE and setting up something else instead, most likely an Ubuntu derivative, such as Linux Mint or maybe Zorin. I know CodeBlocks is in the Ubuntu repository, so that should be no problem. There are probably other good IDE's for SuSE, but their software system isn't easy to navigate. What I would eventually like to learn to do is to program on one platform (preferably Linux) and make programs that will run on all platforms.

The TOG​
 
Yeah, Suse is more for an enterprise than it is for home use and is a direct competitor to Red Hat. If I recall, isn't IBM heavily into supporting Suse from an enterprise perspective? That being said, YAST is a pretty cool utility!

I may just load an emulator on this machine and set up a few flavors of Linix for programming. I'd like to brush up on my sql and C++.

Can I ask what type of specific development your looking into? Do you have any database interests?
 
I haven't really thought bout the details of what type of programs I would like to do. I'll start working toward my diploma of applied computing this fall. My main goal is to get a new job. The one I have now is ruining my back and my joints, which weren't too good when I started, and besides that, it pays very little. A job in programming, where there would not be any physical exertion, would suit me well.

The TOG​
 
TOG
What flavor of Linux would you recommend to install a database and C++ on? Would I have to install a server version of Linux to install a database or do the desktop versions support databases? I'm not looking to network, so a local database would be just fine.
 
I would suggest one of the Ubuntu derivatives. I believe the most popular one now is Linux Mint. All of the distros that are derived from Ubuntu use the same software repository. I have Mint on a virtual computer, and I just took a look at it. It has all the IDE's, compilers and databases you could want.

The TOG​
 
Excellent!
Next question. I've got VMware Workstation which lets me install virtual workstations from Windows (and take snapshots) and I know Cisco also has a free client that also allows you to run virtual machines and I've never used that. What do you use?
 
I use VirtualBox from Oracle. It's free and is available for Windows, Mac and Linux. There's another virtual machine program called VBox, which comes with some Linux distros. Don't get the two confused. They are not even remotely similar. VirtualBox allows you to take snapshots and change settings such as the number of processors, number of screens, amount of memory, type of networking and other things.

The TOG​
 
Back
Top