To do with functions..

This is a discussion on To do with functions.. within the C++ Programming forums, part of the General Programming Boards category; hey, im new at this, and i cant figure out how to make and use functions.. ive done the tutorial ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    5

    Unhappy To do with functions..

    hey, im new at this, and i cant figure out how to make and use functions.. ive done the tutorial and ive been trying for several hours now, help would be very appreciated
    Code:
    #include <iostream>
    
    using namespace std;
    
    void display(void);
    
    int main()
    {
        
        int x, y;
        int loc[3][3];
    loc[1][1] =1;
    loc[2][1]= 1;
    loc[3][1] = 1;
    loc[1][2] =1;
    loc[2][2]= 1;
    loc[3][2] = 1;
    loc[1][3] =1;
    loc[2][3]= 1;
    loc[3][3] = 1;
    
    
    while (1){
    cout<<"enter x then y value for player X\n";
    cin>>x>>y;
    cin.ignore();
    if (loc[x][y] == 1){loc[x][y] = 8;};
    ;
    cin.get();
    
    cout<<"enter x then y value for player O\n";
    cin>>x>>y;
    cin.ignore();
    if (loc[x][y] == 1){loc[x][y] = 0;};
    display();
    cin.get();
    }
    void display(void)// this is line 39
         {
         cout<<"["<<loc[1][1]<<"]"<<"["<<loc[2][1]<<"]"<<"["<<loc[3][1]<<"]"<<"\n"<<"["<<loc[1][2]<<"]"<<"["<<loc[2][2]<<"]"<<"["<<loc[3][2]<<"]"<<"\n"<<"["<<loc[1][3]<<"]"<<"["<<loc[2][3]<<"]"<<"["<<loc[3][3]<<"]";
         }
        
    
    }
    it says here when i try to compile it,
    "line 39 - expected primary-expression before "void" ". what have i done wrong?

    Thanks in advance =D

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > loc[3][3] = 1;
    Arrays start at 0, not 1
    So your data should begin with
    loc[0][0] =1;
    and end with
    loc[2][2] =1;

    As for your error message, count your braces.
    There's one more open brace in your main.

    Which, if you had a decent approach to indentation would have been obvious.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    If you indent your code properly, you would notice that there is a missing closing brace.

    Also, you must remember that array indices begin at 0, not 1.You should probably use nested loops for your display() function too.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You can put whitespace into your code, you know.
    Code:
    cout<<"["<<loc[1][1]<<"]"<<"["<<loc[2][1]<<"]"...
    ->
    Code:
    cout << "[" << loc[1][1] << "]"
        << "[" << loc[2][1] << "]" ...
    Indentation helps too.

    Also:
    Code:
    cout<<"enter x then y value for player X\n";
    cin>>x>>y;
    cin.ignore();
    if (loc[x][y] == 1){loc[x][y] = 8;};
    ;
    If that if statement is false, perhaps you should ask the user for new coordinates. A loop would be useful here.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    5
    ohh you guys are so friendly! =D. i was expecting to get flamed big time..

    anyway, this is what i did..
    Code:
    #include <iostream>
    
    using namespace std;
    
    void display(void);
    
     int loc[2][2] = 1;
    
    int main()
    {
        
        int x, y;
       
    
    while (1){
    cout<<"enter x then y value for player X\n";
    cin>>x>>y;
    cin.ignore();
    if (loc[x][y] == 1){loc[x][y] = 8;}
    else {cout<<"please enter new coordinates";
    cin>>x>>y;};
    display();
    cin.get();
    
    cout<<"enter x then y value for player O\n";
    cin>>x>>y;
    cin.ignore();
    if (loc[x][y] == 1){loc[x-1][y-1] = 0;}
    else {cout<<"please enter new coordinates";
    cin>>x>>y;};
    display();
    cin.get();
    }
    }
    
    
    void display(void)
         {
                   
         cout<<"["<<loc[1][1]<<"]"<<"["<<loc[2][1]<<"]"<<"["<<loc[3][1]<<"]"<<"\n"
             <<"["<<loc[1][2]<<"]"<<"["<<loc[2][2]<<"]"<<"["<<loc[3][2]<<"]"<<"\n"
             <<"["<<loc[1][3]<<"]"<<"["<<loc[2][3]<<"]"<<"["<<loc[3][3]<<"]"; // this draws the grid
         }
        
    
    }
    but now it says:
    "line 7 - invalid initializer"
    "line 46 - expected deceleration before '}' token"


    ohh, one quick question, can i just do that? int loc[2][2] instead of setting all of them individually?
    Last edited by macchesney; 05-29-2007 at 01:50 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    ohh, one quick question, can i just do that? int loc[2][2] instead of setting all of them individually?
    No, you cannot, and that is the current problem. In your main function, write something like:
    Code:
    int loc[3][3];
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            loc[i][j] = 1;
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    You can have an initialiser
    Code:
    int loc[3][3] = {
      { 1, 1, 1 },
      { 1, 1, 1 },
      { 1, 1, 1 },
    };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    5
    ok, with this part,

    Code:
    while (1){
    cout<<"enter x then y value for player X\n";
    cin>>x>>y;
    cin.ignore();
    if (loc[x-1][y-1] == 1){loc[x-1][y-1] = 8;}
    else {cout<<"please enter new coordinates";
    cin>>x>>y;};
    display();
    cin.get();
    how do i make i loop back to line 2 instead of having an else statement?

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    5
    i think im looking for somthing like
    Code:
    else while(){}
    is there such thing?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    You probably want something along these lines:
    Code:
    while (1)
    {
        cout << "enter x then y value for player X\n";
        cin >> x >> y;
        cin.ignore();
        while (loc[x][y] != 1)
        {
            cout << "please enter new coordinates";
            cin >> x >> y;
            cin.ignore();
        }
        loc[x][y] = 8;
        display();
        cin.get();
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    May 2007
    Posts
    5
    Thanks so much, it works like a charm!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21