f



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

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
11/11/2007 4:07:32 AM
comp.programming 11414 articles. 1 followers. Post Follow

2 Replies
937 Views

Similar Articles

[PageSpeed] 52

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
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
Logan
11/11/2007 5:49:21 AM
Reply: