functions and header files


Im doing a homework assignment for CS programming 2 and am running into an error. I get," expected primary expression before "int" on line 7. I have been dealing with figuring out using an implementation file and header and now this comes up. Can someone tell me what I am doing wrong and look to see if I made any other errors. The program should display :
*********
*******
*****
***
*

I have to define a main function, create an implementation file, and a header file. The main function is not really meant to do any work. Some of the jargon we havent covered in class but are expected to I guess learn on our own.

MAIN.CPP
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include "pyramid.h"
using namespace std;

int main()
{
    cout << pyramid(int i, int j);
system("pause");
return 0;
}


IMPLEMENTATION FILE

1
2
3
4
5
6
7
8
9
10
11
12
int pyramid(int,int);
{
    for(int i=5;i>=1;i--)
    {
        for(int j=1;j<=5-i;j++) 
            cout << ' ';
        
        for(int j=1;j<=2*i-1;j++) 
            cout << '*';
            cout << endl;
    }
    return i, j;

HEADER FILE

1
2
3
4
#ifndef PYRAMID_H  
#define PYRAMID_H  
int pyramid(int i,int j);
#ENDIF 

Last edited on
What exactly are you drying to do. You can't put variable declarations (e.g. with with int keyword) inline in another statement.
1) Code tags: http://cplusplus.com/forum/articles/42672/#msg230640

2) On this line: cout << pyramid(int i, int j);
You seem to be defining the some variables inside the function, you can't do that. See:
http://cplusplus.com/doc/tutorial/functions/
Im trying to get the pyramid function to return to the main and print out the inverted pyramid. I see that on line I should not have done that. Perhaps that was done by mistake out of so many attempts to make it work. I still get the error with the variable declarations.

The main function is to be define in order to test my other function.

Firedraco... sorry about the source code. I edited it.
int pyramid(int,int);
This is from your implementation. There are two probably I see immediately.
1) There is a semicolon at the end of the line, separating it from the code block defining it.
2) The parameters are both unnamed, and hence unusable. (There are circumstances where unnamed parameters are justified, but I don't think this is one of them :p)
Last edited on
Im still not getting it to work. Im getting it to complie and run.... but it does not display anything. Below is the program simplified and this one works by displaying the inverted asterisk pyramid. I just have to organize it better by making an implementation file and header file.


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;

void pyramid(); // function prototype

int main()
{
    pyramid();  //function call

system("pause");
return 0;
}
void pyramid()   //function definition
{
     for(int i=5;i>=1;i--)
    {
        for(int j=1;j<=5-i;j++) 
            cout << ' ';
        
        for(int j=1;j<=2*i-1;j++) 
            cout << '*';
            cout << endl;      
    }
}
Last edited on
It works perfectly for me :) Are you sure that code isn't working? Does a console window even show up? What appears in it (if anything at all)?
Last edited on
My last post should work just fine. It works for me. But my original problem still remains. When I seperate the code below to make a seperate header file and implementation file aside from the main.cpp.... it doesnt work. I get multiple errors like:

1. circular main <- main.o dependency dropped
2. In function void pyramid().... cout and endl undeclared
3. [Build error] [pyramid.o] Error1

Currently no console window comes up. I have messed with it several times and have gotten different results. In one instance it compiled and the console window came up but didnt display anything but press any key to continue. In another, the program wouldnt compile and gave me the the error: expected primary expression before "int".




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;

void pyramid(); // function prototype

int main()
{
    pyramid();  //function call

system("pause");
return 0;
}
void pyramid()   //function definition
{
     for(int i=5;i>=1;i--)
    {
        for(int j=1;j<=5-i;j++) 
            cout << ' ';
        
        for(int j=1;j<=2*i-1;j++) 
            cout << '*';
            cout << endl;      
    }
}

¿why don't you post the code that gives you trouble?
I did... its at the very top.
I think you might have updated it a little, can you repost/update your code.
Also, you have typed #endif in capitals, #ENDIF.
Topic archived. No new replies allowed.