Hi
How would I write a function that reads numbers from a txt file into an array. For example a function to call this code below:

float gradeData[12] = {0.0F};
int i; // loop counter
ifstream myfile ("rainfall.txt");
if( myfile.is_open() )
{
    i = 0;
    while( i < 12 && myfile >> gradeData[i] )
            i++;
}

First declare a function like so :

int readInt(std::ifstream& readFile){
  int res = 0;
  //error checking if you want
  readFile >> res;
  return res;
}

Then you can make another function that loops that function like so :

void readIntArray(std::ifstream& readFile, int *Array, const int Size){
  int index = 0;
  //while index is not equal to Size AND !readFile.fail 
 //  Array[index] equals readInt(readFile);
 // ++index;
}

Try something like that.

Thanks a lot... I'll try that. Really appreciate the help.....

Hi.... it's still not working... there's an error that I can't find... pls help....

/**This program reads monthly rainfall data from a file called rainfall.txt which
contains 12 amounts of rainfall, performs some arithmetic processes on the data
and writes the processed data to a file called results.txt. **/

#include <iostream>
#include <fstream>
#include <string>
using namespace std;


float readFloat(std::ifstream& readFile)
    {
       float holdData = 0;
       while(holdData != (-(holdData)))//error checking 
       {
       readFile >> holdData;
       return holdData;
       }
    }

void readFloatArray(std::ifstream& readFile, float *Array, const float Size)
    {
        float index = 0;
        //while index is not equal to Size AND !readFile.fail 
        //  Array[index] equals readInt(readFile);
        // ++index;
    }
     

int main()
{
    float holdData;
 
    

    

    
    
    float rainfallData[12] = {0.0F};
    float i; // loop counter
    ifstream myfile ("rainfall.txt");
    if( myfile.is_open() )
    {
        float i = 0;
        while(i < 12 && myfile >> holdData[i] )
            i++;
    }
    
    
}

This function :

float readFloat(std::ifstream& readFile)
    {
       float holdData = 0;
       while(holdData != (-(holdData)))//error checking 
       {
       readFile >> holdData;
       return holdData;
       }
    }

Is very bad, especially this part:

while(holdData != (-(holdData)))//error checking

That reads as, while holdData is not equal to negative holdData,
which is always true, unless holdData equals 0. Make your function
similar to this :

float readFloat(std::istream& readFrom){
 float result = 0.0f;
 if(! ( istream >> result ) ) //if reading failed{
    cout << "Error occured during reading, return 0.0f" << endl;
    return 0.0f;
 }
 return result; //else reading was successful
}

Thanks... I'll do that.... appreciate your advice and the errors being pointed out. Grateful....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.