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 10569 articles. 18 followers. Post

2 Replies
564 Views

Similar Articles

[PageSpeed] 5


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

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

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

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

Input/Output, C++ to IDL, using Procedures
I am using Visual C to run a program that uses IDL functions to process data. But now I want to have C open IDL GUIs at specific times. The problem that I am having is that all the calling of IDL from C that I have done so far has been with functions. And all the GUI work is done with procedures. Does anyone know of a way to call an IDL GUI from Visual C and then return the GUI variables to C? Sorry, this response may be a bit weak but no one else has stepped up... Why don't you write your GUI procedures as functions? A simple wrapper would suffice. Maybe I am missing something. I ...

Compiling standard c source using CC
Using the Forte 11 (Sun C++ 5.8) compiler on a Solaris 10 system, most of the source files are C++; however there are a few standard c source files mixed in; I want to compile these using standard c rules, not C+ +, so the compiler doesn't complain about C++ keywords. Is there a compile option that will force a standard c, rather than a C++, compile? Something similar to the GNU GCC compile option "-x c". kenkahn wrote: > Using the Forte 11 (Sun C++ 5.8) compiler on a Solaris 10 system, most > of the source files are C++; however there are a few standard c source > ...

Rename a file using the standard C++ library?
Is there a function in the standard C++ library that serves the purpose of renaming a file? If not, do I have to use boost or operating system specific code? Thanks. Jason Heyes wrote: > Is there a function in the standard C++ library that serves the purpose of > renaming a file? If not, do I have to use boost or operating system specific > code? Thanks. I guess you could have a look at rename() method declared in stdio.h. It might be there in the non-deprecated <cstdio>. int rename ( const char * oldname , const char * newname ); ...

how to block keyboard input in Linux using C
I am running a console program in linux and want to block input from the keyboard but I do want to allow only a select few keyboard inputs for example 'a', 'b', and 'c' allowed but other input is blocked. I am not sure what's a good method to do this, I have read about using select or poll but not sure if those are best or only options Any help would be appreciated. Thank you. In article <4eebf996$0$281$14726298@news.sunsite.dk>, Durango <El_Durango@yahoo.com> wrote: > I am running a console program in linux and want to block input from the >...

Using standard input feature "-"
Hi, I'm having a little trouble with the stanard input feature as I'm sending my data/commands to gnuplot via a script. My data is generated on the fly so I need to use the standard input feature "-". If I just run gnuplot by itself and type and example as follows it works... gnuplot> set offsets 0.1 gnuplot> plot "-" with linesp input data ('e' ends) > 1 1 input data ('e' ends) > 1 5 input data ('e' ends) > 5 5 input data ('e' ends) > 5 1 input data ('e' ends) > 1 1 This draws draws as...

use of C++ Standard Library in the game industry
A sanity check: is the C++ Standard Library (STL) used much in the game industry? In all my years of C++, I've avoided learning or using it. Before bothering to write a bunch of code with it, I'd like to know if it's a common industry practice, or if it is common to avoid it in favor of homebrew. I do know that it was only within recent years that MSVC got its act together with a fully working, standard STL. So maybe I'm not quite the dinosaur I think I am? When I first learned C++ in 1993 the STL was not worth bothering with. Cheers, Brandon Van Every In ...

Using C++ standard library with pthreads in Solaris 9
Hi, I was wondering if anyone would know if the standard C++ library provided in Solaris 9 is thread safe. I cannot find any documentation to say that it is - and I have seen problems occur that are enough to make me think that it isn't. I understand that the library is provided by "Rogue Wave", who in turn seem to base their source on the "Apache C++" library. I have tried recompiling the Apache C++ library with the "thread-safe" options turned on, however, the resultant library "libstd.so.1" is differently named to that in "/lib/sparcv9/&quo...

C++ skills and commonly (?) used non-standard libraries.
Hello, I have a couple of years of experience with C++. I started studying C++ syntax, then I read the B.Stroustrup's book, and eventually I went through the N.Josuttis' book on how to program with the C++ Standard Library. I am not a professional programmer however I would like to gain more knowledge of the language to get what could be considered the skills you'd expect from a professional developer ( I also know Unix programming and some Ix86 32-bit Assembly programming too ). My questions are about what to do in order to get the above-mentioned skills. I've heard of comm...

fastest 2D access, use screen pixel coordinates
Hi, i'd like to use OpenGL to do some 2D graphics and i'd like to use the fastest mode possible. One thing that seems critical to me is drawing "sprites" or bitmaps. I heard that using glDrawPixels() is NOT very fast, generating a texture from a bitmap and then drawing that texture is much faster as i understood it, right? As i only want to do 2D graphics, i'd like to use screen pixel coordinates in my program. I tried this by DISABLING GL_DEPTH_TEST and loading Identity matrices to GL_PROJECTION and GL_MODELVIEW. I've set glViewPort with 0, 0, width, height (of ...

how to run some exe file by using standard C++ code?
I have some program in the executable form (for example notepad.exe in windows) and I want to run it in C++ code. How to do it? "Pawel_Iks" <pawel.labedzki@gmail.com> wrote in message news:1190211503.409309.78600@w3g2000hsg.googlegroups.com... >I have some program in the executable form (for example notepad.exe in > windows) and I want to run it in C++ code. How to do it? This is better asked in a windows newsgroup because the answer can be OS specific. system is one way, but then you can also run the executable in it's own process, which becomes windows specif...

MSXML C++
Hi, Does anyone know how to load XML from a input stream over a network. I've written the code to load XML files into a DOM object, but i now need to write the code so XML can be read in from a input stream buffer? Do i have to use SAX ? Replies much appreciated Romeel romeel_k@yahoo.co.uk It depends on the size of your XML message. DOM process needs to load the entire message into memory, whereas SAX doesn't... romeel_k@yahoo.co.uk (Romeel) wrote in message news:<a800c0d2.0309160302.6cd8eea7@posting.google.com>... > Hi, > > Does anyone know how to load XML from ...

using ISO C interop for mixed-language input to fortran
Hello good people in c.l.f.! I've used interop features of C/Fortran before, but this endeavor is a little different in that this goes in the other direction from what I have previously written, where C calls my unbelievably-effective fortran functions. (That might be the other/other direction for others; we all have our perspectives, usually carved by experience.) So I would like to call a C function that will return this data, and fortran does not know the size of the thing being passed to it. I believe this can be achieved by allocatables, but here's the C of it: $ cc -Wa...

help regarding 2D matrix using c mex file
Hi I am beginner and learning mex programming in C. I am trying to read a 2D matrix and print the pythagoras squares. Thought compiling is successful, MATALB has thrown exception. Can someone help me please... thanking you very much, with regards, ramana ********* #include "mex.h" #include <math.h> #define OUT plhs[0] #define SAMPLE double /* define the type used for data samples */ /****************************************************************************/ /* Function Declarations */ /***********************...