help with variables


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: help with variables

  1. #1
    Join Date
    Feb 2007
    Posts
    6

    help with variables

    im a noob to c++ (so if i say something wrong dont get angry)

    ok, now im trying to have the user enter multiple values as input(x,y coordinates to be exact) and im wondering without knowing how many, how do i assign variables to each of them(if you want to know exactly what im trying to do, its a program for finding the centroid and area of a polygon)

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    Do you read pairs of values and you don't know how many pairs the user would want to insert?
    I would use two doubles and a container.
    First initialize the two variables, then read the first pair using
    Code:
    cin>>elem1;
    cin>>elem2;
    After each pair reading, insert the values just read to a vector of double, or better yet a vector <pair<double> >;
    If the pair type seems scary, you can replace it with a simple struct:
    Code:
    struct MyPair
    {
    double d1, d2;
    } mp;
    while (...//until the user presses some key)
    {
     cin>>mp.d1;
     cin>>mp.d2;
     vector<MyPair> myvec;
     myvec.pushback(mp);
    }
    If you're not familiar with vectors, you should be!
    Here's a short tutorial: http://www.devx.com/DevX/LegacyLink/9396
    Last edited by Danny; 03-09-2007 at 03:51 PM.
    Danny Kalev

Similar Threads

  1. Replies: 0
    Last Post: 06-28-2001, 09:59 AM
  2. Replies: 0
    Last Post: 06-15-2001, 11:27 AM
  3. Replies: 0
    Last Post: 06-14-2001, 06:16 PM
  4. Replies: 0
    Last Post: 04-26-2001, 10:01 PM
  5. How To Do It - Shared Class Variables Part II
    By Patrick Ireland in forum .NET
    Replies: 3
    Last Post: 04-26-2001, 02:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center