Thread: Editing output message of system("pause") ?

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    8

    Editing output message of system("pause") ?

    Hey guys,

    Beginner here with a good headstart on C++.... Just wondering, I created a program that contains no loops... it is completely linear moving from start to finish. If the user wants to find the solution to certain info, he has to run the exe again to get it.

    Essentially, I want to edit the message at the end I get (Press any key to continue...) to something different. Obviously, this system pause is a GOOD thing... I want the window to not close automatically so my users can interpret the information they recieve, but I don't want to imply that there is something to continue afterwards. My teacher suggests I inquire online, and bring any results you find to class.

    I've got to agree with him when he says "If this is your pressing issue, then your not too bad off" LOL.

    Not trying to over achieve here, I just take pride in my programs (even if they are simple cmd prompt progs....)

    Thanks!
    A.L.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    There's a bit of an FAQ on this. Cprogramming.com FAQ > How do I get my program to wait for a keypress?

    Basically, system("pause") is an unportable and insecure way to get your console window to stay open. It's much better (but perhaps a little more work . . .) to use standard functions like std::cin.get() to achieve the same effect. If you search the boards you'll find a lot of people talking about system("pause").

    By the way -- if you feel comfortable doing it, posting your code here is one of the best ways to get feedback on it. I've no doubt that if you did so two or four people would point out insightful things about your code . . . .
    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.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    8
    Ok cool!

    Essentially, my program requirements are to make a grading program that follows these guidelines:

    2 grades must be input
    If either of the two grades is below 40, the student automatically fails.
    If the average of the grades is below 60, the student fails.
    If the average of the grades is 60 to 90, inclusive, the student passes (without honors)
    If the average of the grades is 91 or above, the student passes (WITH honors)

    Now, I will take your feedback to my own benefit, however even though the assignment is not due until next week, I will not change any of the coding from this (for the sake of cheating/plagerism policy)..... I agreed to a contract of No cheating by enrolling in my University and I take it pretty seriously....

    But please don't let that detour you from constructive criticism! I will read and interpret the possible changes/suggestions you make, and store that in the front of my head for the improvement in my own education!
    So Dan, if your watching, I won't cheat! I promise. Feel free to compare what I post here to the final product!

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        double grade_one, grade_two, final_grade;
        
        cout << "MyLastName Grade-o-Matic v1.0" << endl << endl << endl;
        cout << "Please input the first score." << endl;
        cout << "Ensure there are no spaces or characters, only positive whole numbers." << endl;
        cin >> grade_one; 
        cout << endl << endl;
        cout << "Thank you.  You have input " << grade_one << " as the first grade." << endl;
        cout << "Please input the student's second score." << endl;
        cout << "Ensure there are no spaces or letters, only positive whole numbers." << endl;
        cin >> grade_two;
        cout << endl << endl;
        cout << "Thank you.  You have input " << grade_two << " as the second grade." << endl;
        
        final_grade = (grade_one + grade_two)/2;
        
        if((grade_one<=40)||(grade_two<=40)) //There is no need to calculate this because anything less than 40 on either grade is an automatic fail.
        {
        cout << "The student fails the course because one of the scores is below 40." << endl << endl;
        }
        else
        {
             if(final_grade < 60)
             {
             cout << "The student's final grade is " << final_grade << "." << endl;
             cout << "The student fails the course." << endl;
             }
             else
             {       
                     if((final_grade <= 90)&&(final_grade>=60))
                                     {
                                     cout << "The student's final grade is " << final_grade << "." << endl;
                                     cout << "The student passes the course WITHOUT honors." << endl << endl;
                                     }
                     else
                                     {
                                     cout << "The student's final grade is " << final_grade << "." << endl;
                                     cout << "The student passes the course WITH honors."  << endl << endl;
                                     }
             }
         }
    
        system("PAUSE");
        ;
    }
    Thanks for your help dwks!

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Technically you could redirect the output of "pause" to nowhere:

    Code:
    #include <cstdio>
    #include <cstdlib>
    
    int main()
    {
        printf("If you press any key the program will end ");
        system("pause > nul");
    }
    IMO, the best option is not to make the program wait for user input before exiting. Instead run the program from an open command prompt and not by double-clicking the exe and get an IDE which keeps console programs open for you.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Aye - pause > nul.

    As mentioned, this isn't portable. Even if you only ever intend to run it on Windows, I think it's good practice to use standard language features... I'd definitely suggest having a look at the link dwks posted.

    Your program looks like it'll do what it's meant to, but I do have a couple of comments:

    Code:
        double grade_one, grade_two, final_grade;
    You can use float here. Float is smaller (4 bytes compared to 8 bytes). It is less precise and has a smaller range but it is plenty for your program.

    You could even use ints for grade_one and grade_two, but you'd always get an int out because this calculation chops off the fractional part:
    Code:
    final_grade = (grade_one + grade_two)/2;
    This is because two ints added together divided by another int give you an int.
    Using a cast:
    Code:
    final_grade = (static_cast<float>(grade_one + grade_two))/2;
    Horrible but works:
    Code:
        final_grade = (grade_one + grade_two)/2.0;
    There's nothing to gain by using ints, but I just thought I'd mention it in case you tried to use them before and couldn't.


    Other comment is:
    Code:
        cout << "MyLastName Grade-o-Matic v1.0" << endl << endl << endl;
    You know you can insert newlines with \n? You don't need to repeatedly use endl:
    Code:
        cout << "MyLastName Grade-o-Matic v1.0\n\n" << endl;
    It's still a good plan to have the last newline as an endl, as endl flushes the output buffer.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Your if-else statements are more complicated than they have to be. For example:
    Code:
             if(final_grade < 60)
             {
                      /* ... */
             }
             else
             {       
                     if((final_grade <= 90)&&(final_grade>=60))
    The code in red isn't necessary. You already know that final_grade is >= 60, otherwise the previous if would have been executed instead, so it's not necessary to check this.

    Also, you probably noticed that the more if-elses that you add, the further your code gets indented. It's an extremely common idiom in programming languages to have an "else if" construct. For example, here's how you could reformat your code (I've also removed redundant tests at the same time):
    Code:
        if((grade_one<=40)||(grade_two<=40))
        {
            // ...
        }
        else if(final_grade < 60)
        {
             // ....
        }
        else if(final_grade <= 90)
        {
            // ...
        }
        else
        {
            // ...
        }
    It makes it clearer to the eye (because humans are very visual beings) that all of the cases are mutually exclusive, and that they are somehow related (if you're doing similar things inside the body of each case).

    You're supposed to have a "return 0;" statement at the end of main(). It's a function that returns int just like any other function. Traditionally 0 indicates success and other nonzero values indicate various errors (because there's only one way to succeed and 2^32-1 ways to fail . . . ).

    What you have is fine and it's really a matter of style, but you may find it interesting that you can break up a cout onto multiple lines. You can even put two string literals next to each other and the preprocessor will automatically concatenate them together. For example:
    Code:
    cout << "Some program by someone.\n"
        "Usage: " << argv[0] << " [arguments]\n"
        "Supported options are:\n"
        "    --fullscreen\t\tRun in fullscreen mode\n"
        "    --windowed\t\tRun in windowed mode (default)\n";
    Good work by the way. For a simple program like it is it's quite well written.
    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.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dwks
    You're supposed to have a "return 0;" statement at the end of main(). It's a function that returns int just like any other function.
    That said, since this is C++, you do not need to have such a statement at the end of the global main function. It is a special exception where the effect of a return 0; would happen even if you left it out. But if you're going to go through the trouble of placing an empty statement (the extra semi-colon you have at the end of main) there, then a little more typing won't hurt
    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

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Hmm, I couldn't remember whether the implicit return 0 thing was part of C++ or C99. Thanks for the correction. Been too long since I was on CBoard . . . .

    Regarding keeping the console window open so the user can read the text: personally I never do this because I run programs from the command line. It's just annoying to have to press a key to close a program. If the person who wants to run the program knows a bit about computers, they'll know what to do if the console window "flashes" before they can read it. Presumably.

    Actually I know of someone who was judging a programming competition, and there was some code that was submitted which did not pause at the end. The judge didn't know what to do, couldn't figure out how to run it. I don't know how you can end up judging a programming contest without encountering this before . . . but he figured it out in the end, so that was all right eventually . . . .
    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.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    8
    Thank you all for your feedback... It was VERY helpful!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. desiging generic message class
    By KIBO in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2010, 04:15 AM
  2. Having trouble making a function do what I want.
    By Shamino in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2007, 11:20 AM
  3. input/output
    By dogbert234 in forum Windows Programming
    Replies: 11
    Last Post: 01-26-2005, 06:57 AM
  4. Character arrays in a Structure
    By vsriharsha in forum C Programming
    Replies: 7
    Last Post: 07-11-2004, 05:36 PM