Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: new to c++

  1. #1
    Join Date
    May 2005
    Beans
    67

    new to c++

    Hi all,
    This is just to introduce myself on this forum. I am new to programming and I was hoping to get some help. Before I start of with any questions I just wanted make sure that it is ok for me to post my programming questions here. They are very basic questions (as I am a programming virgin). The language that I am learning is c++.

  2. #2
    Join Date
    Mar 2005
    Beans
    120
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: new to c++

    Go ahead and shoot.
    Fiesty all the way

  3. #3
    Join Date
    May 2005
    Beans
    67

    Re: new to c++

    I had a number of question, but i only remeber one....

    There was an example on what functions were that I was reading up on, I tried to write it up and compile it. But I got an error.
    The cause of the error was that I said

    void main() instead of int main()

    The thing is that in the example they use void main(). My question is why did I get this error. Here is the code, and the error message.

    #include <iostream.h>
    void newLine()
    {
    cout << endl;
    }

    void main()
    {
    cout << "first line" << endl;
    newLine();
    cout << "second line" <<endl;

    }

    The error message


    exFunc.cpp:7: error: ‘::main’ must return ‘int’

  4. #4
    Join Date
    Jul 2005
    Beans
    1,535
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: new to c++

    The quick answer is that void main() is not legal in C++. It is legal (though bad practice) in C, but the C++ standard requires main to return an int.

    Edit: I should also add that you are including a deprecated header. you should be including <iostream> not <iostream.h>

    Lastly, this code won't compile even if you fix the return type of main. The cout and endl are defined in the std namespace, and you need to tell your program that. The easiest way for now is to add
    Code:
    using namespace std;
    to the top of the program. I have attached a fixed version of your sample.

    Code:
    #include <iostream>
    
    using namespace std;
    
    void newLine()
    {
        cout << endl;
    }
    
    int main(void)
    {
        cout << "first line" << endl;
        newLine();
        cout << "second line" <<endl;
        return 0;
    }
    Last edited by hod139; February 23rd, 2006 at 04:00 PM.

  5. #5
    Join Date
    Mar 2005
    Beans
    120
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: new to c++

    First of all thanks for asking this. I knew that void main () is not supported by c++, but to reply i had to google.
    The various answers are
    1) As per C99 standards the syntax is int main (...), so it is illegal to use void main....
    2) Another good explanation of this
    In C and C++, the main() function _has_ to return an int, which will
    be used by the shell which run the program. However, with C++, the
    return statement isn't mandatory anymore, and a main() function with a
    missing return statement will silently return 0.


    Guys any more official commens on int main vs void main ????

    # if u want to program in C++ use #include <iostream>
    instead of #include <iostream.h>
    Pay attention to warnings.

    Regards,
    Abhay
    Fiesty all the way

  6. #6
    Join Date
    May 2005
    Beans
    67

    Re: new to c++

    thanks for the quick reply,
    I did get an message saying that iostream.h is deprecated. I tried using iostream but I got and error message saying

    exFunc.cpp: In function ‘void printTime()’:
    exFunc.cpp:6: error: ‘cout’ was not declared in this scope
    exFunc.cpp:6: error: ‘endl’ was not declared in this scope
    exFunc.cpp: In function ‘int main()’:
    exFunc.cpp:11: error: ‘cout’ was not declared in this scope
    exFunc.cpp:11: error: ‘endl’ was not declared in this scope

    So, I kept using iostream.h.

    Anyway, it seems that using namespace std; has solved that problem.

    My next question is why is it that with iostream.h there is nothing else to include where as with iostream we have to add using namespace std?

    thanks

  7. #7
    Join Date
    Jul 2005
    Beans
    1,535
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: new to c++

    Quote Originally Posted by abhaysahai
    .
    2) Another good explanation of this
    In C and C++, the main() function _has_ to return an int, which will
    be used by the shell which run the program. However, with C++, the
    return statement isn't mandatory anymore, and a main() function with a
    missing return statement will silently return 0.
    This isn't quite true. In C, main does not have to return an int. It can return anything. See http://homepages.tesco.net/J.deBoyne...void-main.html for an explanation.

  8. #8
    Join Date
    Mar 2005
    Beans
    120
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: new to c++

    Quote Originally Posted by hod139
    The quick answer is that void main() is not legal in C++. It is legal (though bad practice) in C, but the C++ standard requires main to return an int.
    Is it legal to use void main () in C ??? I think that in C99 it is illegal, though it is not implemented strictly in gcc, maybe for backward compatibility.

    Please correct me if I am wrong.
    Fiesty all the way

  9. #9
    Join Date
    Jul 2005
    Beans
    1,535
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: new to c++

    Quote Originally Posted by abhaysahai
    Is it legal to use void main () in C ??? I think that in C99 it is illegal, though it is not implemented strictly in gcc, maybe for backward compatibility.

    Please correct me if I am wrong.
    See my above post and link (http://homepages.tesco.net/J.deBoyne...oid-main.html). Using gcc you will get warnings if you don't use int for the return type, but anything is allowed and it will compile. C++ will throw an error.
    Last edited by hod139; February 23rd, 2006 at 04:32 PM.

  10. #10
    Join Date
    Jul 2005
    Beans
    1,535
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: new to c++

    Quote Originally Posted by mwanafunzi
    My next question is why is it that with iostream.h there is nothing else to include where as with iostream we have to add using namespace std?
    1. The iostream.h header is outdated and does not include the namespace protection. For a quick introduction to namespaces see http://www.glenmccl.com/ns_comp.htm and http://www.winterdom.com/dev/cpp/nspaces.html (found by google).

    2. You didn't have to add using namespace std. For this simple example that was the easiest thing to do. Another way you could have written your code is this:
    Code:
    #include <iostream>
    
    void newLine()
    {
        std::cout << std::endl;
    }
    
    int main(void)
    {
        std::cout << "first line" << std::endl;
        newLine();
        std::cout << "second line" << std::endl;
        return 0;
    }
    which is more explicit.

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •