My C++ programing

Started by
13 comments, last by ARC inc 17 years ago
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 ); ----------------------------------------------------------------------------------
Advertisement
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).
that is the whole code that the lession told me to put in everytime I try to comply it it says

7 untitled1.cpp parse error before `return'
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.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
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.
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.
[TheUnbeliever]
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.
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).
remove this line:
return-type function_name ( arg_type arg1, ..., arg_type argN );
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.

This topic is closed to new replies.

Advertisement