Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Simple C++ program trouble (http://www.go4expert.com/forums/simple-cpp-program-trouble-t8578/)

bsdixon21222 1Feb2008 01:33

Simple C++ program trouble
 
Hello all,

This is my first project with C++. I have completed everything so far, but one function is giving me trouble. I want to output the contents of the myInput.txt file into an array. The issue is that the commas need to stay within the file, but they cause the program to crash when fed into the array. I know I need to strip the commas out, but I am not too sure. Any help is appreciated...Thanks in advanced..

Code:

// Project 1.cpp : Defines the entry point for the console application.
//

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

using namespace std;

void FillMyInput();
void FillMyArray( int arr[], int SIZE );
int GetMax( int arr[], int SIZE );
void OutputArray( int arr[], int SIZE );

const int SIZE = 10;

int main(int argc, char* argv[])
{
        int arr[ SIZE ];

        FillMyInput();
 
        FillMyArray( arr, SIZE );
       
        GetMax( arr, SIZE );

        OutputArray( arr, SIZE );

        return 0;
}

void FillMyInput()
{
        ofstream fillFile;
        fillFile.open( "myInput.txt" );
        fillFile << "7,64,15,12,3,78,23,14,45,1";
        fillFile.close();
        cout << "Filled file with text" << endl;
}

void FillMyArray( int arr[], int SIZE )
{
        ifstream arrayFile;
        arrayFile.open( "myInput.txt" );
        for( char i = 0; i < SIZE; i++ )
                arrayFile >> arr[i];
        cout << endl;
        arrayFile.close();
        cout << "Filled arrary with file contents" <<endl;
}

int GetMax( int arr[], int SIZE )
{
        char c;
        int maxNum = 0;
        for( int i = 0; i < SIZE; i++ )
        {
                if (arr[i] > maxNum)
                        maxNum = arr[i];
        }
        cout << "\nThe largest number in the array was " << maxNum <<endl;
        return maxNum;

}

void OutputArray( int arr[], int SIZE )
{
        char c;
        cout << "\nArray Outputs\n" << endl;
        for( int i = 0; i < SIZE; i++ )
                cout << arr[i] << endl;
        cout << endl;
        cin >> c;

}



All times are GMT +5.5. The time now is 22:02.