Read & Write Program (Errors)

Discussion in 'Mac Programming' started by bravens52, Apr 23, 2010.

  1. bravens52 macrumors regular

    Joined:
    Jul 16, 2009
    #1
    hey guys,

    i am making a read and write program and i am getting a huge amount of errors and i don't know why. I went to see my TA (teachers assistant) and we wrote out the program but im still getting errors. Help?

    this is my read program

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    
    using namespace std;
    
    void makePlayer(char *inputLine, playerInfo &aPlayer); 
    
    int main()
    {
    	int playerInfo;
    	char aPlayer;
    	char ar[50];
    	
    	playerInfo aPlayer;
    	
    	ifstream input;
    	ofstream output;
    	
    	input.open( "players.txt" );
    	
    	if ( input.fail() )
    		
    		{
    		cout << " Error ";
    		exit(0);
    	}
    
    	output.open( "results.txt" );
    	if ( output.fail() )
    	{
    		cout << " Error ";
    		exit(0);
    	}
    	input.getline(ar,50);
    	while (input)
    	{
    		makePlayer(ar, aPlayer);
    		outFile.write( (char *) &aPlayer, sizeof(aPlayer) );
    		
    		input.getline(ar,50);
    	}
    
    
    	
    	return 0;
    	
    }
    
    void makePlayer(char *inputLine, playerInfo aPlayer)
    {
    	struct Player
    	{
    		char playerName(25);
    		int shots
    		int goals 
    		int assists
    	}
    	
    	char *ptr;
    	ptr = strchr(il, ':');
    	*ptr = '10';
    	
    	strcpy(aPlayer, playerName, il);
    	ptr++;
    	
    	il = strchr(ptr, ':');
    	*il = '10'
    	
    	aPlayer goals = atoi(ptr);
    	il++;
    	
    	ptr = strchr(il, ':');
    	*ptr = '10'
    	aPlayer assists = atoi(il);
    	ptr++;
    	
    	aPlayer shots = atoi(ptr);
    	
    }
    
    
    This is my Write Program
    
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    
    using namespace std;
    
    int buildArray(playerInfo playerAr[]);
    void displayArray( playerInfo playerAr{}, int numPlayers);
    
    int main()
    
    {
    	playerInfo players;
    	int numPlayers;
    	numPlayers=buildArrays(players);
    	displayArray(players,numplayers);
    
    	return 0;
    }
    
    buildArray(playerInfo,Players[])
    {
    	int i = 0;
    	ifstream inFile;
    	inFile.open("results.txt");
    	
    	if(inFile.fail() )
    	{
    		cout << "Error";
    		exit(i);
    	}
    	
    	inFile.read((char *) &aPlayer, sizeof aPlayer)
    	
    	while (inFile) 
    	{
    		players[i]=aPlayer;
    		
    		strcpy(players[i].players, aPlayers.playernum);
    		players[i].goals=aPlayer.goals;
    		i++
    		
    	}
    }
    
    displayArray(playerInfo players[], int numPlayers)
    {
    	for (i=0; i < numPlayers; i++) 
    	{
    		cout << players[i].playername;
    		cout << i;
    	}
    }
     
  2. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #2
    Well taking a quick look I see that your function declarations aren't matching your actual function definitions.

    Code:
    	int buildArray(playerInfo playerAr[]);
    	void displayArray( playerInfo playerAr{}, int numPlayers);
    	
    	buildArray(playerInfo, Players[])
    	{
    		...
    	}
    	
    	displayArray(playerInfo players[], int numPlayers)
    	{
    		...
    	}
    
    Rearranging this a little we can see this a little better.

    The funtion declaration doesn't match the definition here:

    Code:
    	int buildArray(playerInfo playerAr[]);
    	buildArray(playerInfo, Players[])
    	{
    		...
    	}
    
    nor does it match the funtion declaration here:

    Code:
    	void displayArray( playerInfo playerAr{}, int numPlayers);
    	displayArray(playerInfo players[], int numPlayers)
    	{
    		...
    	}
    

    You haven't declared what a 'playerInfo' is in a place that can be seen across all compilation units. I'd recommend placing it in a separate header file so as to make 100 percent sure both your read and write programs share exactly the same understanding as to its makeup.
     
  3. naples98 macrumors member

    naples98

    Joined:
    Sep 9, 2008
    Location:
    Houston
    #3
    Code:
    int main()
    {
    int playerInfo;
    char aPlayer;
    char ar[50];
    
    playerInfo aPlayer;
    lloyd has given you some great tips so you should start there.

    Also, think about what is going on with "int playerInfo" and "playerInfo aPlayer" in main. Is it a good idea to use playerInfo as a variable? It may be fine depending on the name of your struct.

    Finally, make sure you understand the difference between players and players[].
     
  4. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #4
    It might help you to take your assignment(s) and do something like the following:

    Code:
    /* =====================================================================================================================
     * File - playerInfo.h
     * ---------------------------------------------------------------------------------------------------------------------
     */
    
    #ifndef playerInfo_H
    #define playerInfo_H
    
    
    /* =====================================================================================================================
     * ---------------------------------------------------------------------------------------------------------------------
     */
    
    struct playerInfo
    {
    	char	playerName[25];
    	int	goalsScored;
    	int	assists;
    	int	shots;
    };
    
    #endif
    
    Code:
    /* =====================================================================================================================
     * File - write.cpp
     * ---------------------------------------------------------------------------------------------------------------------
     * Northern Illinois University, College Of Liberal Arts & Sciences
     * Department Of Computer Science
     * CSCI 240
     *  Computer Programming in C++
     *  Spring 2010 * 
     * 
     * <http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm10.htm>
     *
     * Overview
     * 
     * For this assignment, write 2 programs that will process information for the players that were with the Chicago
     * Blackhawks during the 2009 - 2010 regular season.
     * 
     * Both of the programs that are to be written for this assignment will deal with structures. The first program will
     * create a file of structures. The second will read and display the contents of the file created by the first program.
     * 
     * Structures may contain native data types (like int and double) rather than values converted to characters. This means
     * that files that contain structures cannot be created with a normal text editor, nor can they easily be viewed with a
     * text editor. Strings and characters may be viewable but the numeric data types will show up as garbage.
     * 
     * The structure that will be used in both of the programs will represent one player. It has fields for the player name,
     * number of goals scored, number of assists, and number of shots attempted.
     *  
     * 		 struct playerInfo
     * 		 {
     * 			 char playerName[25];
     * 			 int goalsScored;
     * 			 int assists;
     * 			 int shots;
     * 		 };
     * 
     * Program 1: The Write Program
     * 
     * This program will read some player information from a file, format the information, put it into a structure, and then
     * write that structure to an output file.
     * 
     * Input
     * 
     * The input for program 1 will be read from a file called players.txt. The file consists of a set of records for the
     * players that played during the 2009 - 2010 regular season. Each record represents one player and is made up of
     * multiple fields that are separated by colons. The fields for each player are: the player name, number of goals
     * scored, number of assists, and number of shots attempted. A record in the file resembles the following:
     * 
     * Patrick Kane:28:54:233 Output
     * 
     * The output for program 1 will be a file called team.txt, which will contain a structure for each of the players in
     * the players.txt file.
     * 
     * Suggested Logic for main()
     * 
     * Create a playerInfo variable, C-Style string, input file stream, and output file stream.
     * 
     * Open the file players.txt for input and verify that it opened correctly
     * 
     * Open the file team.txt for binary output and verify that it opened correctly
     * 
     * Use getline to read the first player from the input file into the C-Style string
     * 
     * While there are input records in the input file
     * 
     * Call the makePlayer function to break the C-Style string into the various fields for the structure
     * 
     * Write the structure to the output file using the write command
     * 
     * Use getline to read the next player from the input file into the C-Style string Endwhile
     * 
     * Close the input and output files main note:
     * 
     * A structure can be written to a file by using the write function. For example:
     * 
     * playerInfo aPlayer;
     * 
     * outFile.write( (char *) &aPlayer, sizeof(aPlayer) ); Function to write and use
     * 
     * void makePlayer( char *inputLine, playerInfo &aPlayer )
     * 
     * This function will take the information in inputLine and break it into the 4 individual fields for a player and use
     * them to build a playerInfo structure. It takes 2 arguments: a C-Style string that contains player information and a
     * reference to a playerInfo structure that can be used to pass back a player.
     * 
     * As was mentioned above, each of the fields in the C-Style string is separated by a colon. Use the strchr function to
     * search for a colon (':') so that each field can be isolated, strcpy or atoi can then be used to put the isolated
     * string into the appropriate field of the structure.
     * 
     * The atoi function will be used to convert a string to its numeric representation.
     * 
     * Program 1 Programming Notes:
     * 
     * Add #include <fstream> at the top of your program.
     * 
     * Copy the input file to your hard disk and write your program so that it reads the data from the current directory
     * (ie. don't specify a file path).
     * 
     * Write the program so that it writes to the current directory (ie. don't specify a file path).
     * 
     * Make sure to include "write" as part of the CPP file name.
     */
    
    // MARK: -
    /* =====================================================================================================================
     * ---------------------------------------------------------------------------------------------------------------------
     */
    
    // MARK: - INCLUDE SYSTEM HEADER FILES
    #include <fstream>
    #include <iostream>
    
    // MARK: - INCLUDE APPLICATION HEADER FILES
    #include "playerInfo.h"
    
    
    // MARK: -
    // MARK: - FUNCTION PROTOTYPES
    /* =====================================================================================================================
     * ---------------------------------------------------------------------------------------------------------------------
     */
    
    void makePlayer(char* inputLine, playerInfo& aPlayer);
    
    
    // MARK: -
    // MARK: - PROGRAM ENTRY POINT
    /* =====================================================================================================================
     * ---------------------------------------------------------------------------------------------------------------------
     */
    
    int main(int argc, const char* argv[])
    {
    	// Open the file players.txt for input and verify that it opened correctly
    	
    	// Open the file team.txt for binary output and verify that it opened correctly
    	
    	// Use getline to read the first player from the input file into the C-Style string
    	
    	// While there are input records in the input file
    	
    	// Call the makePlayer function to break the C-Style string into the various fields for the structure
    	
    	// Write the structure to the output file using the write command
    	
    	// Use getline to read the next player from the input file into the C-Style string Endwhile
    	
    	// Close the input and output files main note:
    }
    
    
    // MARK: -
    // MARK: - PROGRAM SUBROUTINES
    /* =====================================================================================================================
     * ---------------------------------------------------------------------------------------------------------------------
     * This function will take the information in inputLine and break it into the 4 individual fields for a player and use
     * them to build a playerInfo structure. It takes 2 arguments: a C-Style string that contains player information and a
     * reference to a playerInfo structure that can be used to pass back a player.
     * 
     * As was mentioned above, each of the fields in the C-Style string is separated by a colon. Use the strchr function to
     * search for a colon (':') so that each field can be isolated, strcpy or atoi can then be used to put the isolated
     * string into the appropriate field of the structure.
     * 
     * The atoi function will be used to convert a string to its numeric representation.
     */
    
    void makePlayer(char* inputLine, playerInfo& aPlayer)
    {
    }
    
    Code:
    /* =====================================================================================================================
     * File - read.cpp
     * ---------------------------------------------------------------------------------------------------------------------
     * Northern Illinois University, College Of Liberal Arts & Sciences
     * Department Of Computer Science
     * CSCI 240
     *  Computer Programming in C++
     *  Spring 2010 * 
     * 
     * <http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm10.htm>
     *
     * Overview
     * 
     * For this assignment, write 2 programs that will process information for the players that were with the Chicago
     * Blackhawks during the 2009 - 2010 regular season.
     * 
     * Both of the programs that are to be written for this assignment will deal with structures. The first program will
     * create a file of structures. The second will read and display the contents of the file created by the first program.
     * 
     * Structures may contain native data types (like int and double) rather than values converted to characters. This means
     * that files that contain structures cannot be created with a normal text editor, nor can they easily be viewed with a
     * text editor. Strings and characters may be viewable but the numeric data types will show up as garbage.
     * 
     * The structure that will be used in both of the programs will represent one player. It has fields for the player name,
     * number of goals scored, number of assists, and number of shots attempted.
     * 
     * 	   struct playerInfo
     * 	   {
     * 		   char playerName[25];
     * 		   int goalsScored;
     * 		   int assists;
     * 		   int shots;
     * 	   };
     *    
     * Program 2: The Read Program
     * 
     * This program will read the file of structures that was created by program 1 (team.txt) and place the player
     * information into an array of playerInfo structures. The information will then be printed.
     * 
     * Suggested Logic for main()
     * 
     * Create an array of playerInfo and an integer to hold the number of players
     * 
     * Call the buildArray function to build the array of playerInfo structures
     * 
     * Call the displayArray function to display the array of playerInfo structures Functions to write and use
     * 
     * int buildArray( playerInfo playerAr[] )
     * 
     * This function will read the file of data and fill the array. It takes as its argument the array of playerInfo
     * structures. It returns the number of valid players.
     * 
     * Suggested logic:
     * 
     * Declare an input file stream and any other variables that are needed
     * 
     * Open the input file for binary input and make sure that it opened correctly
     * 
     * Initialize a subscript to the beginning of the array
     * 
     * Use the read function to get the first player from input
     * 
     * While there are players in the file
     * 
     * Put the player into the playerInfo array
     * 
     * Increment the subscript to the next spot in the array
     * 
     * Use the read function to get the next player from input Endwhile
     * 
     * Close the input file
     * 
     * Return the number of players in the array (think about the subscript) buildArray note:
     * 
     * A structure can be read from a file by using the read function. For example:
     * 
     * playerInfo aPlayer;
     * 
     * inFile.read( (char *) &aPlayer, sizeof(aPlayer) ); void displayArray( playerInfo playerAr[], int numPlayers )
     * 
     * This function will display the information for the Chicago Blackhawks players. For each player, display their name,
     * number of goals scored, number of assists, number of points, number of shots, and shooting percentage.
     * 
     * This function takes as its arguments the array of playerInfo structures and the number of players in the array.
     * 
     * Use the following formulas for the calculated values:
     * 
     * Number of Points = Number of goals scored + Number of assists
     * 
     * Shooting Percentage = Number of Goals Scored / Number of shots taken * 100 Use the following as a basic format for
     * the output:
     * 
     * Chicago Blackhawks 2009-2010 Player Stats
     * 
     * Player               Goals   Assists   Points   Shots   Shooting %
     * ------------------------------------------------------------------
     * Patrick Kane           28      54        82      233      12.0
     * Duncan Keith           13      52        65      193       6.7
     * Patrick Sharp          22      39        61      241       9.1
     * Jonathan Toews         22      37        59      185      11.9
     * Marion Hossa           22      22        44      174      12.6
     * Troy Brouwer           21      18        39      108      19.4
     * Brian Campbell          7      31        38      131       5.3
     * 
     * NOTE 1: It's possible that a player didn't attempt any shots during the season. If that's the case, the shooting
     * percentage should be 0.0%.
     * 
     * NOTE 2: The player names should be left justified. All of the numeric values should be right justified. The shooting
     * percentage should be displayed with exactly 1 digit after the decimal point.
     * 
     * Program 2 Programming Notes:
     * 
     * Add #include <fstream> at the top of your program.
     * 
     * The array should be able to hold 30 elements. Use a symbolic constant to represent the maximum size of the array.
     * 
     * Copy the input file to your hard disk and write your program so that it reads the data from the current directory
     * (ie. don't specify a file path).
     * 
     * Make sure to include "read" as part of the CPP file name.
     */
    
    // MARK: -
    /* =====================================================================================================================
     * ---------------------------------------------------------------------------------------------------------------------
     */
    
    // MARK: - INCLUDE SYSTEM HEADER FILES
    #include <fstream>
    #include <iostream>
    
    // MARK: - INCLUDE APPLICATION HEADER FILES
    #include "playerInfo.h"
    
    
    // MARK: -
    // MARK: - FUNCTION PROTOTYPES
    /* =====================================================================================================================
     * ---------------------------------------------------------------------------------------------------------------------
     */
    
    int buildArray(playerInfo playerAr[]);
    
    
    // MARK: -
    // MARK: - PROGRAM ENTRY POINT
    /* =====================================================================================================================
     * ---------------------------------------------------------------------------------------------------------------------
     */
    
    int main(int argc, const char* argv[])
    {
    	// Create an array of playerInfo and an integer to hold the number of players
    
    	// Call the buildArray function to build the array of playerInfo structures
    
    	// Call the displayArray function to display the array of playerInfo structures Functions to write and use
    }
    
    
    // MARK: -
    // MARK: - PROGRAM SUBROUTINES
    /* =====================================================================================================================
     * ---------------------------------------------------------------------------------------------------------------------
     * This function will read the file of data and fill the array. It takes as its argument the array of playerInfo
     * structures. It returns the number of valid players.
     */
    
    int buildArray(playerInfo playerAr[])
    {
    }
    
    You'll note that your assignments contain enough information to use as an outline for your program.

    Embedding the assignment makes it easy to reference as you fill in the outline it contains.

    Copying sections of the suggestions as comments into the suggested functions and then implement per the comments.

    Code:
    int main(int argc, const char* argv[])
    {
    	// Open the file players.txt for input and verify that it opened correctly
    	
    	// Open the file team.txt for binary output and verify that it opened correctly
    	
    	// Use getline to read the first player from the input file into the C-Style string
    	
    	// While there are input records in the input file
    	
    	// Call the makePlayer function to break the C-Style string into the various fields for the structure
    	
    	// Write the structure to the output file using the write command
    	
    	// Use getline to read the next player from the input file into the C-Style string Endwhile
    	
    	// Close the input and output files main note:
    }
    
    
    /* =====================================================================================================================
     * ---------------------------------------------------------------------------------------------------------------------
     * This function will take the information in inputLine and break it into the 4 individual fields for a player and use
     * them to build a playerInfo structure. It takes 2 arguments: a C-Style string that contains player information and a
     * reference to a playerInfo structure that can be used to pass back a player.
     * 
     * As was mentioned above, each of the fields in the C-Style string is separated by a colon. Use the strchr function to
     * search for a colon (':') so that each field can be isolated, strcpy or atoi can then be used to put the isolated
     * string into the appropriate field of the structure.
     * 
     * The atoi function will be used to convert a string to its numeric representation.
     */
    
    void makePlayer(char* inputLine, playerInfo& aPlayer)
    {
    }
    
    And finally remember to remove any such comments that no longer represent the actual code. There is nothing more misleading than comments that build a false mental "image" of what the code is doing.
     

Share This Page