Cant understand this one

R

Rich

Hi all

I am using DEVCPP 4.9.8.1 which uses MingW32 GCC 3.2 compiler

I am trying to compile this program, but without success, I am sure the
compiler is set up ok, as I can compile and run hello.cpp fine

here is the listing please can someone tell what I am doing wrong please

#include <iostream>
#include <cstdlib>

using namespace std;

int integerPower ( int, int ) ;

int main( void )
{
int x, y ;

cout << "First enter the base number, folowed by the component: " << endl
;
cin >> x, y ;

cout << integerPower ( x, y ) << endl ;

int integerPower ( int base, int component )
{
int ans = base ;
for ( i = component; i >=1; --i )
{
ans *= base ;
}

return ans;
}
system("PAUSE");
return 0;
}

and here is the compile log

Compiler: Default compiler
Building Makefile: "M:\C++\How To Program\CH03\Ex3_18\Makefile.win"
Executing make...
make.exe -f "M:\C++\How To Program\CH03\Ex3_18\Makefile.win" all
g++.exe -c ex3_18.cpp -o
x3_18.o -I"K:/Eric/Dev-Cpp/include/c++" -I"K:/Eric/Dev-Cpp/include/c++/ming
w32" -I"K:/Eric/Dev-Cpp/include/c++/backward" -I"K:/Eric/Dev-Cpp/include"

ex3_18.cpp: In function `int main()':
ex3_18.cpp:18: parse error before `{' token

ex3_18.cpp:20: `component' undeclared (first use this function)
ex3_18.cpp:20: (Each undeclared identifier is reported only once for each
function it appears in.)
ex3_18.cpp:22: `ans' undeclared (first use this function)
ex3_18.cpp:22: `base' undeclared (first use this function)

ex3_18.cpp: At global scope:
ex3_18.cpp:27: ISO C++ forbids declaration of `system' with no type

ex3_18.cpp:27: `int system' redeclared as different kind of symbol
K:/Eric/Dev-Cpp/include/stdlib.h:373: previous declaration of `int
system(const
char*)'
ex3_18.cpp:27: invalid conversion from `const char*' to `int'
ex3_18.cpp:28: parse error before `return'

make.exe: *** [ex3_18.o] Error 1

Execution terminated

I notice that it is moaning about component, which is being passed in to the
function as an argument, I didnt think I needed to declare component first,
as I have already done so in the function prototype, like I say this one has
confused me
TIA
 
A

Allan Bruce

Rich said:
Hi all

I am using DEVCPP 4.9.8.1 which uses MingW32 GCC 3.2 compiler

I am trying to compile this program, but without success, I am sure the
compiler is set up ok, as I can compile and run hello.cpp fine

here is the listing please can someone tell what I am doing wrong please

#include <iostream>
#include <cstdlib>

using namespace std;

int integerPower ( int, int ) ;

int main( void )
{
int x, y ;

cout << "First enter the base number, folowed by the component: " << endl
;
cin >> x, y ;

cout << integerPower ( x, y ) << endl ;

You can define a function within a function or indeed main(), so remove from
here
int integerPower ( int base, int component )
{
int ans = base ;
for ( i = component; i >=1; --i )
{
ans *= base ;
}

return ans;
}

to here, and then place it at the end of the file
Allan

system("PAUSE");
return 0;
}

and here is the compile log

Compiler: Default compiler
Building Makefile: "M:\C++\How To Program\CH03\Ex3_18\Makefile.win"
Executing make...
make.exe -f "M:\C++\How To Program\CH03\Ex3_18\Makefile.win" all
g++.exe -c ex3_18.cpp -o
3_18.o -I"K:/Eric/Dev-Cpp/include/c++" -I"K:/Eric/Dev-Cpp/include/c++/ming32" -I"K:/Eric/Dev-Cpp/include/c++/backward" -I"K:/Eric/Dev-Cpp/include"
ex3_18.cpp: In function `int main()':
ex3_18.cpp:18: parse error before `{' token

ex3_18.cpp:20: `component' undeclared (first use this function)
ex3_18.cpp:20: (Each undeclared identifier is reported only once for each
function it appears in.)
ex3_18.cpp:22: `ans' undeclared (first use this function)
ex3_18.cpp:22: `base' undeclared (first use this function)

ex3_18.cpp: At global scope:
ex3_18.cpp:27: ISO C++ forbids declaration of `system' with no type

ex3_18.cpp:27: `int system' redeclared as different kind of symbol
K:/Eric/Dev-Cpp/include/stdlib.h:373: previous declaration of `int
system(const
char*)'
ex3_18.cpp:27: invalid conversion from `const char*' to `int'
ex3_18.cpp:28: parse error before `return'

make.exe: *** [ex3_18.o] Error 1

Execution terminated

I notice that it is moaning about component, which is being passed in to the
function as an argument, I didnt think I needed to declare component first,
as I have already done so in the function prototype, like I say this one has
confused me
TIA
 
A

Artie Gold

cin >> x >> y;
cout << integerPower ( x, y ) << endl ;


You can define a function within a function or indeed main(), so remove from ^^^^^
can't
here


int integerPower ( int base, int component )
{
int ans = base ;
for ( i = component; i >=1; --i )
{
ans *= base ;
}

return ans;
}


to here, and then place it at the end of the file
Allan


system("PAUSE");
return 0;
}

and here is the compile log

Compiler: Default compiler
Building Makefile: "M:\C++\How To Program\CH03\Ex3_18\Makefile.win"
Executing make...
make.exe -f "M:\C++\How To Program\CH03\Ex3_18\Makefile.win" all
g++.exe -c ex3_18.cpp -o

3_18.o -I"K:/Eric/Dev-Cpp/include/c++" -I"K:/Eric/Dev-Cpp/include/c++/ming

32" -I"K:/Eric/Dev-Cpp/include/c++/backward" -I"K:/Eric/Dev-Cpp/include"
ex3_18.cpp: In function `int main()':
ex3_18.cpp:18: parse error before `{' token

ex3_18.cpp:20: `component' undeclared (first use this function)
ex3_18.cpp:20: (Each undeclared identifier is reported only once for each
function it appears in.)
ex3_18.cpp:22: `ans' undeclared (first use this function)
ex3_18.cpp:22: `base' undeclared (first use this function)

ex3_18.cpp: At global scope:
ex3_18.cpp:27: ISO C++ forbids declaration of `system' with no type

ex3_18.cpp:27: `int system' redeclared as different kind of symbol
K:/Eric/Dev-Cpp/include/stdlib.h:373: previous declaration of `int
system(const
char*)'
ex3_18.cpp:27: invalid conversion from `const char*' to `int'
ex3_18.cpp:28: parse error before `return'

make.exe: *** [ex3_18.o] Error 1

Execution terminated

I notice that it is moaning about component, which is being passed in to
the

function as an argument, I didnt think I needed to declare component
first,

as I have already done so in the function prototype, like I say this one
has

confused me


See above.
HTH,
--ag
 
R

Rich

cin >> x >> y;

Thanks :)
from
^^^^^
can't

Ahhh I did wonder if Allan might have meant can't, as it was already
defined in main

I forgot that it is not allowed to define a function within a function, and
that main is itself a function

cheers guys

Rich
 
A

Allan Bruce

Artie Gold said:
cin >> x >> y;
You can define a function within a function or indeed main(), so remove
from
^^^^^
can't
here


int integerPower ( int base, int component )
{
int ans = base ;
for ( i = component; i >=1; --i )
{
ans *= base ;
}

return ans;
}


to here, and then place it at the end of the file
Allan


system("PAUSE");
return 0;
}

and here is the compile log

Compiler: Default compiler
Building Makefile: "M:\C++\How To Program\CH03\Ex3_18\Makefile.win"
Executing make...
make.exe -f "M:\C++\How To Program\CH03\Ex3_18\Makefile.win" all
g++.exe -c ex3_18.cpp -o

_18.o -I"K:/Eric/Dev-Cpp/include/c++" -I"K:/Eric/Dev-Cpp/include/c++/ming

2" -I"K:/Eric/Dev-Cpp/include/c++/backward" -I"K:/Eric/Dev-Cpp/include"
ex3_18.cpp: In function `int main()':
ex3_18.cpp:18: parse error before `{' token

ex3_18.cpp:20: `component' undeclared (first use this function)
ex3_18.cpp:20: (Each undeclared identifier is reported only once for each
function it appears in.)
ex3_18.cpp:22: `ans' undeclared (first use this function)
ex3_18.cpp:22: `base' undeclared (first use this function)

ex3_18.cpp: At global scope:
ex3_18.cpp:27: ISO C++ forbids declaration of `system' with no type

ex3_18.cpp:27: `int system' redeclared as different kind of symbol
K:/Eric/Dev-Cpp/include/stdlib.h:373: previous declaration of `int
system(const
char*)'
ex3_18.cpp:27: invalid conversion from `const char*' to `int'
ex3_18.cpp:28: parse error before `return'

make.exe: *** [ex3_18.o] Error 1

Execution terminated

I notice that it is moaning about component, which is being passed in to
the

function as an argument, I didnt think I needed to declare component
first,

as I have already done so in the function prototype, like I say this one
has

confused me


See above.
HTH,
--ag

Er, yup thats what I meant ;-)
Allan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top