Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Question regrading arrays, functions and file i/o
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
JR




PostPosted: Thu Nov 04, 2004 7:59 pm   Post subject: Question regrading arrays, functions and file i/o

ok lets say i got something like this

code:


#include <iostream.h>
#include <fstream.h>

void load()
{
ifstream fin("data7.txt");

if (!fin)
{
cout<<"File data7.txt not found"<<endl;
}
else
{
int age[25];
for (i=0;i<26;i++)
{
fin>>age[i];
}

}



ok this is the function basically. now what i want to do is when lets say u press 1. it loads the numbers from the file data7. i dont have the menu here so its just an example.

now first i gotta initialize the array to 0 which is not that hard.
now the thing is
lets say i dont load the file and i press show, it will show the array which will be equal to 0. now i wnat it to do such as that when i press load it will load the array and when i press show it will show it. the probelm is that i cant do it right. i have another ifstream in the output function to output the array when its loaded but then it will always show that array because the load happens inside of the output function. how to make it work? like how to make it such that when i press 1 it loads and if i press 2 before 1 it will output the array that was initialied and when i press 1 and then 2 it will output the numbers from the loaded file.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Nov 04, 2004 8:23 pm   Post subject: (No subject)

First off, let's refomat that so it's easier to read.

code:
#include <iostream>
#include <fstream>

void load()
{
        std::ifstream inputFile("data7.txt");

        if (!inputFile)
        {
                std::cout << "File data7.txt not found" << std::endl;
        }
        else
        {
                int age[25];
               
                for (i = 0; i < 26; i++)
                {
                        inputFile >> age[i];
                }
        }
}


I've also taken the liberty of changing things to make them standards compliant, and made the variable names a bit more descriptive.

Now, your primary problem is:

code:
int age[25];


You're creating the function locally, then expecting to be able to use it elsewhere. You can't. Instead, you should pass the array into the function. Note that you'll also have to pass in the length of the array.

code:
#include <iostream>
#include <fstream>

void load(int age[], int length)
{
        std::ifstream inputFile("data7.txt");

        if (!inputFile)
        {
                std::cout << "File data7.txt not found" << std::endl;
        }
        else
        {
                for (i = 0; i < length; i++)
                {
                        inputFile >> age[i];
                }
        }
}


Of course, we could go one step further. Instead of hard-coding the name of the file...

code:
#include <iostream>
#include <fstream>

void load(std::ifstream& inputFile, int age[], int length)
{
        if (!inputFile)
        {
                std::cout << "Couldn't read from file." << std::endl;
        }
        else
        {
                for (i = 0; i < length; i++)
                {
                        inputFile >> age[i];
                }
        }
}


Calling it can then be:

code:
#include <iostream>
#include <fstream>

void load(std::ifstream& inputFile, int age[], int length);

int main()
{
        int age[25];
        std::ifstream inputFile("data7.txt");

        while (true)
        {
                int choice = 0;

                std::cout << "1 for reading, and 2 for output." << std::endl;
                std::cin >> choice;

                if (choice == 1)
                {
                        load(inputFile, age, 25);
                }
                else if (choice == 2)
                {
                        // print out the array
                }
                else
                {
                        std::cerr << "Invalid choice." << std::endl;
                }
        }

        return 0;
}

void load(std::ifstream& inputFile, int age[], int length)
{
        if (!inputFile)
        {
                std::cerr << "Couldn't read from file." << std::endl;
        }
        else
        {
                for (i = 0; i < length; i++)
                {
                        inputFile >> age[i];
                }
        }
}


And if you want something that maybe looks a bit prettier...

code:
#include <iostream>
#include <fstream>

using namespace std;

void load(ifstream& inputFile, int age[], int length);

int main()
{
   int age[25];
   ifstream inputFile("data7.txt");

   while (true)
   {
      int choice = 0;

      cout << "1 for reading, and 2 for output." << endl;
      cin >> choice;

      if (choice == 1)
      {
         load(inputFile, age, 25);
      }
      else if (choice == 2)
      {
         // print out the array
      }
      else
      {
         cerr << "Invalid choice." << endl;
      }
   }

   return 0;
}

void load(ifstream& inputFile, int age[], int length)
{
   if (!inputFile)
   {
      cerr << "Couldn't read from file." << endl;
   }
   else
   {
      for (i = 0; i < length; i++)
      {
         inputFile >> age[i];
      }
   }
}
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: