Help with code from book!

Hello I'm trying to teach myself c++ using this book, but when I type in the code sometimes I get errors. The book came with a cd with all the code answers on it and they still pop up errors. I'll post the code along with the breakdown of the errors any help sure would be appreciated!!!! Also you can ignore my notes if you like.

#include <iostream>

using namespace std;

//Function must be declared before being used.
int triangle(int num);

int main() {
int n;
cout << "Enter number and press enter: ";
cin >> n;
cout << " Function returned " << traingle(n);
return 0;
}

//traingle-number function.
//return 1+2...... + n
int triangle(int n) {
int i;
int sum = 0
for (i = 1; i <= n; i++) //for i = 1 to n,
sum += i; //add i to sum
return sum;
}

Build Log.

C:\Users\Ricky\Desktop\C++\practice.c|1|error: iostream: No such file or directory|
C:\Users\Ricky\Desktop\C++\practice.c|3|error: expected '=', ',', ';', 'asm' or '__attribute__' before 'namespace'|
C:\Users\Ricky\Desktop\C++\practice.c||In function 'main':|
C:\Users\Ricky\Desktop\C++\practice.c|10|error: 'cout' undeclared (first use in this function)|
C:\Users\Ricky\Desktop\C++\practice.c|10|error: (Each undeclared identifier is reported only once|
C:\Users\Ricky\Desktop\C++\practice.c|10|error: for each function it appears in.)|
C:\Users\Ricky\Desktop\C++\practice.c|11|error: 'cin' undeclared (first use in this function)|
C:\Users\Ricky\Desktop\C++\practice.c||In function 'triangle':|
C:\Users\Ricky\Desktop\C++\practice.c|21|error: expected ',' or ';' before 'for'|
C:\Users\Ricky\Desktop\C++\practice.c|21|error: expected ';' before ')' token|
C:\Users\Ricky\Desktop\C++\practice.c|21|error: expected statement before ')' token|
||=== Build finished: 9 errors, 0 warnings ===|
Just so you know, always post the code you have written in code tags. If you do, your post will look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

//Function must be declared before being used.
int triangle(int num);

int main() {
int n;
cout << "Enter number and press enter: ";
cin >> n;
cout << " Function returned " << traingle(n);
return 0;
}

//traingle-number function.
//return 1+2...... + n
int triangle(int n) {
int i;
int sum = 0
for (i = 1; i <= n; i++) //for i = 1 to n,
sum += i; //add i to sum
return sum;
}


So what I saw was on line 12 you have misspelled triangle and on line 20 you forgot a semicolon after 0. Also, the formal parameter in your prototype and function definition of triangle are different.
Last edited on
Thank you! I will fix these right now.. and yeah i do have the code tags i just didn't really know how to get them to show up on the forums. So i took your suggestions and I fixed them and it did remove 3 of the errors but i'm still getting something like this...

C:\Users\Ricky\Desktop\C++\practice.c|1|error: iostream: No such file or directory|
C:\Users\Ricky\Desktop\C++\practice.c|3|error: expected '=', ',', ';', 'asm' or '__attribute__' before 'namespace'|
C:\Users\Ricky\Desktop\C++\practice.c||In function 'main':|
C:\Users\Ricky\Desktop\C++\practice.c|10|error: 'cout' undeclared (first use in this function)|
C:\Users\Ricky\Desktop\C++\practice.c|10|error: (Each undeclared identifier is reported only once|
C:\Users\Ricky\Desktop\C++\practice.c|10|error: for each function it appears in.)|
C:\Users\Ricky\Desktop\C++\practice.c|11|error: 'cin' undeclared (first use in this function)|
||=== Build finished: 6 errors, 0 warnings ===|

And I really appreciate the help by the way!

Last edited on
It's because your compiler can't find the iostream header.

What program are you using to compile your program?
Hugg you are write I was just googling for a half hour or so trying to figure out why it wouldn't recognize it. I'm using codeblocks and I didn't realize that you need the .cpp extension for the compiler to work properly! Beginner mistake.

For future reference for any people who come to this thread later for codeblocks you need .cpp extension to recognize <iostream>

Thanks for your help guys =]
Maese, his prototype was perfectly fine. It doesn't matter if the variable name is the same in your prototype as your definition, in fact you don't even have to give a variable name in the prototype. As long as you define the return type, function name and variable type(s) then it is fine.

This is a perfectly valid C++ program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#Include <iostream>

void Test( int );

int main()
{
     Test( 5 );

     return 0;
}

void Test( int x )
{
     std::cout << x << std::endl;
}


Menne31 one of the most important (and basic) things of programming is to be able to read and understand the errors you get compiling. Most of these errors are very self explanatory and the rest you can more than likely find with a simple Google search.

I can tell you now though what is causing your errors. First of all your compiler is unable to locate the <iostream> header which is BAD, any compiler that's even half decent should be able to locate this. Your compiler also doesn't seem to recognize the "using namespace" command which is also horrible (and weird.)

Go get yourself a new compiler, I would suggest the free 2010 version of Visual Studio which can be found on Microsoft's website. The only other error that's not caused by having a horrible compiler would be a small typo where you said 'traingle' instead of 'triangle' but that is easily fixed.
Last edited on
Topic archived. No new replies allowed.