473,326 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 :
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
Oct 22 '09 #1
3 3264
donbock
2,426 Expert 2GB
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.
Oct 22 '09 #2
@donbock
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?
Oct 23 '09 #3
donbock
2,426 Expert 2GB
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.
Oct 23 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Robert | last post by:
Im a beginner in PHP and Im having a problem with this code. Im trying to remove duplicate elements from an array created via $_GET. I want users to be able to click on a link which sends an email...
8
by: Dan | last post by:
Quick question about passing variables to subs to reduce the need for publicly declared variables in VB6. If I have an event sub (MouseDown) that runs a few lines of code, how can I use a variable...
3
by: Matt | last post by:
I always heard people saying IIS ASP front end, and MS-SQL back end. ASP is for server side programming and dynamic content generation, how could it is called front end? Because I thought it is...
5
by: mitchchristensen | last post by:
I have a transaction log that tracks issues from a call center. Each time an issue is assigned to someone else, closed, etc. I get a time stamp. I have these time stamps for the beginning of an...
2
by: Jeff Pritchard | last post by:
Some time ago I am sure I came across something that said this was possible, though it doesn't seem to work. A client wants to replace an Access back-end with SQL Server tables. We have tried...
4
by: John Baker | last post by:
Hi: I have an application that has a back end and a front end. I need to add a table which is identical to one in the back end, and then use it for a temporary holing place form some records. I...
15
by: Amir Michail | last post by:
Hi, Trying to open a file for writing that is already open for writing should result in an exception. It's all too easy to accidentally open a shelve for writing twice and this can lead to...
27
by: galt_57 | last post by:
I need to do just a few multiplies of long integers and have a divide by ten. Can I avoid using floats? Can I use two longs as a 64 bit value somehow? Thanks.
5
by: robert.waters | last post by:
Hello, I have been experiencing crashes and code corruption in my project (vbe6.dll; a decompile fixes the corruption); for the life of me I cannot figure out why, and I can't pin down the...
8
by: StephQ | last post by:
Suppose we are performing a numerical simulation that involves calling a function that performs some stuff. This function is called inside a for loop which iterates for many (100.000) times. The...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.