Thread: How to ignore "Enter" key

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    How to ignore "Enter" key

    I am trying to write a code which stores the roll no. and total markes of five students in 3 subjects. I want the display to appear tabular, so that the roll no.'s and marks get printed one below the other. But when I use scanf to get the input and when I hit the enter key after entering a no., the cursor comes down to the second line thus messing up my table. Is there any way to ignore the enter key so that the cursor remains on the same line. Plz help.....

    Code:
    #include<stdio.h>
    
    int main()
    {
    	int i,rollno[5],sub1[5],sub2[5],sub3[5],t;
    	printf("Enter the roll no., sub1, sub 2, and sub3 marks");
    	printf("\n\nRoll No.\tSubject1\tSubject2\tSubject3\n");
    
    	for(i=0;i<5;i++)
    	{
    		scanf("%d",&rollno[i]);
    		printf("      \t");
    		scanf("%d",&sub1[i]);
    		printf("      \t");
    		scanf("%d",&sub2[i]);
    		printf("      \t");
    		scanf("%d",&sub3[i]);
    		printf("\n");
    	}
    
    	for(t=0,i=0;i<5;i++)
    	{
    		t=sub1[i]+sub2[i]+sub3[i];
    		printf("\nTotal Marks of Stdent %d = %d",i+1,t);
    	}
             return 0;	
    }
    Last edited by juice; 12-02-2011 at 05:15 AM.

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Pretty sure you'll need to use curses to return the cursor back to its original position.

    curses (programming library) - Wikipedia, the free encyclopedia

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Scanf() will want the newline to know when your data is OK to store. curses, conio.h or SetConsoleCursorPosition (for Windows only), will do what you want.

    What operating system are you using?

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Am using winXP

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A little example program:

    Code:
    /* shows how to use SetConsoleCursorPosition(), in Windows */
    
    #include <stdio.h>
    #include <windows.h>
    
    void Gotoxy(int x, int y); 
    void showIt(void);
    int a[81] = {
    {1,2,3,4,5,6,7,8,9},
    {4,5,6,7,8,9,1,2,3},
    {7,8,9,1,2,3,4,5,6},
    {0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0},
    {2,1,4,3,6,5,8,9,7},
    {3,6,5,8,9,7,2,1,4},
    {8,9,7,4,1,2,3,6,5}
    };
    
    int main(void) {
      showIt();
    
      return 0;
    }
    
    void Gotoxy(int x, int y) {
       COORD coord;
       coord.X = x;
       coord.Y = y;
       SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    void showIt(void)  {
      int i;
      Gotoxy(1,5);
      for(i = 0; i < 81; i+=9)  {
         printf("  | %d%d%d | %d%d%d | %d%d%d |\n", a[i+0],a[i+1],a[i+2],a[i+3],a[i+4],a[i+5],a[i+6],a[i+7],a[i+8]);
         if(i == 18 || i == 45) printf("  +-----+-----+-----+\n");
      }
    }
    Not the best example for you, but anyways..

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    No offences....
    but wtf!!

  7. #7
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Might as well just make a GUI application. All that curses crap isn't worth the time.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by juice View Post
    No offences....
    but wtf!!
    Lemme guess -- you thought this would be simple? Ha ha ha hahaha (no offence )
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by juice View Post
    I am trying to write a code which stores the roll no. and total markes of five students in 3 subjects. I want the display to appear tabular, so that the roll no.'s and marks get printed one below the other. But when I use scanf to get the input and when I hit the enter key after entering a no., the cursor comes down to the second line thus messing up my table. Is there any way to ignore the enter key so that the cursor remains on the same line. Plz help.....
    Try this... What it does is get all the inputs on one line then present a tabular summary at the end.

    Code:
    #include<stdio.h>
    
    int main()
    {
        int i,rollno[5],sub1[5],sub2[5],sub3[5],t;
    
        printf("Enter the roll# sub1 sub 2 sub3 marks, separated by spaces\n");
        for(i = 0; i < 5; i++)
          { 
            printf("Student #%d : ", i+1);
            scanf("%d %d %d ",&rollno[i], &sub1[i], &sub2[i], &sub3[i]);
          }
    
        printf("\n\nRoll No.\tSubject1\tSubject2\tSubject3\tTotal\n");
        for(i=0,i=0;i<5;i++)
          {
             t=sub1[i]+sub2[i]+sub3[i];
            printf("%d\t%d\t%d\t%d\t%d\n",rollno[i], sub1[i], sub2[i], sub3[i], t);
    	}
       return 0;	
    }

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    c'mon, just change the Gotoxy(1,5) to Gotoxy(10,15), and you'll see it's pretty easy.

    The Gotoxy() functions is just copy and paste. Naturally, it's not as simple as conio.h or curses, but that's it - it's Windows!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dont want my program to wait for "enter"
    By MaaaTtY in forum C++ Programming
    Replies: 7
    Last Post: 02-12-2009, 11:40 AM
  2. Replies: 5
    Last Post: 03-22-2008, 12:49 PM
  3. Programmatically sending "enter" keystroke to Outlook
    By Mr. Kamikaze in forum C# Programming
    Replies: 6
    Last Post: 09-17-2004, 10:52 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM