Thread: please can some1 see whats wrong with this (newbie)

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    11

    please can some1 see whats wrong with this (newbie)

    hi all. im new to c++, trying to teach myself from internet tutorials... its slow progress. anyway i wanted to make a program to solve a puzzle. i havent even got on to writing an algorithm for working it out yet. im stuck trying to make a function that prints a 9x9 2 dimentional array. heres the complete program

    Code:
    #include <iostream>
    
    using namespace std;
    
    void print_table(int array);
    
    int main()
    {
        int table[9][9];
        for(int i=0;i<9;i++)
        {
                for(int ii=0;ii<9;ii++)
                {
                        table[i][ii] = 0;
                        }
                }
        int *ptr;
        ptr = table;
        print_table(ptr);
        cin.get();
        }
    
    void print_table(int array)
    {
         for(int i=0;i<9;i++)
         {
                 for(int ii=0;ii<9;ii++)
                {
                         cout << *array[i][ii] << "|";
                         }
                cout << "- - - - - - - - -" << endl;
                }
         }
    its more than likely that im going about this all the wrong way. my compiler says

    18 C:\Dev-Cpp\Untitled1.cpp cannot convert `int[9][9]' to `int*' in assignment

    so im assuming it has something to do with the fact that i dont know how to pass an array to a function, im just guessing. thanks in advance for help. first post here

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Lose the * in front of array in your cout statement and change void print_table(int array) to void print_table(int *array).
    Away.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Or even better
    Code:
    #include <iostream>
    
    using namespace std;
    
    void print_table(int array[9][9]);
    
    int main()
    {
        int table[9][9];
        for(int i=0;i<9;i++)
        {
                for(int ii=0;ii<9;ii++)
                {
                        table[i][ii] = 0;
                        }
                }
        print_table(table);
        cin.get();
        }
    
    void print_table(int array[9][9])
    {
         for(int i=0;i<9;i++)
         {
                 for(int ii=0;ii<9;ii++)
                {
                         cout << *array[i][ii] << "|";
                         }
                cout << "- - - - - - - - -" << endl;
                }
         }
    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.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    11
    ok thx

  5. #5
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    main() should return an int sir....I don't see a return statement.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    According the gurus and those with access to a copy of the current standard, you no longer need to explicitly return an int from main(), though many of us still do, out of habit if nothing else.
    You're only born perfect.

  7. #7
    *this
    Join Date
    Mar 2005
    Posts
    498
    When did this standard modification come out?
    Last time I tried to use plain main with no return type I got yelled at and pointed to some articles lol...

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The thing people yell about is that the declaration of main should specify a return type of int, not void. If control reaches the end of main and nothing is returned, then 0 will be returned automatically. This is special to the main function.

    That rule has always been in the standard.

    Old compilers, especially VC++ 6.0 don't follow the standard in this case and will give warnings and do the wrong thing if you don't return something with an int return type specified.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Last time I tried to use plain main with no return type I got yelled at and pointed to some articles
    New-style C++ (those with namespaces) allow a returnless main to be implicitly return 0;
    Old C++ and anything C needs an explicit return 0; at the end.

    Personally, I think it's just a sop to all the void main programmers to make it easy for them to update their code by just changing one word.
    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.

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by Salem
    Personally, I think it's just a sop to all the void main programmers to make it easy for them to update their code by just changing one word.
    This story never ends. I think the C standard should enforce correct use of main. Compilers should just give an error when main is used wrongly.

  11. #11
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    They do. Try to compile void main on the latest GCC or G++ and see what happens.
    Code:
    #include <iostream>
    
    void main()
    {
        std::cout<<"Hello"<<std::endl;
        std::cin.get();
    }
    gcc version 3.4.2 (mingw-special)
    4 C:\Dev-Cpp\boardCode\void.cpp `main' must return `int'
    4 C:\Dev-Cpp\boardCode\void.cpp return type for `main' changed to `int'
    Last edited by prog-bman; 07-08-2005 at 01:39 AM.
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM
  3. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM
  4. Windows newbie question
    By Dohojar in forum Windows Programming
    Replies: 7
    Last Post: 04-24-2002, 08:59 PM
  5. what's wrong with my newbie program??
    By insoolated in forum C++ Programming
    Replies: 1
    Last Post: 09-14-2001, 08:49 PM