Thread: To do with functions..

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,057
    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
    28,413
    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;
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  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
    28,413
    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();
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 03: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