Hello,
I am fairly new to C++ programming but not new to programming, I have programmed in VB and Pascal in the past.
I am using C++ from the ground up by herbert schildt, and going through the tutorials I am stuck on Functions Returning Values.
I am using XCode as my compiler as I am on Mac OSX 10.6 and when building and running the following code, XCode generates 3 error messages. Is there anything in the code that is incorrect?
// Returning a value.
#include <iostream.h>
mul (int x, int y); // mul () 's prototype
main ()
{
int answer;
answer = mul (10, 11); // assign return value
cout << "The answer is" << answer;
return 0;
}
/* This function returns a value. */
mul (int x, int y)
{
return x * y; // return product of x and y
}
Also I am new to these forums and would appreciate help, thanks :)
<iostream.h> should be <iostream>
For cout use std::cout or add usingnamespace std; below the #include
you must precede the function declaration with its return type: int mul (int x, int y); int main()
You function prototype doesn't have a return value. A correct prototype would be int mul (int x, int y); // mul () 's prototype -- notice int
This also applies to main and mul's implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream> // always include iostream because iostream.h is old
usingnamespace std;
int mul (int x, int y); // mul () 's prototype
int main ()
{
int answer;
answer = mul (10, 11); // assign return value
cout << "The answer is" << answer;
return 0;
}
/* This function returns a value. */
int mul (int x, int y)
{
return x * y; // return product of x and y
}
Hey everyone thanks for the fast replies.
I am using the 1994 version of C++ from the Ground up by Herbert Schildt, so it is pretty out of date :s
could you recommend any over decent books for me to learn c++ been browsing reviews on Amazon and came across these thre:
C++: A Beginner's Guide, Second Edition (Beginner's Guides (McGraw-Hill))
by Herbert Schildt
C++ (Teach Yourself Books) (Teach Yourself (NTC))
by Richard Riley (Paperback)
Accelerated C++: Practical Programming by Example (C++ in Depth Series)
by Andrew Koenig (Paperback)
which all had good reviews which one could you recommend is the best of the lot?
I would also recommend Scott Meyers Effective C++ ways which is quite a good read after you have read beginner C++ books. C++ Primer by Stanley Lippman is also a good read.
I started out with C++ for Dummies 5th Edition by Stephen Randy Davis. I must have re-read the thing 2 or 3 times. It's a good read, the author's humorous.
I wouldn't recommend Accelerated C++ until you have some good experience. The title should say enough about that. It's "Accelerated". Although the author may claim you need no C++ experience to read the book, I don't believe that's true. I wouldn't have understood a single thing from Accelerated C++ without a really good background knowledge of the language.