Author Topic: location?  (Read 6472 times)

radly

  • User
  • *
  • Posts: 3
location?
« on: February 27, 2006, 01:17:05 AM »

Sir, i would like to ask this:

if i know the location of a certain number, example the location 3.
then i open an external file with the numbers which is inorder(increasing)
how can i output the number in the 3rd location?



ty......(",)




Gillius

  • Administrator
  • User
  • *****
  • Posts: 147
    • http://www.gillius.org/
location?
« Reply #1 on: February 27, 2006, 07:03:22 PM »
Can you just keep a counter initialized at 3, and if the counter reaches 0, then you output the number?
Gillius
Gillius's Programming http://www.gillius.org/

encargues_miguelerhlich

  • User
  • *
  • Posts: 6
    • http://bsit.bravehost.com
Hey!!!
« Reply #2 on: February 28, 2006, 08:42:45 AM »
you can also use the getline() function..... hehehe... but im not sure with it.. i'd alreadt trid it na.
Miguel ErhlichD. Encargues

radly

  • User
  • *
  • Posts: 3
location?
« Reply #3 on: March 02, 2006, 01:41:49 AM »
Sir,

               can i ask an example...?

louiecerv

  • Professor
  • User
  • *
  • Posts: 85
location?
« Reply #4 on: March 02, 2006, 07:57:51 AM »
Quote from: "radly"
Sir,

               can i ask an example...?


Code: [Select]


int getValueAt(int n)
{
    ifstream fin;
     fin.open("numlist.txt");
     int next;
     for (int i=0;i<n;i++)
     {
         fin >> next;
     }
     fin.close();  
     return next;
}
Analyze. Design. Develop. Debug. Deploy. Maintain.

louiecerv

  • Professor
  • User
  • *
  • Posts: 85
Careful
« Reply #5 on: March 02, 2006, 08:02:20 AM »
The sample assumes that there is valid data at the position requested.  It is left as exercise for you to add validation code to avoid the exception.
Analyze. Design. Develop. Debug. Deploy. Maintain.

Gillius

  • Administrator
  • User
  • *****
  • Posts: 147
    • http://www.gillius.org/
location?
« Reply #6 on: March 02, 2006, 06:21:18 PM »
Actually no execption will ever occur with this code.  If there is nothing there or there are things other than numbers in the file it will return either the last number seen in the file before the error or a random (undefined) number.
Gillius
Gillius's Programming http://www.gillius.org/

radly

  • User
  • *
  • Posts: 3
location?
« Reply #7 on: March 06, 2006, 01:01:10 AM »
tnx...