Connecting Tech Pros Worldwide Help | Site Map

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

Newbie
 
Join Date: Oct 2009
Posts: 2
#1: 4 Weeks Ago
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 :
Expand|Select|Wrap|Line Numbers
  1.  
  2. // Write a program to add two matrices.
  3.  
  4. #include <stdio.h>
  5. #include <conio.h>
  6.  
  7. void main()
  8.  
  9. {
  10.         int row,column,**ptrA,**ptrB,**ptrC,counter1,counter2;
  11.         clrscr();
  12.  
  13.         printf("Enter the number of row of the matrices to be processed:");
  14.         scanf("%d",&row);
  15.  
  16.         printf("\nEnter the number of columns of the matrices to be processed:");
  17.         scanf("%d",&column);
  18.  
  19.         ptrA=(int**)(calloc(row,sizeof(int *)));
  20.         ptrB=(int**)(calloc(row,sizeof(int *)));
  21.         ptrC=(int**)(calloc(row,sizeof(int *)));
  22.  
  23.         for (counter1=0;counter1<row;counter1++)
  24.         {
  25.             *(ptrA+counter1)=(int*)(calloc(column,sizeof(int)));
  26.             *(ptrB+counter1)=(int*)(calloc(column,sizeof(int)));
  27.             *(ptrC+counter1)=(int*)(calloc(column,sizeof(int)));
  28.         }
  29.  
  30.         printf("\n\n\nEnter the values for First Matrice:\n\n\n");
  31.  
  32.         for (counter1=0;counter1<row;counter1++)
  33.         {
  34.             for (counter2=0;counter2<column;counter2++)
  35.             {
  36.  
  37.                    printf(" \t");
  38.  
  39.                    scanf("%d",**((ptrA+counter1)+counter2));
  40.  
  41.  
  42.             }
  43.             printf("\n");
  44.         }
Replies would be greatly appreciated!

--
Theprofoundgeek

P.s : Not - so - profound after this problem
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 828
#2: 4 Weeks Ago

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


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.
Newbie
 
Join Date: Oct 2009
Posts: 2
#3: 4 Weeks Ago

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


Quote:

Originally Posted by donbock View Post

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?
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 828
#4: 4 Weeks Ago

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


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.
Reply