Cant understand this one

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rich

    Cant understand this one

    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\Ex 3_18\Makefile.w in"
    Executing make...
    make.exe -f "M:\C++\How To Program\CH03\Ex 3_18\Makefile.w in" 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


  • Allan Bruce

    #2
    Re: Cant understand this one


    "Rich" <REMOVEAmigaBlo ke@yahoo.co.uk> wrote in message
    news:bfbt4d$jtc $1@pheidippides .axion.bt.co.uk ...[color=blue]
    > 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: " <<[/color]
    endl[color=blue]
    > ;
    > cin >> x, y ;
    >
    > cout << integerPower ( x, y ) << endl ;
    >[/color]

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

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

    [color=blue]
    > system("PAUSE") ;
    > return 0;
    > }
    >
    > and here is the compile log
    >
    > Compiler: Default compiler
    > Building Makefile: "M:\C++\How To Program\CH03\Ex 3_18\Makefile.w in"
    > Executing make...
    > make.exe -f "M:\C++\How To Program\CH03\Ex 3_18\Makefile.w in" all
    > g++.exe -c ex3_18.cpp -o
    >[/color]
    3_18.o -I"K:/Eric/Dev-Cpp/include/c++" -I"K:/Eric/Dev-Cpp/include/c++/ming[color=blue]
    >[/color]
    32" -I"K:/Eric/Dev-Cpp/include/c++/backward" -I"K:/Eric/Dev-Cpp/include"[color=blue]
    >
    > 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[/color]
    the[color=blue]
    > function as an argument, I didnt think I needed to declare component[/color]
    first,[color=blue]
    > as I have already done so in the function prototype, like I say this one[/color]
    has[color=blue]
    > confused me
    > TIA
    >
    >[/color]


    Comment

    • Artie Gold

      #3
      Re: Cant understand this one

      Allan Bruce wrote:[color=blue]
      > "Rich" <REMOVEAmigaBlo ke@yahoo.co.uk> wrote in message
      > news:bfbt4d$jtc $1@pheidippides .axion.bt.co.uk ...
      >[color=green]
      >>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: " <<[/color]
      >
      > endl
      >[color=green]
      >>;
      >> cin >> x, y ;[/color][/color]
      cin >> x >> y;
      [color=blue][color=green]
      >>
      >> cout << integerPower ( x, y ) << endl ;
      >>[/color]
      >
      >
      > You can define a function within a function or indeed main(), so remove from[/color]
      ^^^^^
      can't
      [color=blue]
      > here
      >
      >[color=green]
      >> int integerPower ( int base, int component )
      >> {
      >> int ans = base ;
      >> for ( i = component; i >=1; --i )
      >> {
      >> ans *= base ;
      >> }
      >>
      >> return ans;
      >> }[/color]
      >
      >
      > to here, and then place it at the end of the file
      > Allan
      >
      >
      >[color=green]
      >> system("PAUSE") ;
      >> return 0;
      >>}
      >>
      >>and here is the compile log
      >>
      >>Compiler: Default compiler
      >>Building Makefile: "M:\C++\How To Program\CH03\Ex 3_18\Makefile.w in"
      >>Executing make...
      >>make.exe -f "M:\C++\How To Program\CH03\Ex 3_18\Makefile.w in" all
      >>g++.exe -c ex3_18.cpp -o
      >>[/color]
      >
      > 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"
      >[color=green]
      >>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(cons t
      >> 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[/color]
      >
      > the
      >[color=green]
      >>function as an argument, I didnt think I needed to declare component[/color]
      >
      > first,
      >[color=green]
      >>as I have already done so in the function prototype, like I say this one[/color]
      >
      > has
      >[color=green]
      >>confused me[/color][/color]


      See above.
      HTH,
      --ag


      --
      Artie Gold -- Austin, Texas

      Comment

      • Rich

        #4
        Re: Cant understand this one

        cin >> x >> y;

        Thanks :)
        [color=blue][color=green]
        > > You can define a function within a function or indeed main(), so remove[/color][/color]
        from[color=blue]
        > ^^^^^
        > can't
        >[/color]

        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[color=blue]
        > See above.
        > HTH,
        > --ag
        >
        >
        > --
        > Artie Gold -- Austin, Texas
        >[/color]


        Comment

        • Allan Bruce

          #5
          Re: Cant understand this one


          "Artie Gold" <artiegold@aust in.rr.com> wrote in message
          news:3F197998.5 000908@austin.r r.com...[color=blue]
          > Allan Bruce wrote:[color=green]
          > > "Rich" <REMOVEAmigaBlo ke@yahoo.co.uk> wrote in message
          > > news:bfbt4d$jtc $1@pheidippides .axion.bt.co.uk ...
          > >[color=darkred]
          > >>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: " <<[/color]
          > >
          > > endl
          > >[color=darkred]
          > >>;
          > >> cin >> x, y ;[/color][/color]
          > cin >> x >> y;
          >[color=green][color=darkred]
          > >>
          > >> cout << integerPower ( x, y ) << endl ;
          > >>[/color]
          > >
          > >
          > > You can define a function within a function or indeed main(), so remove[/color][/color]
          from[color=blue]
          > ^^^^^
          > can't
          >[color=green]
          > > here
          > >
          > >[color=darkred]
          > >> int integerPower ( int base, int component )
          > >> {
          > >> int ans = base ;
          > >> for ( i = component; i >=1; --i )
          > >> {
          > >> ans *= base ;
          > >> }
          > >>
          > >> return ans;
          > >> }[/color]
          > >
          > >
          > > to here, and then place it at the end of the file
          > > Allan
          > >
          > >
          > >[color=darkred]
          > >> system("PAUSE") ;
          > >> return 0;
          > >>}
          > >>
          > >>and here is the compile log
          > >>
          > >>Compiler: Default compiler
          > >>Building Makefile: "M:\C++\How To Program\CH03\Ex 3_18\Makefile.w in"
          > >>Executing make...
          > >>make.exe -f "M:\C++\How To Program\CH03\Ex 3_18\Makefile.w in" all
          > >>g++.exe -c ex3_18.cpp -o
          > >>[/color]
          > >
          > >[/color][/color]
          _18.o -I"K:/Eric/Dev-Cpp/include/c++" -I"K:/Eric/Dev-Cpp/include/c++/ming[color=blue][color=green]
          > >
          > >[/color][/color]
          2" -I"K:/Eric/Dev-Cpp/include/c++/backward" -I"K:/Eric/Dev-Cpp/include"[color=blue][color=green]
          > >[color=darkred]
          > >>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[/color][/color][/color]
          each[color=blue][color=green][color=darkred]
          > >> 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(cons t
          > >> 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[/color]
          > >
          > > the
          > >[color=darkred]
          > >>function as an argument, I didnt think I needed to declare component[/color]
          > >
          > > first,
          > >[color=darkred]
          > >>as I have already done so in the function prototype, like I say this one[/color]
          > >
          > > has
          > >[color=darkred]
          > >>confused me[/color][/color]
          >
          >
          > See above.
          > HTH,
          > --ag
          >
          >
          > --
          > Artie Gold -- Austin, Texas
          >[/color]

          Er, yup thats what I meant ;-)
          Allan


          Comment

          Working...