Thread: Array and Function issue

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    3

    Question Array and Function issue

    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
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You don't define functions inside other functions. You need a structure like this:
    Code:
    void my_func(int a)
    {
        cout<<a<<endl;
    }
    
    int main()
    {
        int a;
        func(a);
        
        return 0;
    }
    Also, you can assign the letters of the alphabet to the char array in a loop. A char type is really an int, and all characters are converted to integer codes for storage, then they are converted back to characters for display. You can do directly what the compiler is doing indirectly. If you look in an ASCII table, you will see the integer codes for a-z are 97-122, so you can do this:
    Code:
    for (int i=0; i<26; i++)
       square[i]= i + 97;
    And, when you are ready to display the char's, it doesn't matter how the integer code got in the variable, the compiler just converts the code to a character and displays it.
    Last edited by 7stud; 06-04-2003 at 09:27 PM.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    3
    PHP Code:
    #include <iostream>
    using namespace std;
        
    int x;
        
    int a;
        
    int d;
        
    int square[25];
        for (
    int c=0c<26c++)
         
    square[c]= 97;
       }

        
    void read () {
        for (
    int i=0i<=25i++){
        
    cout<<square[i];
        }
    int main()
    {
        for (
    a=0a<=25a++){
        for (
    x=0x<=ax++){
        
    read();
        }
        
    cout <<"\n";
        }

    i still get errors.. now all i want this to do is make var that changes in a for statement and that goes like this

    a
    next i increasment or next step or w/e
    b
    then
    c
    untill u finish the alphabet then u go
    aa
    ab
    ac
    ad
    ae
    untill thats done then
    ba
    bb
    bc
    bd
    be
    bf
    untill you get something like
    zzzzzzzzz


    thanks,
    jordan

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    int x;
    int a;
    int d;
    get rid of the x and a and declare them inside your "for" loop:
    Code:
    for (int a=0; a<=25; a++){
        for (int x=0; x<=a; x++){
        read();
        }
        cout <<"\n";
        }
    don't know what "d" is for

    also:
    int square[25];
    for (int c=0; c<26; c++)
    square[c]= c + 97;
    }
    move to inside of main

    there might still be problems, im not looking that closely
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    3
    PHP Code:
    #include <iostream>
    using namespace std;

        
    void read () {
        for (
    int i=0i<=25i++){
        
    cout<<square[i];
        }
    int main()
    {
        
    int square[25];
        for (
    int c=0c<26c++)
        
    square[c]= 97;
        }
    for (
    int a=0a<=25a++){
        for (
    int x=0x<=ax++){
        
    read();
        }
        
    cout <<"\n";
        }


    still a billion errors.. AHh this is confusing..

    thanks a lot,
    jrodan

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You've got some problems matching up brackets there. read() is missing a closing bracket, and main() has a closing bracket too soon.

    Also, read() cannot see the variable 'square', so you'd have to pass it as an argument.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Anticipating the next question:

    A function has a return type and parameter types:

    <return type> read(<parameter list>)
    {
    }
    If you don't need to return a value then the type is void, same for the parameter types.
    Code:
    void read(void)
    {
       cout<<"hello";
    }
    
    int main()
    {
        read();
      
        return 0;
    }
    Your function doesn't need to return anything to main()--it just needs to display the array--so your return type will be void.

    Now for parameters. Your function needs the array to print it. To indicate an array is going to be passed to your function, you have to indicate the type in the parameter list for your function:
    Code:
    void read(char square[]) //The array size is not part of the type
    {
        ....
    }
    
    int main()
    {
       char square[26];
       
       ...
    
       read(square); //Passes the array square to read()
      
       return 0;
    }
    Last edited by 7stud; 06-04-2003 at 11:00 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM