Gillius's Programming Forums

Programming => Programming Questions => Topic started by: radly on February 27, 2006, 01:17:05 AM

Title: location?
Post by: radly 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......(",)



Title: location?
Post by: Gillius 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?
Title: Hey!!!
Post by: encargues_miguelerhlich 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.
Title: location?
Post by: radly on March 02, 2006, 01:41:49 AM
Sir,

               can i ask an example...?
Title: location?
Post by: louiecerv 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;
}
Title: Careful
Post by: louiecerv 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.
Title: location?
Post by: Gillius 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.
Title: location?
Post by: radly on March 06, 2006, 01:01:10 AM
tnx...