How to Use Standard Input in C++ for a 2D coordinates

  • Permalink
  • submit to reddit
  • Email
  • Follow


Hi,

I am new to c++ programming.Could anyone tell me how I can use
standard input in c++ to read 2D coordinates? e.g., read n elements
which have x-coordinates and y-coordinates.

Could anyone show me a simple example? Thanks very much.


---Tonny

0
Reply tonnyspain (9) 11/11/2007 4:07:32 AM

See related articles to this posting


Tonny wrote:

> I am new to c++ programming.Could anyone tell me how I can use
> standard input in c++ to read 2D coordinates? e.g., read n elements
> which have x-coordinates and y-coordinates.
>
> Could anyone show me a simple example? Thanks very much.

Why not use a soft language (if you are going to have to learn a new 
language anyway), and read SVG format thru an XML reader?

If you simply must use C++, for a real technical reason, whip out your 
"tinyxml" and use that to read simple SVG format.

If you can't change the format of the input coordinates, find the forum 
which covers the format you must use, and ask your question there!

But it bears repeating that the complexity of C++ is not indicated for such 
a simple task!

-- 
  Phlip 


0
Reply Phlip 11/11/2007 4:16:51 AM

Tonny wrote:
> I am new to c++ programming.Could anyone tell me how I can use
> standard input in c++ to read 2D coordinates? e.g., read n elements
> which have x-coordinates and y-coordinates.
> 
> Could anyone show me a simple example? Thanks very much.

First step is to define your input format to be whatever is easiest
to parse using easy C++ constructs.  I'm not a C++ expert, but off
the top of my head, the easiest way to do this would be to say that
every coordinate is a decimal number and all the points are separated
by white space (spaces within a line or even line endings).  For
example, here's a sample input file containing the points (2.5,-3)
and (0,8):

	2.5 -3
	0 8

The following would also be equivalent:

	2.5 -3 0 8

As would this:

	2.5
	-3 0
	8

You'll note that even in the last example, the -3 and the 0 aren't part
of the same pair.  That's just to drive home the point that the position
on the line doesn't matter.

Second step would be to write some simple code to use the built-in
simple input stream parsing to do 99% of the work for you.  For
example:

     #include <iostream>

     int main(void) {
         float x1, y1;
         float x2, y2;

         // read two points
         std::cin >> x1 >> y1;
         std::cin >> x2 >> y2;

         // write them back out to verify they were read correctly
         std::cout << "first point: (" << x1 << "," << y1 << ")" << std::endl;
         std::cout << "second point: (" << x2 << "," << y2 << ")" << std::endl;
     }

I've omitted error checking and all that, but it gets the point across.
You would probably want to make some sort of "read a point from std::cin"
function, but I've left that out as well, because that's not really
specific to the concept of reading points.  You might also want to
make it read from a particular file instead of just the standard input,
but that just amounts to replacing "std::cin" with a variable.

   - Logan
0
Reply Logan 11/11/2007 5:49:21 AM
comp.programming 10711 articles. 18 followers. Post

2 Replies
678 Views

Similar Articles

[PageSpeed] 33


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

when to use C and when to use C++
Hi everyone, I have been asked this question quite a few times and i wonder what could the actual reason? " When to prefer C over C++ or vice versa, of course this is for real world pratical scenario" As per my understanding, C++ makes it easier to program application related logic as OOPS helps in that sense, but for some reason that is not a reasonable explanation, could any one add on their comments? sam_cit@yahoo.co.in wrote: > Hi everyone, > > I have been asked this question quite a few times and i wonder what > could the actual reason? > > &qu...

Using both C and C++ ISO standard libraries
(Originally posted as: another strange output?) >I just recently was educated that I should be using #include <iostream> >instead of using #include <iostream.h> as I had been doing. >However, even though the education cured one strange output issue, now >with the "correct" include spec I cannot get it to show the radix base like >it used to using the "incorrect" include spec. >Very much appreciate info as to what I am still missing. Code below >============================================== >#include <windows.h...

Is it true you cannot use the ANSI C (not C++) standard library functions in the kernel?
Hi. I have a question about the use of the ANSI C (not C++) Standard Library in kernel code for w2k/xp/vista. I've been told, somewhat in passing, by a good kernel developer, that the standard library functions (strcpy, qsort, etc) cannot be used in kernel code because "you can end up coming out of them in the wrong state". (We were talking about something else at that moment and he was avoiding getting into the full explanation). So, I'm not quite sure what he meant by that - I wonder now if he was talking about calling conventions? in the DDK, all the...

The use of the C++ standard
[I'm not positive if this should go here or comp.std.c++, so I hope you'll forgive me if you feel it is off topic here.] I've programmed in C++ for several years now (think 5 years seriously, and maybe 8 from when I made my hello world), and I'm debating whether or not I should buy the C++ standard document. So I have a couple questions about it. 1. Under what circumstances would you recommend buying it? Is it useful for mostly just compiler/library implementers, or do you guys use it in everyday programming? If so, how? 2. There's a published version for about 60 USD (s...

How to do analog input output of PCI-6052 or 6071 using simple C/C++ language
I need to drive a dc motor and read the potentiometer using the NI PCI-6052 or PCI-6071E by programming in simple C or C++ or Borland C but not in visual C. what should be the necessary steps? how to access the memory registers&nbsp;of NI Cards using C commands? &nbsp; Hi Haider, Adding to what Christian said here is a good link that should help you as well.&nbsp; <a href="http://zone.ni.com/devzone/cda/tut/p/id/5409" target="_blank"> http://zone.ni.com/devzone/cda/tut/p/id/5409</a> I found this by searching for "DAQmx text based" on...

difference between Standard C++ 98 and Standard C++ 2003
does anyone know the difference between Standard C++ 98 and Standard C++ 2003? --- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.comeaucomputing.com/csc/faq.html ] Unofficial list of revisions, by Andrew Koenig: http://www.acceleratedcpp.com/authors/koenig/c++std/revisions.pdf --- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news...

Why We Use C Than C++...
Hi, Please help me in this regard... All the kernel level programs are written in C... (i.e: Open Source LINUX)... Why are they not using C++... I personally feel that C++ is more easy to code than C... When i searched in our encyclopedia (i.e Google), i came up with several answers, telling "C is much faster than C++ and most kernal prgms uses only C" ... But why is it so...? Why they use C for these OS based programs.. Please Help me with detailed explanation... Cheers, Balaji Jayaraman... In a well developed C++ program, there isn't much difference between C and C++ perform...

C++ code with C-style interface for a library to be used in C++ and C?
Hi! I have a question about building and then using libraries containing C++ code. Let's say I have some C++ code and a .cpp file with 1 function that uses some other C++ code / classes etc... Also, any possible exception is handled within the function itself. In code: MyModule.h ---------- #ifndef MYMODULE_H #define MYMODULE_H extern int Test(float inParam1,float inParam2,float* outResult); #endif // #ifndef MYMODULE_H MyModule.cpp ------------ #include "MyModule.h" #include "MyClasses.h" // contains MyClassA and MyClassB int Test(float inParam1,float inParam2,...

What coding standard are you using for C?
What coding standard are you using for C (if any)? Like naming variables, functions, constants, globals, locals, types etc.? for example: MyFunction(); myFunction(); My_Function() my_function() UART_read() UartRead() UARTRead(); Geek wrote: > What coding standard are you using for C (if any)? > > Like naming variables, functions, constants, globals, locals, types etc.? > for example: > > MyFunction(); > myFunction(); > My_Function() > my_function() > > UART_read() > UartRead() > UARTRead(); You will get as many *different* answers as respondent...

the difference between standard c and standard c++ about their underlay realization mechanism
What is the difference between standard c and standard c++ about their underlay realization mechanism? I think it's hard to tell all the difference in detail,just me some your opinion. Thanks! On 21 Nov 2004 06:52:59 -0800, jut_bit_zx@eyou.com (zhou xiang) wrote in comp.lang.c++: > What is the difference between standard c and standard c++ about their > underlay realization mechanism? The standards are available quite inexpensively from www.ansi.org in PDF format. As for the realization mechanism, neither language specifies one. They define an abstract machine that execu...

C++ standards committee looking at adding Cairo to the C++ standard
It appears that Herb Sutter wants to add a 2D drawing library to the C++ standard, and he is looking at libCairo as a base for this library. The link to the Cairo's mailing list: http://lists.cairographics.org/archives/cairo/2013-December/024858.html What are your thoughts on this? Rui Maciel Rui Maciel <rui.maciel@gmail.com> wrote in news:labt7p$co4$1@dont-email.me: > It appears that Herb Sutter wants to add a 2D drawing library to the > C++ standard, and he is looking at libCairo as a base for this > library. > > The link to the Cairo'...

how to use the return value when using C# dlls in a C++ project?
hi, I am using a C# library in my C++ project. I want to make use of the classes defined in the library to do some job, and use the return value to go on other works in my project. But it seem difficult to convert a return value of C# datatype into C++ complaint datatype, like convert System::String in C# into std::string in C++. like in my test code: ClassA ^a = gcnew ClassA(); // ClassA is a class in the C# library System::String result = a-> getResult(); std::string str; // here I want to copy result to str, then I can use the string freely. But I don't know how to do it. Is th...

what should the minimum number of channels be on a DAQ board being used to acquire eight analogue signals (four using differental inputs and four using single-ended inputs)? and why?
I am sitting an exam and the lituture for this subject is a littel fuzzy.&nbsp;I think the answer is 13 analogue channels because the singel-ended inputs&nbsp;use a common ground so that makes 5 and then because the differential inputs&nbsp;each use there own groung referende thats 8. is this the correct answer and resoning? &nbsp; &nbsp; Also sorry to be a pain but "what is the maximum frequency of these signals , which can be measured using a DAQ board with the maximum sampling rate of 500kHz?"&nbsp;&nbsp;I have no idea how to begain to work this out as ...

Class B using Class C, and Class A using both B and C
The following code illustrates a problem I have been trying to get around in c++. Can anyone explain how I can get this to work. The code compiles fine if I leave out the lines with the comments after them. Tnaks in advance for any help. jd //file test.cpp: #include "pair.h" #include "triple.h" int main(){ pair y= pair(2,4); triple z = triple (1,2,3); pair *x= new pair(1,2); triple *fg= new triple (4,5,6); y.print_value(); z.print_value(); x->print_value(); fg->print_value(); return 0; } ...

UNICODE input for CGI using C
Dear All, I'm trying to accept a multi-lingual string (UNICODE) in a form and am trying to parse it. What i am getting is %XX (which is a single byte, not 2 bytes). So, is the data getting lost? What format is it, if it is not getting lost. Thanx in advance, Punit. In article <1180444998.814728.246960@a26g2000pre.googlegroups.com>, <puneet.p.shah@gmail.com> wrote: > I'm trying to accept a multi-lingual string (UNICODE) in a >form and am trying to parse it. What i am getting is %XX (which is a >single byte, not 2 bytes). So, is the data g...

Asynchronous programming using standard C
I'm interested in asynchronous programming but am having a few problems understanding the underlying implementation. I assume that asynchronous programming is generally done by calling a function (which includes a call back function pointer in its arguments) which starts a thread and then immediately returns. Once the thread has completed the task it then calls the call back function to return the data to the caller. Is this the basic idea of asynchronous programming or have I got it completely wrong? Thankfully threading is now on-topic in this group thanks to C11. ...

Standard Library of useful utilities in C...
I'm new to c programming, and I come to it from higher-level, object oriented languages. I was hoping to find a standard library that contained common structures and functions that would be used in many C programs. As an example, I am looking for a standard implementation of a hash table. Does such a library exist? I checked out the GNU C Library, but it didn't seem to have what I was looking for. Does every C programmer implement this stuff from scratch? Thanks, Scott Huey -- comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must have an appropr...

Redirect standard input using CORBA
Hello, i need help or some advice to do some work. I want to make a simple example, i've a Client and a Server. My client ask the server to run some program, and i want that the server return the output messages of the running program. I want to know how redirect the output stream over CORBA. For now i've created my client and my server. My client ask the server to run a program. The server execute the program. To launch the program on the server side, i've inherited from ACE_Process and i'm using a ACE_Process_Options to set the command and parameters. Tha...

Using the carry flag in standard C
Recently the discussion about the integer flags started again. I argued again with my "_carry()" function, but Mr Jean Marc Bourguet and Mr K. Thompson proposed a better alternative. Thinking about it, I think that their solution is much better than mine, since it produces less restrictions and fits better in the existing language. We could do something like: int add_with_carry(int a,int b, bool *carry); int sub_with_borrow(int a,int b,bool *borrow); int mult_with_carry(int a,int b,bool *carry); The advantage of this is that in machines that do not have any carry f...

Issue with Using Standard input operation
Hi, I'm calling gnuplot from a script and the data is only ready dynamically so I have to sent it to GnuPlot with the plot "-" operator. If I do the following from the GnuPlot command line it works.. gnuplot> plot "-" with linesp input data ('e' ends) > 1 2 input data ('e' ends) > 5 2 input data ('e' ends) > 6 3 input data ('e' ends) > 7 4 input data ('e' ends) > e which produces a plot lines lines and crosses on the data points (i'm excluding and extra settings for fitting data to scre...

Use a wrapper to build a C++ Dll that calls a C++ .lib and Use the Dll is LabView
Hi All, &nbsp; I need to access some functions in a C++ .lib within LabView, but has no source code for the library.&nbsp; Can I build a Dll in Visual C++ that calls this library, then have LabView calls the Dll? If so, how different is building this Dll compared to building a Dll from a .cpp file?&nbsp; I am new to C++, could some one suggest how to&nbsp;use the .lib in Visual C++ 6.0 studio to build the Dll? &nbsp; Thanks for&nbsp;your help. Bryan Hi Emilie, &nbsp; Thank you for the help. Sorry for the late reply here, got caught into a few busy projects. I do h...

Anyone out there successfully using Pro*C/C++ in C++ mode?
So far I had been mostly satisfied with Pro*C/C++. It is a huge improvement from by previous programming environment (OCI. I can't believe I ever programmed in that assembly language). All my programs have been written written in C so far. However, since I have been known to get myself into trouble :-) I decided to try the C++ side of Pro*C/C++, to take it for a spin. Long story short: It was a bad decision, and I decided to go back to good ole' C. Reasoning: (1) That is not real C++. I use C++ for the convenience of 'string', for instance, and the pre-compiler forces me to...

Extent of standard C/C++ library support in Visual C++
Hi, I am curious whether or not Visual C++ fully supports standard C/C++ APIs. I have gathered the standard C/C++ APIs (mainly from cplusplus.com) and posted that to the comp.sources.d newsgroup (too long to be posted here). Can someone kindly scan through the list to verify if everything standard is supported on Visual C++? Thanks, Song On Dec 28, 1:22 pm, songbg_...@yahoo.com wrote: > Hi, > > I am curious whether or not Visual C++ fully supports standard C/C++ > APIs. I have gathered the standard C/C++ APIs (mainly from > cplusplus.com) and posted that to ...

C Standard as part of C++
Hello group, To what extent is it necessary to have the C90 Standard, in addition to the C++ Standard, to have the complete, official reference to the C++ language? For example, I've noted that I can't find some C90 API function in the C++ Standard even though these functions are part of C++. In what other ways is it necessary for one to have the C90 Standard? Thanks to all, Dave [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] Dave Theese wrote: > Hello group, > > To what extent is it ...