Avoiding End-of-line (\n) after using Scanf.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • theprofoundgeek
    New Member
    • Oct 2009
    • 2

    Avoiding End-of-line (\n) after using Scanf.

    Hi,

    I am writing a C code to add two matrices. I want to take input with a feel of real martice, some thing like

    1 2 3
    4 5 6
    7 8 9

    but the Scanf function automatically enter EOL and hence my input looks some thing like this,

    1
    2
    3

    4
    5
    6

    7
    8
    9

    any idea how to get this thing straight?

    Here is the Code :
    Code:
    // Write a program to add two matrices.
    
    #include <stdio.h>
    #include <conio.h>
    
    void main()
    
    {
    		int row,column,**ptrA,**ptrB,**ptrC,counter1,counter2;
    		clrscr();
    
    		printf("Enter the number of row of the matrices to be processed:");
    		scanf("%d",&row);
    
    		printf("\nEnter the number of columns of the matrices to be processed:");
    		scanf("%d",&column);
    
    		ptrA=(int**)(calloc(row,sizeof(int *)));
    		ptrB=(int**)(calloc(row,sizeof(int *)));
    		ptrC=(int**)(calloc(row,sizeof(int *)));
    
    		for (counter1=0;counter1<row;counter1++)
    		{
    			*(ptrA+counter1)=(int*)(calloc(column,sizeof(int)));
    			*(ptrB+counter1)=(int*)(calloc(column,sizeof(int)));
    			*(ptrC+counter1)=(int*)(calloc(column,sizeof(int)));
    		}
    
    		printf("\n\n\nEnter the values for First Matrice:\n\n\n");
    
    		for (counter1=0;counter1<row;counter1++)
    		{
    			for (counter2=0;counter2<column;counter2++)
    			{
    				
    			       printf(" \t");
    
    			       scanf("%d",**((ptrA+counter1)+counter2));
    
    
    			}
    			printf("\n");
    		}
    Replies would be greatly appreciated!

    --
    Theprofoundgeek

    P.s : Not - so - profound after this problem
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    You want the input to look like ...
    • a decimal number
    • some whitespace
    • a decimal number
    • some whitespace
    • a decimal number
    • newline

    You need the scanf format string to reflect this desired input format.

    Comment

    • theprofoundgeek
      New Member
      • Oct 2009
      • 2

      #3
      Originally posted by donbock
      You want the input to look like ...
      • a decimal number
      • some whitespace
      • a decimal number
      • some whitespace
      • a decimal number
      • newline

      You need the scanf format string to reflect this desired input format.
      Donbock, that is a feasible solution, but the problem is that the number of elements to be scanned is dynamic, so how can I decide the length of scanf format string. Any Idea?

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Construct the scanf format string dynamically based on the entered number of columns.

        Copy text from stdin to an array and then parse the array contents.

        Comment

        Working...