I am having trouble inputting sample data into an array and am wondering what I need to do to correct this?

The input sample is like this: 3 7 9 3 8 2 10 11 4 14

This is the code I am using and is not working. The MAX_SIZE of the array is 10.

void readToArray(int intArray[], ifstream& inFile)
{
  int i;
  for(i = 0; i < MAX_SIZE; i++)
    {
      inFile >> intArray[i];
    }
}

Thanks for the help!

This is the code I am using and is not working.

Then you did something wrong. Since you didn't bother to explain what "not working" means, that's all we can say.

I copied your code exactly, and it seems to be working perfectly fine. I created a main method to access it, using a file containing your sample data:

#include <iostream>
#include <fstream>

using namespace std;

const int MAX_SIZE = 10;

void readToArray(int intArray[], ifstream &inFile);

void main () {
	ifstream file = ifstream("C:\\test.txt");

	int intArray[MAX_SIZE];
	
	readToArray(intArray, file);

	for(int i = 0; i < MAX_SIZE; i++)
		cout << intArray[i] << endl;

	system("pause");
}

void readToArray(int intArray[], ifstream& inFile)
{
  int i;
  for(i = 0; i < MAX_SIZE; i++)
    {
      inFile >> intArray[i];
    }
}

There must be something else going on. Are you getting a compile-time error? What is it? You may need to post more of your source code...

Alright thank you nmaillet that is working now. I am trying to print the array recursively now and when I put it through that function there is no output.

This is my recursive print function

void printArray(const int intArray[], int first, int last, ofstream& outFile)
{
  outFile << "Array:";
    if(first <= last)
      {
        outFile << intArray[first] << " ";
        printArray(intArray, first + 1, last, outFile);
      }
}

This is my whole program and what I am trying to do. Nothing is being outputted though.

Input is like this: 3 7 9 3 8 2 10 11 4 14
The Sum function adds up from 1 all the way to the last number in the array so the sum of 1+2+3+4+5...+14=105.

Output should look like this:
Array: 3 7 9 3 8 2 10 11 4 14
Array in reverse order:14 4 11 10 2 8 3 9 7 3
SUM of 1-14: 105
3 power of 7: 2187

#include "recur.h"
int main()
{
  ifstream inFile;
  ofstream outFile;
  inFile.open("in.data");
  outFile.open("out.data");

  if(inFile.fail() || outFile.fail())
    {
      outFile << "Input or Output file ERROR!" << endl;
    }

  int first;
  int last;
  int intArray[MAX_SIZE];
  int sum;
  int x;
  int n;
  int power;

  readToArray(intArray, inFile);
  printArray(intArray, first, last, outFile); 
  printArrayRev(intArray, first, last, outFile);
  outFile << "SUM of 1-10:" << sum << endl;
  outFile << x << "power of" << n << ":" << power << endl;

  return 0;
}
//recur.cxx
#include "recur.h"

void readToArray(int intArray[], ifstream& inFile)
{
  int i;
  for(i = 0; i < MAX_SIZE; i++)
    {
      inFile >> intArray[i];
    }
}

int Power(int x, int n)
{
  int power;

  if(n == 0)
    return 1;
  else
    {
      power = (x*Power(x, n-1));
      return power;
    }
}

void printArray(const int intArray[], int first, int last, ofstream& outFile)
{
  outFile << "Array:";
  if(first <= last)
    {
      outFile << intArray[first] << " ";
      printArray(intArray, first + 1, last, outFile);
    }
}

void printArrayRev(const int intArray[], int first, int last, ofstream& outFile)
{
  outFile << "Array in reverse order:";
  if(last >= first)
    {
      outFile << intArray[first] << " ";
      printArrayRev(intArray, first, last -1,  outFile);
    }
}

int sumOfInt(int n)
{
  int sum;

  if(n > 0)
    {
      sum = n + sumOfInt(n-1);
      return sum;
    }
  else return 0;
}
//recur.h
#include <iostream>
#include <iomanip>
#include <fstream>

const int MAX_SIZE = 10;

using namespace std;

void readToArray(int intArray[], ifstream& inFile);
int Power(int x, int n);
void printArray(const int intArray[], int first, int last, ofstream& outFile);
void printArrayRev(const int intArray[], int first, int last, ofstream& outFile);
int sumOfInt(int n);

Near the top of your main():

if(inFile.fail() || outFile.fail())
{
    outFile << "Input or Output file ERROR!" << endl;
}

but if outFile is in a fail-state, you probably can't output to it! Try instead:

cerr << "Input or Output file ERROR!" << endl;

This will at least tell you if there was a problem opening the files. Also at this point, your program should probably give up, since it either won't be able to read in the input data, or else won't be able to write the output, so add also add:

return 1;

at the same time.

Edited 5 Years Ago by raptr_dflo: typo

I still have not found out what I am doing wrong so if anyone has any input in to what is causing it not to print out that would be great. Thanks

This article has been dead for over six months. Start a new discussion instead.