Simple C++ program trouble

Discussion in 'C++' started by bsdixon21222, Jan 31, 2008.

  1. bsdixon21222

    bsdixon21222 New Member

    Joined:
    Jan 31, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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;
    
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice