473,799 Members | 3,111 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

2 New Member
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 3286
donbock
2,426 Recognized Expert Top Contributor
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
theprofoundgeek
2 New Member
@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 Recognized Expert Top Contributor
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
13843
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 address to an array. I just want to remove duplicate email addresses from the array. Ive tried array_unique() on my test server but it doesnt work. So i tried to remove duplicates myself before storing them into the array. The script works...
8
7653
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 in that sub without making it global? I tried adding the variable to the arguments of the sub, but VB doesn't seem to like anything other than the predescribed format of the event. So, for example, given the MouseDown event: Private Sub...
3
5290
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 executed in the server, which is back end? I think I am confused with the term front end and back end here. Please advise. Thanks!!
5
10792
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 issue to the end of an issue and I'd like to determine how many business hours these issues were open. Issue BeginDt Enddt Total hours 1 3/29/05 5:00 PM 4/1/05 2:00 PM 69 ...
2
4506
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 linking the SQL tables to an Access back-end and then linking the linked tables in the Access back-end to the Access front-end. Doesn't work at all. Obviously, this can't be done. What is the best way to replace an Access back-end with SQL tables?
4
2810
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 have done the following: .. Copied the table in the back end and given the copy a new name .. Created a table with the identical name to the back end copy in the front end .. Used the linkage process to link the table reference in the front end...
15
3747
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 hard to track down database corruption errors. Amir
27
2879
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
3913
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 offending code because each crash immediately shuts down the database and forces me to recompile in order to open it again. I am following proper coding procedures, e.g. creating and destroying objects properly (AFAIK), closing adodb recordsets...
8
1879
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 function does not perform complicated calculations but requires some "storage" to store 4 temporary "vectors" (concept: they could be doubles*, valarrays, vectors,....) for intermediate operations before returning the result. Possible solutions:...
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9546
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10490
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9078
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7570
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.