Thread: 2 Noobish Questions from a 2nd Day Learner

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    5

    2 Noobish Questions from a 2nd Day Learner

    Hey Guys,


    I literally just started C++ yesterday and I'm already loving it. I am using the Bloodshed Dev-C++ Compiler to work on my coding/compiling. I am taking C++ as an independent study course in High School, so all I have to learn from is some old book that doesn't elaborate for somebody who is unfamilar with C or C++.

    Anyways, I have two questions to ask of you guys. They are probably going to seem VERY noobish, but please keep in mind that I have only started yesterday.

    Question #1: My programs terminate after they have finished their execution. I have done a couple of lessons out of my book which involves create very simple calculating programs. After entering the numbers to be solved with a mathematical function, the program self-terminates. I can see for a flash of a second that it displays the answer, but it doesn't stay up long enough to get more than a subliminal seeming glance at it. I found a way to conquer the problem by making a "filler" input prompt, but i'm almost certain that this in unethical and not the permanent answer to my problem.

    Question #2:

    Code:
    cout << "Insert message here/n";
    When I have an output command and use the "/n" to create a break in the line, the program still displays the /n along with the message I want it to output. It acts as if it is ignoring the command. Again, I found a fix to the problem by adding:

    Code:
    endl;
    ...after a line, but again, I am sure this is unethical in the lines of proper programming.



    Thanks for any assistance and for being patient with my noob-ness.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Bucket
    Question #1: My programs terminate after they have finished their execution. I have done a couple of lessons out of my book which involves create very simple calculating programs. After entering the numbers to be solved with a mathematical function, the program self-terminates. I can see for a flash of a second that it displays the answer, but it doesn't stay up long enough to get more than a subliminal seeming glance at it. I found a way to conquer the problem by making a "filler" input prompt, but i'm almost certain that this in unethical and not the permanent answer to my problem.
    No, that's exactly what you want to do. In reality, those console based applications are supposed to be invoked from the command line. When they end they're supposed to send you back to the command line prompt, however, when you open them in their own window, if they don't pause themselves before completion, they'll just close.
    Quote Originally Posted by Bucket
    Question #2:

    Code:
    cout << "Insert message here/n";
    What you want is '\n' not '/n'. All escape sequences use the backslash not front slash. For instance '\0' or '\r'. Also note that endl is not the same as a the linefeed character '\n' because it also flushes the buffer.
    Last edited by SlyMaelstrom; 08-25-2006 at 08:27 AM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    5
    I see, thank you very much. I'm too used to HTML and it's use of the frontslash...

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    in dev C++ you have to (well, have to if you want the program to be readable through opening the exe through windows explorer), instead of return zero, insert

    Code:
    system("PAUSE");
    return EXIT_SUCCESS;

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    5
    Thanks for the additional information. It helps out alot.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    I don't want to sound rude but I would ignore that last tip if I was you. system() is a function which uses unportable parameters which means that system("cmd") would fail on unix and system("ls") would fail on windows. As for the return line, unless you are returning something else than success (i.e. you are returning 1 if something bad happens to close the application) then you really don't need to return anything because it will be done automatically, implicitly.

    Edit: Generally speaking, consider the system() function evil, unless you know your app will never be ported to any other OS.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I agree with the first part of your statement Desolation. However, as for the second part, there is no guarantee the system or the compiler will place return 0 on success. I don't know of any system that expects other type of argument, or any compiler that doesn't know what to do with the absence of a return statement in main(). But the point being that leaving the return statement out (even on success) qualifies as undefined behavior.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    When will he really start worrying about how his calculator sim gets ported to unix?

    I'm just giving him a somewhat temporary solution to a minor annoyance as a learner.

    Or use cin.get();, or use code::blocks which allows you to use return 0 while still allowing the program to run in a dos prompt without closing immediately.

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    5
    My main concern at the moment is to learn how to code, and finding new ways to do different things. All the computers I plan to be working on / distributing whatever I make will be Windows OS. This may change in the future, but as of now, it is not a concern.

    Edit: Thanks for the information, once again.

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    It's not just a matter or portability indigo. It is also a matter of security. All it takes is an executable named pause.exe/bat/com with malicious code on one path taking precedence over the system pause command.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    903
    After searching on Bjarne Stroustrup's website (which I consider a good source), I will prove you wrong.

    http://www.research.att.com/~bs/bs_faq2.html#void-main

    In C++, main() need not contain an explicit return statement. In that case, the value returned is 0, meaning successful execution.

  12. #12
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    prove whom wrong?

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    903
    Mario F.

    Quote Originally Posted by Mario F.
    However, as for the second part, there is no guarantee the system or the compiler will place return 0 on success. [...] But the point being that leaving the return statement out (even on success) qualifies as undefined behavior.

  14. #14
    Registered User
    Join Date
    Aug 2006
    Posts
    5
    I have to leave class now, so I will check this thread tomorrow morning to see if a resolution has been agreed on.

    Although i'm wearing it out, thanks again. Having no instructor is really taking a toll on me.

    *sigh*

  15. #15
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    And I have had a lot of problems with Dev C++, I would recommend using several IDEs to find the right one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 35
    Last Post: 01-31-2008, 06:01 PM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  4. debug program
    By new_c in forum C Programming
    Replies: 3
    Last Post: 03-18-2002, 11:50 PM
  5. Simplified code
    By soonerfan in forum C Programming
    Replies: 2
    Last Post: 12-05-2001, 03:50 PM