Cant understand this one

Discussion in 'C++' started by Rich, Jul 19, 2003.

  1. Rich

    Rich Guest

    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
     
    Rich, Jul 19, 2003
    #1
    1. Advertising

  2. Rich

    Allan Bruce Guest

    "Rich" <> wrote in message
    news:bfbt4d$jtc$...
    > 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++/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
    > TIA
    >
    >
     
    Allan Bruce, Jul 19, 2003
    #2
    1. Advertising

  3. Rich

    Artie Gold Guest

    Allan Bruce wrote:
    > "Rich" <> wrote in message
    > news:bfbt4d$jtc$...
    >
    >>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 ;

    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


    --
    Artie Gold -- Austin, Texas
     
    Artie Gold, Jul 19, 2003
    #3
  4. Rich

    Rich Guest

    cin >> x >> y;

    Thanks :)

    > > You can define a function within a function or indeed main(), so remove

    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
    > See above.
    > HTH,
    > --ag
    >
    >
    > --
    > Artie Gold -- Austin, Texas
    >
     
    Rich, Jul 19, 2003
    #4
  5. Rich

    Allan Bruce Guest

    "Artie Gold" <> wrote in message
    news:...
    > Allan Bruce wrote:
    > > "Rich" <> wrote in message
    > > news:bfbt4d$jtc$...
    > >
    > >>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 ;

    > 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
    > >>

    > >
    > >

    _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
    >
    >
    > --
    > Artie Gold -- Austin, Texas
    >


    Er, yup thats what I meant ;-)
    Allan
     
    Allan Bruce, Jul 19, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack Schafer
    Replies:
    1
    Views:
    540
    Joe Smith
    Apr 23, 2004
  2. Learner
    Replies:
    1
    Views:
    442
    Kevin Spencer
    Jan 5, 2006
  3. Nagaraj
    Replies:
    1
    Views:
    893
    Lionel B
    Mar 1, 2007
  4. Íßêïò Ãêñ33ê
    Replies:
    7
    Views:
    155
  5. cant understand this code

    , Feb 24, 2014, in forum: C Programming
    Replies:
    3
    Views:
    119
    James Kuyper
    Feb 24, 2014
Loading...

Share This Page