Sign in to follow this  
ARC inc

My C++ programing

Recommended Posts

Hey well I've been learning some C++ programing from a website an a book, well I was doing a lession on fuctions but the fuction seems not to work for me could some take look at my code an tell me what I did wrong. I'm using Dev-C++ complier just to let you know not sure if it makes any differnce. ---------------------------------------------------------------------------------- #include <cstdlib> // Include rand() using namespace std; // Make rand() visible int a = rand(); // rand is a standard function that all compliers have return-type function_name ( arg_type arg1, ..., arg_type argN ); int mult ( int x, int y ); ----------------------------------------------------------------------------------

Share this post


Link to post
Share on other sites
Quote:
Original post by ARC inc

#include <cstdlib> // Include rand()

using namespace std; // Make rand() visible

int a = rand(); // rand is a standard function that all compliers have

return-type function_name ( arg_type arg1, ..., arg_type argN );
int mult ( int x, int y );
Is that the actual code? If so, you may be misunderstanding some things; if not, post the actual code, in its entirety (use [source] tags to preserve formatting).

Share this post


Link to post
Share on other sites
What's the website? Because that isn't anywhere close to actual code. It is somewhat descriptive of what code might eventually look like, after you fill in various details. But it isn't working code. Either you misread some stuff from the book/website, or it is very poorly written.

Share this post


Link to post
Share on other sites
Firstly, I don't think that rand() is in the namespace std. I thought it was one of those that weren't in anything (aka: carried over from C), but I could be wrong.

There's a number of things wrong with your code. Most importantly, it's far from complete. You are required to have:

void main()
{
...somecode...
}

main() is where the actual program starts. There needs to be a start, otherwise what would be the firs thing for your program to do? Also, you seem to be posting a 'template' for a function. That is, the book was just showing you an example of what your functions will look like, that's not an actual function (return-type isn't an actual return type, arg_type isn't an actual argument type, function_name shouldn't be your actual name of the function, etc.).

It looks to me like you should go back to the first program. It seems like your moving a little too fast and copy-and-pasting more than you should.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ezbez
void main()
{
...somecode...
}


In C++, main is specified to return int (although you can omit the return statement because an exception is made for main whereby 0 is assumed as the return value). void main is valid only in C.

However, as a demonstration of what Ezbez is talking about, here's a basic outlay of the most minimal C++ program possible - so minimal, that it does nothing at all (well, technically it does a few things that you don't need to worry about at your stage or even really at all).

int main()
{
}


Here's one that does what your code seemed to be a template for - generating a random number and displaying it on the screen:


#include <iostream> // For output to screen
#include <cstdlib> // For srand() and rand()
#include <ctime> // To get the current time for the seed

/* Note: no 'using namespace std'. You don't really need it.
*/


int main()
{
/* The next line 'seeds' the random number generator (don't worry about
* what this means just now - it makes the numbers seem more random). It
* calls two functions, srand() and time(). time() returns the current
* time in a certain format, and srand() takes a seed for the random number
* generator.
*/

std::srand(time(NULL));

/* Here, we set a variable of type 'int' (essentially a standard integer
* variable which can contain a reasonable range of numbers), called 'a'
* to the random number which the function rand() returns.
*/

int a = std::rand();

/* Had we put 'using namespace std' above, then I wouldn't have to put
* 'std::' before 'cout' here. This prints the value of 'a' to the screen.
* 'endl' signals the end of a line - but also does something else, called
* flushing the output buffer that you don't need to worry about quite yet.
*/

std::cout << a << std::endl;
}




Perhaps this makes clear just how much is missing from your apparent code.

EDIT: Changed, since a little Googling suggests that where I thought rand was in the global namespace, it is indeed in the std namespace.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ezbez
Firstly, I don't think that rand() is in the namespace std. I thought it was one of those that weren't in anything (aka: carried over from C), but I could be wrong.

There's a number of things wrong with your code. Most importantly, it's far from complete. You are required to have:

void main()
{
...somecode...
}

main() is where the actual program starts. There needs to be a start, otherwise what would be the firs thing for your program to do?


main() always returns an int.

The C++ versions of the various C headers (like cstdlib instead of stdlib.h) place their contents in namespace std. Though for some reason my gcc allows me to use them without the "std::" qualifier, I can also us them with it. Its good practise to use them with it too.

@OP what is the lesson? I think perhaps that you didn't finish the lesson yet [smile]. Programming tutorials often have code fragments in the middle attempting to explain different ideas, but on their own these fragments won't compile. This is the problem with the line "return-type function_name ( arg_type arg1, ..., arg_type argN );", its not actual code, it is just to demonstrate the general look of a function.

If you read the tutorial to the end, it should have a full program (including main) that you can compile and change.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ezbez
Firstly, I don't think that rand() is in the namespace std. I thought it was one of those that weren't in anything (aka: carried over from C), but I could be wrong.


All the C functions are moved into the std namespace. The only things which stay in the global namespace are macros (like assert).

Share this post


Link to post
Share on other sites
Quote:
Original post by TheUnbeliever
Quote:
Original post by Ezbez
void main()
{
...somecode...
}


In C++, main is specified to return int (although you can omit the return statement because an exception is made for main whereby 0 is assumed as the return value). void main is valid only in C.


Heh, thanks for the clarification. I've always been confused about the return type of main.

Share this post


Link to post
Share on other sites
Quote:
Original post by Durfy
remove this line:
return-type function_name ( arg_type arg1, ..., arg_type argN );


That still won't work.

A global is initialized with a non-constant (which I don't think is valid, AFAIK).
There is no entrypoint.
There is a prototype for a function which is never defined.

Share this post


Link to post
Share on other sites
"A global is initialized with a non-constant (which I don't think is valid, AFAIK)."

It is valide to initialize a global with a non-constant. I do it all the time. You have to be careful though, as you don't know what order the initializers will be called in. They will all be called before main though.

Share this post


Link to post
Share on other sites
I think you may have been using this tutorial. If so I can understand some confusion. In their first box they showed this:

int a = rand();

Which can't be done outside of a function(as everyone has been pointing out). The fourth box is the real code(the one below the sentence that reads "Let's look at an example program:"). I believe they were trying to show that you had to have all three of those lines, they just didn't show that int a = rand(); must be inside a function(such as main). They probably would have been better of just showing the 4th box and then explaining each line individually. HOpe this helps.

Share this post


Link to post
Share on other sites
Quote:
Original post by Glak
"A global is initialized with a non-constant (which I don't think is valid, AFAIK)."

It is valide to initialize a global with a non-constant. I do it all the time. You have to be careful though, as you don't know what order the initializers will be called in. They will all be called before main though.


Thanks! That's fair enough. This isn't something I had encountered (or ever tried to do). It just seemed reasonable (hence the small disclaimer).

Share this post


Link to post
Share on other sites
Yes that is where i got my tut, it is sometimes confusing, but atleast I'm working on it an not giving, up, I'll look some more at that tut, but I'm not sure if it's going to help any btw my book is way out of date so I'm gonna be posting here more an more. ^_^ thanks agian.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this