This is my whole program and what I am trying to do. Nothing is being outputted through the functions though. When I output the array in the client code it prints correctly but not through the recursive function. So I am wondering what I am doing incorrectly?

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);

Thanks for all of your help.

Recommended Answers

All 5 Replies

I still have not found anything that will work for my program so anything you can find will be very helpful. Thank you again in advance.

Does it work if you replace outFile.open("out.data"); with outFile = cout; ? With this change you should at least see output to your terminal or IDE output-window.

If you have a problem opening "out.data", your error-statement at line 11 will fail. You can't output an error message to a stream that you failed to open! Use cout (or better yet, cerr) instead.

Then, after reading your input file, you call printArray with variables first and last uninitialized. Presumably you'd at least want to hard-code them to 0 and MAX_SIZE-1, although you should be smarter about verifying that you were able to read MAX_SIZE values from your input file.

Alright I tried all of that and it still is not working for me, nothing is being outputted still.

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

I can't read minds. If you've changed your code, and it still doesn't work for you, please post your current code so we can all see what it looks like now!

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.