#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    5
    Rep Power
    0

    Question Array with functions


    hello i put this code in the compiler (Dev-C++ 4.0):

    PHP Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        
    int i;
        
    int x;
        
    int a;
        
    char square[25];
        
    square[0] = 'a';
        
    square[1] = 'b';
        
    square[2] = 'c';
        
    square[3] = 'd';
        
    square[4] = 'e';
        
    square[5] = 'f';
        
    square[6] = 'g';
        
    square[7] = 'h';
        
    square[8] = 'i';
        
    square[9] = 'j';
        
    square[10] = 'k';
        
    square[11] = 'l';
        
    square[12] = 'm';
        
    square[13] = 'n';
        
    square[14] = 'o';
        
    square[15] = 'p';
        
    square[16] = 'q';
        
    square[17] = 'r';
        
    square[18] = 's';
        
    square[19] = 't';
        
    square[20] = 'u';
        
    square[21] = 'v';
        
    square[22] = 'w';
        
    square[23] = 'x';
        
    square[24] = 'y';
        
    square[25] = 'z';
        
    void read () {
        for (
    i=0i<=25i++){
        
    cout<<square[i];
        }
        }
        for (
    a=0a<=25a++){
        for (
    x=0x<=ax++){
        
    read();
        }
        
    cout<<"\n";
        }

    and i get a **** load of errors like:
    35 test22.cpp
    parse error before `{'
    36 test22.cpp
    parse error before `)'


    then i just get a lot of sntax errors like
    40 test22.cpp
    syntax error before `<='


    if i take out the read fuction and the calling of the read function it all compiles fine.. could someone please help me figure out what im doing rong.. thanks,
    Jordan
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    You can't declare one function within another in C. That's the problem. You'd have to declare read() outside main(). Since read() accesses an array, you can either
    (a) declare the array as a global
    (b) modify the read() function to accept an array pointer as a parameter.

    Hope this helps :)
  4. #3
  5. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    On a different note, square should actually be defined as char square[26], instead of char square[25].
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  6. #4
  7. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    char sSquare[27];//extra space for the '\0'

    sprintf(sSquare,"abcde...........xyz");
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa

IMN logo majestic logo threadwatch logo seochat tools logo