Thread: pausing output window

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    2

    pausing output window

    I'm suppose to write a program using if else statements or switch statements taht prompts the user to enter a digit from 0 - 9, and displays the results by spelling out the number. (ex: 4 = four)

    however, when i run the program it did not pause the output window with scanf("pause") so i used another variable (m) and used scanf("%d", m) so the program would wait until the user entered another number. the only problem with this is when entering a number that is not between 0 - 9 , it still does not pause the output window, or it gives an error. what am i doing wrong here? (breathing is not a valid answer )

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int uiNum; /* number entered by user */
    	int m; /* used to pause program */
    
    	printf("Please type in a number between 0 and 9: ");
    	scanf("%d", &uiNum);
    
    	if (uiNum == 0) {
    		printf("\nYou entered zero!\n"); }
    	else if (uiNum == 1){
    		 printf("\nYou entered one!\n");}
    	else if (uiNum == 2) {
    		printf("\nYou entered two!\n");}
    	else if (uiNum == 3){
    			printf("\nYou entered three\n");}
    	else if (uiNum == 4)	{
    			printf("\nYou entered four!\n");}
    	else if (uiNum == 5){
    			printf("\nYou entered five!\n");}
    	else if (uiNum == 6){
    			printf("\nYou entered six!\n");}
    	else if (uiNum == 7){
    			printf("\nYou entered seveb!\n");}
    	else if (uiNum == 8){
    			printf("\nYou entered eight!\n");}
    	else if (uiNum == 9){
    			printf("\nYou entered nine!\n");}
    	else {
    		   	printf("\nInvaild number entered!\n");} /* if user enters anything but 0-9, this message is displayed */
    
    	printf("Press any key + enter to exit.");  /* pauses the program so user can read output */
    
    	scanf("%d", &m);
    
    	return 0;
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Use, getchar() it's the most portable way to pause.

    eg

    Code:
        /* ... */
        getchar();
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    2
    I tried that also, it still doesn't pause the program. Could there be a conflict with me using vista? or maybe it's the pelles c compiler i'm using?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Consider this scenario:

    You ask the user for the original number. They enter this and then press enter:

    Code:
    123abc
    The input buffer contains this:

    Code:
    '1', '2', '3', 'a', 'b', 'c', '\n'
    So you read with scanf() and you get 123 as your number.

    Question:

    What will the next call to scanf() result in for a number? Hint: The buffer now contains this:

    Code:
    'a', 'b', 'c', '\n'
    Basically, scanf() will ignore the non-numeric chars in this case, meaning the buffer is stuck this way. If you use scanf() you should be prepared for silly things like this. If you call getchar(), you end up with 'a'. Then you call again, and you end up with 'b'. One hackish solution to line up the input is to read from stdin until you hit either a '\n' or a newline char. For example:

    Code:
    int c;
    
    ...
    
    while(((c = getchar()) != '\n') && (c != EOF));
    This is answered in the FAQ btw, so you might get a better explanation from there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  2. creating a child window
    By rakan in forum Windows Programming
    Replies: 2
    Last Post: 01-23-2007, 03:22 PM
  3. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  4. Problem with creating new window, from another window
    By Garfield in forum Windows Programming
    Replies: 6
    Last Post: 01-11-2004, 02:10 PM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM