Thread: using system("pause");

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    10

    Question using system("pause");

    Hello I am trying to use the system("pause"); comand at the end of my program but it is pausing too early. I want it to pause after it outputs the last if statement, but it dont it pauses right before it. Here is the code of it:

    Code:
    #include <iostream.h> //for basic input and output
    #include <string.h>   //for !strcmp
    #include <windows.h>  //for the screen clear and colouring
    
    void color(int x)
    {
    	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),WORD(x));
    }
    
    int main()
    {
    	char *ans1=new char[20];
    	char *ans2=new char[20];
    
    	system("cls");
    	color(3);
    	cout << "What is your name?: ";
    	cin >> ans1;
    	system("cls");
    	color(4);
    	cout << "You said your name was ";
    	cout << ans1 << ".\nIs this correct?\nyes/no\n";
    	cin >> ans2;
    	system("cls");
    	color(5);
    	if (!strcmp(ans2, "yes"))
    		cout << "Pleased to meet you" << ans1 << "\n\n";
    	if (!strcmp(ans2, "no"))
    		cout << "Go away if you can't tell me your real name!\n\n";
    	system("pause");
    	return(0);
    }
    Any segustion on why it isn't working?
    I am using MSVC++6.0

  2. #2
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339
    Hey,

    I coudnt work out what was wrong but i tried restructing it and using a different pausing method and it works now.

    Code:
    #include <iostream.h> //for basic input and output
    #include <string.h>   //for !strcmp
    #include <windows.h>  //for the screen clear and colouring
    
    void color(int x)
    {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),WORD(x));
    }
    
    int main()
    {
    	char *ans1=new char[20];
    	char *ans2=new char[20];
    
    	system("cls");
    	color(3);
    	cout << "What is your name?: ";
    	cin >> ans1;
    	system("cls");
    	color(4);
    	cout << "You said your name was ";
    	cout << ans1 << ".\nIs this correct?\nyes/no\n";
    	cin >> ans2;
    	system("cls");
    	color(5);
    
    	if (strcmp(ans2, "yes") == 0)
    		cout << "Pleased to meet you " << ans1 << "\n\n";
    	
    	else if (strcmp(ans2, "no") == 0)
    		cout << "Go away if you can't tell me your real name!\n\n";
    
    	char endc;
    	cout<<"Press any key to continue "<<endl;
    	cin>>endc;
    
    
    	return(0);
    }
    TNT
    You Can Stop Me, But You Cant Stop Us All

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    10
    Thank you, it actually pauses at the end but in order for that to work you have to type something in then press enter. Isn't there a way to just have it so any button you press wil automatically continue on?

  4. #4
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339
    Hey, FAQ should contain your answers lol.

    Qutoed from faq:


    OPTION 1: getch()

    Syntax:
    #include <conio.h>
    int getch(void);

    Example:
    #include <conio.h> //for "getch()"
    #include <stdio.h> //for "printf"

    int main(void)
    {
    printf("Press a key to continue...");
    getch();
    return 0;
    }

    NOTE: One could also use gets() instead of getch(). It is more portable than getch() but requires the user to press enter. gets() will get a string from stdin and echo to the screen. Terminates on newline character.

    TNT
    You Can Stop Me, But You Cant Stop Us All

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I want it to pause after it outputs the last if statement, but it
    >dont it pauses right before it
    Welcome to the world of buffered input, the output buffer isn't flushed until after the system command is executed, so it appears that system ( "PAUSE" ); is occuring first. The output stream is buffered, which means that it can flush itself (print to whatever medium you want) whenever it is convenient unless you tell it to do so explicitly.

    Add flush to the end of your cout statements, like so:
    cout<<"Pleased to meet you "<< ans1 <<"\n\n"<<flush;

    -Prelude
    My best code is written with the delete key.

  6. #6
    "The Oldest Member Here" Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,039
    flush never works
    just use getch();
    but with linux use svgalib
    vga_delay_buffer_exec();
    depends on what version you have

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    10
    I already tried all the ones in the FAQ and they did the same thing that is why I posted the message.

    As for Prelude your advice for using flush worked! I thank all of you who have helped.

  8. #8
    Unregistered
    Guest
    adding delete[] helps clear memory...

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    if you use endl instead of "\n", it should work properly
    (is this because endl flushes the buffer?). The way i think of it is that "\n" is just a character on the screen and doesn't technically end the line like endl.

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    how about fflush(stdout) ?

  11. #11
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    I think Synersis has it.

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    10
    I never thought of endl like that I thought it was exactly the same as \n. At least that is how the c++ tutorials I've read made it sound. Thank you for the new found knowledge you have provided.

  13. #13
    Registered User
    Join Date
    Apr 2002
    Posts
    6
    #include <cstdlib> || <cstdlib.h>
    system("PAUSE");

  14. #14
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    how did you run that code penpen420
    you need to #include <stdlib.h>
    for it to work!

  15. #15
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    endl does flush the buffer, simply outputting '\n' however does not. Saying cout << endl; is kinda like saying cout << '\n' << flush;.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed