reading in no input

This is a discussion on reading in no input within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I am wondering how i would read in no input. what i mean by this is, a part ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    2

    reading in no input

    Hi everyone,

    I am wondering how i would read in no input. what i mean by this is, a part of my program asks the user to "press enter to continue..". I basically want the program to halt and wait until the enter key is pressed and then the program should continue execution.

    any ideas?


    thank to everyone who read/replyied in advance.


    dan.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >any ideas?
    This is a common question that our FAQ answers.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    2

    yeah, but the problem is...

    thanks for the reply. So basically the answer to my query was the function 'getchar()'. I tried this function in program1 shown below and it worked fine, but when i used it in my real program below, it doesnt work at all. why is this?


    program1
    ------------
    Code:
    main()
    {
    printf("hit enter...");
    getchar();
    }


    real program
    ----------------
    Code:
    switch(choice)
    		{
    		case 1: {
    					system("cls");
    					printf("\n");
    					printf("Conversion: Metres  -->  Kilometres\n");
    					printf("-----------------------------------\n");
    					printf("Please enter Metres: "); scanf("%d",&metres);
    
    					while (metres < 0)
    					{
    						printf("\n");
    						printf("Invalid Length! Please re-enter Length:"); scanf("%d", &metres);
    					}
    
    					kilometres = (metres * METRE_TO_KILOM);
    					printf("\n");
    					printf("%d metres = %d kilometres",metres, kilometres); 
    					printf("\nPress [Enter] to continue..."); 
    					getchar();
    					
    
    				}; break;

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >So basically the answer to my query was the function 'getchar()'.
    That's only half of the answer. The other half covers the problem you're having now, which is extraneous characters in the stream being consumed by getchar. The FAQ page I linked to goes over all of this. You should read it more carefully.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Basically, scanf() ignores leading whitespace, but getchar() doesn't. In addition scanf() stops input into the variable when nonleading whitespace is encountered, but doesn't remove the nonleading whitespace from the input buffer. In your example,

    scanf("%d", &metres);

    leaves the newline char in the input buffer placed there at termination of input into metres when you pressed the enter key. The newline char is still waiting there when getchar() is called. Since there is something in the buffer when getchar() is called it takes the first remaining char out of the input buffer and allows the program to continue without pausing to wait for further input. To fix that you have to clear the input buffer before the call to getchar() in order to pause the program to wait for additional input. Unfortunately, using fflush() to clear the stdin stream buffer as shown in the FAQ isn't the way to clear the input buffer. In C++ the best way to clear the input buffer is something like this:

    std::cin.ignore(std::numeric_limits<std::streamsiz e>::max());

    but for most purposes just ignoring a sufficiently large number of char will work. In your case, you can probably get by by calling getchar() twice in succession since most likely you only have the one newline char remaining in the input buffer. That's not the most robust approach to the problem, but it probably is adequate for us duffers.
    You're only born perfect.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Well, in C you would use
    Code:
    int c;  /* int for EOF */
    
    while((c = getchar()) != '\n' && c != EOF);
    getchar();
    But in C++, as you were saying, you should use
    Code:
    std::cin.ignore(std::numeric_limits<std::streamsize>::max());
    std::cin.get();
    since getchar() is a C function.
    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
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    std::cin.ignore( std::numeric_limits<std::streamsize>::max());

    should be

    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n');

  8. #8
    Registered User
    Join Date
    Nov 2006
    Location
    under your bed....
    Posts
    24
    use this line :

    system("Pause");

    its exaculy wat u want.

  9. #9
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Except for the fact that that's not portable and the solutions presented before are better...
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Instead of scanf() which is dangerous and delicate, you should use fgets()...sscanf(). Which also happens to solve your extraneous characters in stream problem too.

    Oh and this is the C++ forum.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 01:20 PM
  2. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 08:12 PM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Help with linked list (and reading user input)
    By p1kn1c in forum C Programming
    Replies: 2
    Last Post: 04-05-2006, 12:43 AM
  5. Reading input as an integer
    By n0de in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2002, 06:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21