12 Replies - 831 Views - Last Post: 01 October 2009 - 07:37 AM Rate Topic: -----

#1 xxwolfsrainxx  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 93
  • Joined: 04-December 08

Converting a C++ File into a C file

Posted 30 September 2009 - 11:46 AM

The Problem I'm having is the way in my C++ file we use ifstream and I'm trying to use's C's fscanf in place but it says, fscanf' cannot start a parameter declaration. Attached the baskets.sun file as well.
This is the main part I'm having trouble with:
//Function Prototypes
void OpenToRead(fscanf&);
void Load(fscanf& gamefile, SunsRecord[], int& length, int& numberofhomegames, int& numberofawaygames, int& overallwin,
	int& overallloss, int& totalwinningpointdifference, int& totallosingpointdifference);
void Sort(SunsRecord games[], int length);
void printToFile(const SunsRecord[], int length, int numberofhomegames, int numberofawaygames, int overallwin,
	int overallloss, int totalwinningpointdifference, int totallosingpointdifference);

int main(void)
{SunsRecord games[86]; // array of sunsrecord records 86 because the number of games in a season
 int length;					  // number of actual records in file
 //Have to declare these in main so that I can use them in printToFile and in Load.
 int numberofhomegames;
 int numberofawaygames;
 int overallwin;
 int overallloss;
 int totalwinningpointdifference;
 int totallosingpointdifference;

 fscanf gamefile;			// input file variable

 OpenToRead(gamefile);		 // get name and open file for input
 Load(gamefile, games, length, numberofhomegames, numberofawaygames, overallwin, overallloss,
	totalwinningpointdifference, totallosingpointdifference); // read file into array of structs
 Sort(games, length);		  // sort the array
 printToFile(games, length, numberofhomegames, numberofawaygames, overallwin, overallloss,
	totalwinningpointdifference, totallosingpointdifference);	  // print the array
 return 0;
}


baskets.sun file (couldn't upload it because the .sun extension isn't allowed XD
10 13 H "LA_Clippers" 105 83
10 16 A "Seattle"	  99 93
10 19 A "San_Antonio" 104 96
10 20 H "Utah"		108 67
10 24 A "LA_Lakers"   103 111
10 26 H "Seattle"	 103 93
10 28 A "LA_Clippers" 109 88
10 29 A "Sacramento"  124 96
11  3 H "Atlanta"	 112 82
11  5 A "Philadelphia" 108 98
11  6 A "New_Jersey"  112 80
11  9 A "Chicago"	 94 74
11 10 A "Cleveland"   109 114
11 13 H "Sacramento"  111 113
11 16 A "Dallas"	  107 101
11 17 A "New_Orleans"  95 84
11 19 H "LA_Lakers"   107 102
11 21 A "LA_Clippers" 122 111
11 22 H "Chicago"	 100 82
11 24 H "Milwaukee"   111 104
11 26 H "New_Orleans"  95 86
11 30 A "Utah"		115 102
12  1 H "Cleveland"   120 101
12  3 H "Minnesota"	93 97
12  5 A "Portland"	121 99
12  7 H "Golden_State" 118 104
12  8 A "LA_Lakers"   113 110
12 10 H "Charlotte"   115 106
12 13 H "Orlando"	 121 100
12 15 H "Utah"		108 86
12 17 A "Seattle"	 112 110
12 18 H "Washington"  110 96
12 20 A "Denver"	  107 105
12 23 H "Memphis"	 109 102
12 26 H "Toronto"	 106 94
12 28 A "San_Antonio"  94 115
12 29 A "New_Orleans" 107 96
 1  2 H "Portland"	117 98
 1  4 A "Minnesota"   122 115
 1  5 A "Houston"	 108 98
 1  8 A "LA_Clippers" 108 91
 1  9 H "Indiana"	 124 89
 1 11 H "Miami"	   122 107
 1 12 A "Utah"		108 115
 1 14 A "Indiana"	  83 101
 1 15 A "Washington"  103 108
 1 17 A "Detroit"	  80 94
 1 19 H "Memphis"	  79 88
 1 21 H "San_Antonio" 123 128
 1 23 H "New_Jersey"  113 105
 1 25 A "New_York"	133 118
 1 26 A "Milwaukee"   121 115
 1 28 A "Boston"	  128 119


Here is my original C++ File:
#include <iostream> // keyboard and screen I/O
#include <fstream>  // file I/O ifsteam for input and ofsteam for output
#include <string>   // string class for C++ strings
#include <iomanip>  // formatted output support
using namespace std;	//uses standard packages


//Struct that holds the data of the Suns Record
struct SunsRecord
{   int	 month;
	int	 day;
	char	placeofgame;
	string  opponentsname;
	int	 sunsscore;
	int	 opposingscore;
	int	 pointdifference;
};								// note; at end of struct definition
//Function Prototypes
void OpenToRead(ifstream&); //ifstream = input file stream. Have to put & for file stream.
void Load(ifstream& gamefile, SunsRecord[], int& length, int& numberofhomegames, int& numberofawaygames, int& overallwin,
	int& overallloss, int& totalwinningpointdifference, int& totallosingpointdifference);
void Sort(SunsRecord games[], int length);
void printToFile(const SunsRecord[], int length, int numberofhomegames, int numberofawaygames, int overallwin,
	int overallloss, int totalwinningpointdifference, int totallosingpointdifference);

int main(void)
{SunsRecord games[53]; // array of sunsrecord records
 int length;					  // number of actual records in file
 //Have to declare these in main so that I can use them in printToFile and in Load.
 int numberofhomegames;
 int numberofawaygames;
 int overallwin;
 int overallloss;
 int totalwinningpointdifference;
 int totallosingpointdifference;

 ifstream gamefile;			// input file variable

 OpenToRead(gamefile);		 // get name and open file for input
 Load(gamefile, games, length, numberofhomegames, numberofawaygames, overallwin, overallloss,
	totalwinningpointdifference, totallosingpointdifference); // read file into array of structs
 Sort(games, length);		  // sort the array
 printToFile(games, length, numberofhomegames, numberofawaygames, overallwin, overallloss,
	totalwinningpointdifference, totallosingpointdifference);	  // print the array
 return 0;
}
//*******************************************************************
void OpenToRead(ifstream& gamefile)
// prompts user for file name and opens file, else quits
{string filename;			   // user-specified file name
 do{cout << "\n\n\tSuns Records";
	cout << "\n\n\tEnter the file name for Suns records: ";
	cin >> filename;		 // take in the file name
	gamefile.open(filename.c_str());
	if(!gamefile)
	   cout << "\tCan't open the file.  Please try again, or enter QUIT";
   }while(!gamefile && (filename != "QUIT"));
 if (filename == "QUIT")
	exit(1);					  // end program if no file
 return;
}
//*******************************************************************
void Load(ifstream& gamefile, SunsRecord games[], int& length, int& numberofhomegames, int& numberofawaygames,
	int& overallwin, int& overallloss, int& totalwinningpointdifference, int& totallosingpointdifference)
// read from the file into the array of records
{length = 0;
 numberofhomegames = 0;
 numberofawaygames = 0;
 overallwin = 0;
 overallloss = 0;
 totalwinningpointdifference = 0;
 totallosingpointdifference = 0;
 while(gamefile && length < 53)
	{gamefile >> games[length].month;
	 gamefile >> games[length].day;
	 gamefile >> games[length].placeofgame;
	 if(games[length].placeofgame == 'H')
	 {
		 numberofhomegames++;
	 }
	 else
	 {
		 numberofawaygames++;
	 }
	 gamefile >> games[length].opponentsname;
	 gamefile >> games[length].sunsscore;
	 gamefile >> games[length].opposingscore;
	 games[length].pointdifference = games[length].sunsscore - games[length].opposingscore;
	 if(games[length].sunsscore < games[length].opposingscore)
	 {
		 overallloss++;
		 totallosingpointdifference += games[length].pointdifference;

	 }
	 else if(games[length].sunsscore > games[length].opposingscore)
	 {
		 overallwin++;
		 totalwinningpointdifference += games[length].pointdifference;
	 }
	 else
	 {
		 overallwin += 0; //setting something for it to do if the scores are even
	 }
	 length++;
	}
 length--;
 return;
}
//*******************************************************************
void Sort(SunsRecord games[], int length)
// selection sort of the filled structs in the array by gpa
{int passcount;
 int place;
 int minindex;
 SunsRecord temp;			  // temp record for swapping

 for(passcount = 0; passcount < length - 1; passcount++)
	{minindex = passcount;		// start with initial min of first gpa
	 for(place = passcount + 1; place < length; place++)
	 if(games[place].opponentsname < games[minindex].opponentsname) // opponents name = sort key
																	//tricky spot because you can just compare them.
																	//don't have to use any outside function call to compare.
		minindex = place;
	 temp = games[minindex];
	 games[minindex] = games[passcount];
	 games[passcount] = temp;
	}
 return;
}
//*******************************************************************
void printToFile(const SunsRecord games[], int length, int numberofhomegames, int numberofawaygames, int overallwin,
	int overallloss, int totalwinningpointdifference, int totallosingpointdifference)
// prints the filled records in the array of student records
{fstream report;
 int count;
 double percentageofwins;
 int winningpointmargin;
 int losingpointmargin;

 percentageofwins = (double)overallwin / 53; //tricky spot have to typecast
 winningpointmargin = totalwinningpointdifference / overallwin;
 losingpointmargin = totallosingpointdifference / overallloss;

 string tempstring;
 cout << fixed << showpoint << setprecision(1);
 cout << "\n\tOverall Wins" << setw(4) << overallwin << "\n";
 cout << "\tOverall Losses" << setw(4) << overallloss << "\n";
 cout << "\tNumber of Home Games" << setw(4) << numberofhomegames << "\n";
 cout << "\tNumber of Away Games" << setw(4) << numberofawaygames << "\n";
 cout << "\tWinning Point Difference" << setw(4) << totalwinningpointdifference << "\n";
 cout << "\tLossing Point Difference" << setw(4) << totallosingpointdifference << "\n";
 cout << "\tPercentage of Wins " << setw(4) << percentageofwins << "\n";
 cout << "\tAverage Winning Point Margin" << setw(4) << winningpointmargin << "\n";
 cout << "\tAverage Losing Point Margin" << setw(4) << losingpointmargin << "\n";

 report.open("ShabanP3.dta");
 report << fixed << showpoint << setprecision(2);
 report << "\n\n\t\t\t\tSuns Basketball Record\n\n";
 report << "\n\tOpponents Name\t\tMonth\tPlace of Game\tPoint Difference\n";
 for(count = 0; count < length; count++)
	{tempstring = games[count].opponentsname + "			";
	 report << '\t' << tempstring.substr(0,15).data() << "	  ";
	 report << "\t" << setw(5) << games[count].month;
	 report << "\t\t" << setw(5) << games[count].placeofgame;
	 report << "\t\t" << setw(8)<< games[count].pointdifference<< endl;
	}
 report.close();
 cout << "\n\n\tYour report has been written to file ShabanP3.dta\n\t";
 cout << "\n\n\t";
 return;
}

This is my C file.
 // SunRecordP5.C Sorting a file into an array of Structs
// structs contain info on previous basketball games
// data file baskets.sun contains month it was played (integer), day of the month it was played (integer)
// H for a home game, A for an Away game (one character), opponent’s team name (a string of  up to 15 characters)
// the points scored by the Suns (integer), the points scored by the opponent team (integer)
#include <stdio.h>	 // bring in read/write abilities from standard library
#include <ctype.h>	// bring in toupper character function
#include <conio.h>	 // console input/output functions
#include <stdlib.h>


//Struct that holds the data of the Suns Record
struct SunsRecord
{   int	 month;
	int	 day;
	char	placeofgame;
	char	opponentsname[16];
	int	 sunsscore;
	int	 opposingscore;
	int	 pointdifference;
};								// note; at end of struct definition
//Function Prototypes
void OpenToRead(fscanf&);
void Load(fscanf& gamefile, SunsRecord[], int& length, int& numberofhomegames, int& numberofawaygames, int& overallwin,
	int& overallloss, int& totalwinningpointdifference, int& totallosingpointdifference);
void Sort(SunsRecord games[], int length);
void printToFile(const SunsRecord[], int length, int numberofhomegames, int numberofawaygames, int overallwin,
	int overallloss, int totalwinningpointdifference, int totallosingpointdifference);

int main(void)
{SunsRecord games[86]; // array of sunsrecord records 86 because the number of games in a season
 int length;					  // number of actual records in file
 //Have to declare these in main so that I can use them in printToFile and in Load.
 int numberofhomegames;
 int numberofawaygames;
 int overallwin;
 int overallloss;
 int totalwinningpointdifference;
 int totallosingpointdifference;

 fscanf gamefile;			// input file variable

 OpenToRead(gamefile);		 // get name and open file for input
 Load(gamefile, games, length, numberofhomegames, numberofawaygames, overallwin, overallloss,
	totalwinningpointdifference, totallosingpointdifference); // read file into array of structs
 Sort(games, length);		  // sort the array
 printToFile(games, length, numberofhomegames, numberofawaygames, overallwin, overallloss,
	totalwinningpointdifference, totallosingpointdifference);	  // print the array
 return 0;
}
//*******************************************************************
void OpenToRead(fscanf& gamefile)
// prompts user for file name and opens file, else quits
{string filename;			   // user-specified file name
 do{printf ("\n\n\tSuns Records");
	printf ("\n\n\tEnter the file name for Suns records: ");
	scanf ("%s", filename);		 // take in the file name
	gamefile.open(filename.c_str());
	if(!gamefile)
	   cout << "\tCan't open the file.  Please try again, or enter QUIT";
   }while(!gamefile && (filename != "QUIT"));
 if (filename == "QUIT")
	exit(1);					  // end program if no file
 return;
}
//*******************************************************************
void Load(fscanf& gamefile, SunsRecord games[], int& length, int& numberofhomegames, int& numberofawaygames,
	int& overallwin, int& overallloss, int& totalwinningpointdifference, int& totallosingpointdifference)
// read from the file into the array of records
{length = 0;
 numberofhomegames = 0;
 numberofawaygames = 0;
 overallwin = 0;
 overallloss = 0;
 totalwinningpointdifference = 0;
 totallosingpointdifference = 0;
 while(gamefile && length < 86)
	{fscanf("%d", games[length].month);
	 fscanf("%d", games[length].day);
	 fscanf("%c", games[length].placeofgame);
	 if(games[length].placeofgame == 'H')
	 {
		 numberofhomegames++;
	 }
	 else
	 {
		 numberofawaygames++;
	 }
	 gets(games[length].opponentsname);
	 fscanf("%d", games[length].sunsscore);
	 fscanf("%d", games[length].opposingscore);
	 games[length].pointdifference = games[length].sunsscore - games[length].opposingscore;
	 if(games[length].sunsscore < games[length].opposingscore)
	 {
		 overallloss++;
		 totallosingpointdifference += games[length].pointdifference;

	 }
	 else if(games[length].sunsscore > games[length].opposingscore)
	 {
		 overallwin++;
		 totalwinningpointdifference += games[length].pointdifference;
	 }
	 else
	 {
		 overallwin += 0; //setting something for it to do if the scores are even
	 }
	 length++;
	}
 length--;
 return;
}
//*******************************************************************
void Sort(SunsRecord games[], int length)
// selection sort of the filled structs in the array by gpa
{int passcount;
 int place;
 int minindex;
 SunsRecord temp;			  // temp record for swapping

 for(passcount = 0; passcount < length - 1; passcount++)
	{minindex = passcount;		// start with initial min of first gpa
	 for(place = passcount + 1; place < length; place++)
	 if(games[place].opponentsname < games[minindex].opponentsname) // opponents name = sort key
																	//tricky spot because you can just compare them.
																	//don't have to use any outside function call to compare.
		minindex = place;
	 temp = games[minindex];
	 games[minindex] = games[passcount];
	 games[passcount] = temp;
	}
 return;
}
//*******************************************************************
void printToFile(const SunsRecord games[], int length, int numberofhomegames, int numberofawaygames, int overallwin,
	int overallloss, int totalwinningpointdifference, int totallosingpointdifference)
// prints the filled records in the array of student records
{int count;
 float percentageofwins;
 int winningpointmargin;
 int losingpointmargin;

 percentageofwins = (float)overallwin / 53; //tricky spot have to typecast
 winningpointmargin = totalwinningpointdifference / overallwin;
 losingpointmargin = totallosingpointdifference / overallloss;

 char tempstring[16];
 printf("\n\tOverall Wins %-4d", overallwin, "\n");
 printf("\tOverall Losses %-4d",  overallloss, "\n");
 printf("\tNumber of Home Games %-4d",  numberofhomegames, "\n");
 printf("\tNumber of Away Games %-4d",  numberofawaygames, "\n");
 printf("\tWinning Point Differences %-4d",  totalwinningpointdifference, "\n");
 printf("\tLossing Point Differences %-4d",  totallosingpointdifference, "\n");
 printf("\tPercentage of Wins %-4f",  percentageofwins, "\n");
 printf("\Average Winning Pont Margin %-4d",  winningpointmargin, "\n");
 printf("\Average Losing Pont Margin %-4d",  losingpointmargin, "\n");


 FILE *file;
 file = fopen("ShabanP5.dta","a+"); /* apend file (add text to
 a file or create a file if it does not exist.*/
 fprintf(file,"%s", "\n\n\t\t\t\tSuns Basketball Record\n\n");
 fprintf(file,"%s", "\n\tOpponents Name\t\tMonth\tPlace of Game\tPoint Difference\n");
 for(count = 0; count < length; count++)
	{tempstring = games[count].opponentsname + "			";
	 fprintf(file, "%c", '\t', "%c", tempstring.data(), "%s", "	  ");
	 fprintf(file, "%s", "\t", "%-5d", games[count].month);
	 fprintf(file, "%s", "\t", "%-5c", games[count].placeofgame);
	 fprintf(file, "%s", "\t\t", "%-8d", games[count].pointdifference);
	}
 fclose(file); /*done!*/
 printf("\n\n\tYour report has been written to file ShabanP5.dta\n\t");
 printf("\n\n\t");
 return;
}


This post has been edited by xxwolfsrainxx: 30 September 2009 - 11:48 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Converting a C++ File into a C file

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Converting a C++ File into a C file

Posted 30 September 2009 - 12:21 PM

fscanf (as far as I know) is only a function.

You may be looking for a file pointer instead FILE *whatever_file
Was This Post Helpful? 0
  • +
  • -

#3 xxwolfsrainxx  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 93
  • Joined: 04-December 08

Re: Converting a C++ File into a C file

Posted 30 September 2009 - 12:28 PM

View Posterik.price, on 30 Sep, 2009 - 11:21 AM, said:

fscanf (as far as I know) is only a function.

You may be looking for a file pointer instead FILE *whatever_file

haha I see. My teacher wrote this in our instructions:
Rewrite your Suns basketball program into C. Your fstream and cin and cout used in the C++ version must be replaced with traditional C commands and functions such as fscanf, scanf, and printf. Even though we are using C++ compilers, I want you to learn traditional C language in this assignment and the next one. Name your program file with a .c extension after the name. So I assumed the fstream was the same as fscanf because of how she wrote it. Okay thanks Erik ^^>
Was This Post Helpful? 0
  • +
  • -

#4 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Converting a C++ File into a C file

Posted 30 September 2009 - 12:34 PM

No problem!

Good luck! :)
Was This Post Helpful? 0
  • +
  • -

#5 xxwolfsrainxx  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 93
  • Joined: 04-December 08

Re: Converting a C++ File into a C file

Posted 30 September 2009 - 12:46 PM

So I tried doing this but it won't allow me to do this. I can't think of a way to use a function to load the data from the file into an array.
#include <stdio.h>	 // bring in read/write abilities from standard library
#include <ctype.h>	// bring in toupper character function
#include <conio.h>	 // console input/output functions
#include <stdlib.h>


//Struct that holds the data of the Suns Record
struct SunsRecord
{   int	 month;
	int	 day;
	char	placeofgame;
	char	opponentsname[16];
	int	 sunsscore;
	int	 opposingscore;
	int	 pointdifference;
};								// note; at end of struct definition
//function Prototypes
void Load(FILE *gamefile, SunsRecord[], int& length, int& numberofhomegames, int& numberofawaygames, int& overallwin,
	int& overallloss, int& totalwinningpointdifference, int& totallosingpointdifference);
void Sort(SunsRecord games[], int length);
void printToFile(const SunsRecord[], int length, int numberofhomegames, int numberofawaygames, int overallwin,
	int overallloss, int totalwinningpointdifference, int totallosingpointdifference);

int main(void)
{SunsRecord games[86]; // array of sunsrecord records 86 because the number of games in a season
 int length;					  // number of actual records in file
 //Have to declare these in main so that I can use them in printToFile and in Load.
 int numberofhomegames, numberofawaygames, overallwin, overallloss, totalwinningpointdifference, totallosingpointdifference;
 printf ("\n\n\tSuns Records");
 FILE *gamefile;
 if ( ( gamefile = fopen( "baskets.sun", "r" ) ) == NULL )
 	printf( "File could not be opened\n" );
 else
 {
 	Load(gamefile, games, length, numberofhomegames, numberofawaygames, overallwin, overallloss,
		totalwinningpointdifference, totallosingpointdifference); // read file into array of structs
 }
 Sort(games, length);		  // sort the array
 printToFile(games, length, numberofhomegames, numberofawaygames, overallwin, overallloss,
	totalwinningpointdifference, totallosingpointdifference);	  // print the array
 return 0;
}
//*******************************************************************
/*void OpenToRead(FILE gamefile)
// prompts user for file name and opens file, else quits
{string filename;			   // user-specified file name
 do{printf ("\n\n\tSuns Records");
	printf ("\n\n\tEnter the file name for Suns records: ");
	scanf ("%s", filename);		 // take in the file name
	gamefile.open(filename.c_str());
	if(!gamefile)
	   cout << "\tCan't open the file.  Please try again, or enter QUIT";
   }while(!gamefile && (filename != "QUIT"));
 if (filename == "QUIT")
	exit(1);					  // end program if no file
 return;
}*/
//*******************************************************************
void Load(FILE gamefile, SunsRecord games[], int& length, int& numberofhomegames, int& numberofawaygames,
	int& overallwin, int& overallloss, int& totalwinningpointdifference, int& totallosingpointdifference)
// read from the file into the array of records
{length = 0;
 numberofhomegames = 0;
 numberofawaygames = 0;
 overallwin = 0;
 overallloss = 0;
 totalwinningpointdifference = 0;
 totallosingpointdifference = 0;
 while(gamefile && length < 86)
	{fscanf(gamefile, "%d", games[length].month);
	 fscanf(gamefile, "%d", games[length].day);
	 fscanf(gamefile, "%c", games[length].placeofgame);
	 if(games[length].placeofgame == 'H')
	 {
		 numberofhomegames++;
	 }
	 else
	 {
		 numberofawaygames++;
	 }
	 gets(games[length].opponentsname);
	 fscanf(gamefile, "%d", games[length].sunsscore);
	 fscanf(gamefile, "%d", games[length].opposingscore);
	 games[length].pointdifference = games[length].sunsscore - games[length].opposingscore;
	 if(games[length].sunsscore < games[length].opposingscore)
	 {
		 overallloss++;
		 totallosingpointdifference += games[length].pointdifference;

	 }
	 else if(games[length].sunsscore > games[length].opposingscore)
	 {
		 overallwin++;
		 totalwinningpointdifference += games[length].pointdifference;
	 }
	 else
	 {
		 overallwin += 0; //setting something for it to do if the scores are even
	 }
	 length++;
	}
 length--;
 return;
}
//*******************************************************************
void Sort(SunsRecord games[], int length)
// selection sort of the filled structs in the array by gpa
{int passcount;
 int place;
 int minindex;
 SunsRecord temp;			  // temp record for swapping

 for(passcount = 0; passcount < length - 1; passcount++)
	{minindex = passcount;		// start with initial min of first gpa
	 for(place = passcount + 1; place < length; place++)
	 if(games[place].opponentsname < games[minindex].opponentsname) // opponents name = sort key
																	//tricky spot because you can just compare them.
																	//don't have to use any outside function call to compare.
		minindex = place;
	 temp = games[minindex];
	 games[minindex] = games[passcount];
	 games[passcount] = temp;
	}
 return;
}
//*******************************************************************
void printToFile(const SunsRecord games[], int length, int numberofhomegames, int numberofawaygames, int overallwin,
	int overallloss, int totalwinningpointdifference, int totallosingpointdifference)
// prints the filled records in the array of student records
{int count;
 float percentageofwins;
 int winningpointmargin;
 int losingpointmargin;

 percentageofwins = (float)overallwin / length; //tricky spot have to typecast
 winningpointmargin = totalwinningpointdifference / overallwin;
 losingpointmargin = totallosingpointdifference / overallloss;

 char tempstring[16];
 printf("\n\tOverall Wins %-4d", overallwin, "\n");
 printf("\tOverall Losses %-4d",  overallloss, "\n");
 printf("\tNumber of Home Games %-4d",  numberofhomegames, "\n");
 printf("\tNumber of Away Games %-4d",  numberofawaygames, "\n");
 printf("\tWinning Point Differences %-4d",  totalwinningpointdifference, "\n");
 printf("\tLossing Point Differences %-4d",  totallosingpointdifference, "\n");
 printf("\tPercentage of Wins %-4f",  percentageofwins, "\n");
 printf("\Average Winning Pont Margin %-4d",  winningpointmargin, "\n");
 printf("\Average Losing Pont Margin %-4d",  losingpointmargin, "\n");


 FILE *file;
 file = fopen("ShabanP5.dta","a+"); /* apend file (add text to
 a file or create a file if it does not exist.*/
 fprintf(file,"%s", "\n\n\t\t\t\tSuns Basketball Record\n\n");
 fprintf(file,"%s", "\n\tOpponents Name\t\tMonth\tPlace of Game\tPoint Difference\n");
 for(count = 0; count < length; count++)
	{tempstring = games[count].opponentsname + "			";
	 fprintf(file, "%c", '\t', "%c", tempstring.data(), "%s", "	  ");
	 fprintf(file, "%s", "\t", "%-5d", games[count].month);
	 fprintf(file, "%s", "\t", "%-5c", games[count].placeofgame);
	 fprintf(file, "%s", "\t\t", "%-8d", games[count].pointdifference);
	}
 fclose(file); /*done!*/
 printf("\n\n\tYour report has been written to file ShabanP5.dta\n\t");
 printf("\n\n\t");
 return;
}



This post has been edited by xxwolfsrainxx: 30 September 2009 - 12:49 PM

Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,613
  • Joined: 23-August 08

Re: Converting a C++ File into a C file

Posted 30 September 2009 - 02:14 PM

Here's a push in the right direction.

This here:
void Load(FILE gamefile, SunsRecord games[], int& length, int& numberofhomegames, int& numberofawaygames,
    int& overallwin, int& overallloss, int& totalwinningpointdifference, int& totallosingpointdifference)


is not valid C. You can't pass-by-reference in this way through C, you need to use pointers. Here's what the function sig should look like:

void Load(FILE *gamefile, SunsRecord games[], int* length, int* numberofhomegames, int* numberofawaygames,
    int* overallwin, int* overallloss, int* totalwinningpointdifference, int* totallosingpointdifference)


When you call this function, all those int*s are called from code like this example:

#include <stdio.h>

void getInts(int *i1, int *i2)
{
    /* Change the value at the memory location
        pointed to by the address in i1 to be 42 */
    *i1 = 42;
 
    /* Likewise i2 */
    *i2 = 69;
}

int main(void)
{
    int zero = 0;
    int one = 1;

    printf("Before call: zero=%d, one=%d\n", zero, one);

    /* Pass the ADDRESSES of the variables to the function */
    getInts(&zero, &one);

    printf("After call: zero=%d, one=%d\n", zero, one);

    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#7 xxwolfsrainxx  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 93
  • Joined: 04-December 08

Re: Converting a C++ File into a C file

Posted 30 September 2009 - 02:16 PM

View PostJackOfAllTrades, on 30 Sep, 2009 - 01:14 PM, said:

Here's a push in the right direction.

This here:
void Load(FILE gamefile, SunsRecord games[], int& length, int& numberofhomegames, int& numberofawaygames,
    int& overallwin, int& overallloss, int& totalwinningpointdifference, int& totallosingpointdifference)


is not valid C. You can't pass-by-reference in this way through C, you need to use pointers. Here's what the function sig should look like:

void Load(FILE *gamefile, SunsRecord games[], int* length, int* numberofhomegames, int* numberofawaygames,
    int* overallwin, int* overallloss, int* totalwinningpointdifference, int* totallosingpointdifference)


When you call this function, all those int*s are called from code like this example:

#include <stdio.h>

void getInts(int *i1, int *i2)
{
    /* Change the value at the memory location
        pointed to by the address in i1 to be 42 */
    *i1 = 42;
 
    /* Likewise i2 */
    *i2 = 69;
}

int main(void)
{
    int zero = 0;
    int one = 1;

    printf("Before call: zero=%d, one=%d\n", zero, one);

    /* Pass the ADDRESSES of the variables to the function */
    getInts(&zero, &one);

    printf("After call: zero=%d, one=%d\n", zero, one);

    return 0;
}

Thanks man. Sorry. We learned C++ first. It's for a class were we learn Prolog, Lisp, C++ and C.

What does the error: Type name expected its for this function
void Load(FILE *gamefile, SunsRecord games[], int* length, int* numberofhomegames, int* numberofawaygames,
int* overallwin, int* overallloss, int* totalwinningpointdifference, int* totallosingpointdifference)

This post has been edited by xxwolfsrainxx: 30 September 2009 - 02:30 PM

Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5907
  • View blog
  • Posts: 12,813
  • Joined: 16-October 07

Re: Converting a C++ File into a C file

Posted 30 September 2009 - 05:50 PM

I'd start with something like this:

// In C, as opposed to C++, struct must always be referenced with the type
// e.g. struct SunsRecord foo[42];
// one way around that is typedef

struct SunsRecordStruct { 
	int	 month;
	int	 day;
	char	placeofgame;
	char	opponentsname[16];
	int	 sunsscore;
	int	 opposingscore;
	int	 pointdifference;
};

typedef struct SunsRecordStruct SunsRecord;
// now you can use the raw type name SunsRecord


// the number of variables you're passing is seriously busy, time for another struct

// magic numbers suck in both C++ and C
// don't use any:
#define GAMES_MAX 86

// another way of using typedef
typedef struct {
	SunsRecord games[GAMES_MAX];
	int length;
	int numberofhomegames, numberofawaygames;
	int overallwin, overallloss;
	int totalwinningpointdifference, totallosingpointdifference;
} GameInfo;

// return NULL on fail
FILE *OpenToRead();

// load the file, return a loaded GameInfo
GameInfo Load(FILE *fp);

// now pass GameInfo around
void Sort(GameInfo *gameInfo);
void printToFile(GameInfo *gameInfo);


int main(void) {
	FILE *fp;
	
	fp = OpenToRead();
	if (fp != NULL) {
		GameInfo gameInfo = Load(fp);
		Sort(&gameInfo);
		printToFile(&gameInfo);
		close(fp);
	}
	return 0;
}



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#9 xxwolfsrainxx  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 93
  • Joined: 04-December 08

Re: Converting a C++ File into a C file

Posted 30 September 2009 - 11:05 PM

Okay started it from scratch. Now I have a problem where the reading of the file takes in another line, no idea what line but here's the new code. I'll explain it better, so, there are 53 games in the baskets.sun file. it's supposed to have 42 wins and 11 losses. When you remove line 92 (length = length - 1;) it shows the other line that's read from the file. Here's the file
10 13 H "LA_Clippers" 105 83
10 16 A "Seattle"	  99 93
10 19 A "San_Antonio" 104 96
10 20 H "Utah"		108 67
10 24 A "LA_Lakers"   103 111
10 26 H "Seattle"	 103 93
10 28 A "LA_Clippers" 109 88
10 29 A "Sacramento"  124 96
11  3 H "Atlanta"	 112 82
11  5 A "Philadelphia" 108 98
11  6 A "New_Jersey"  112 80
11  9 A "Chicago"	 94 74
11 10 A "Cleveland"   109 114
11 13 H "Sacramento"  111 113
11 16 A "Dallas"	  107 101
11 17 A "New_Orleans"  95 84
11 19 H "LA_Lakers"   107 102
11 21 A "LA_Clippers" 122 111
11 22 H "Chicago"	 100 82
11 24 H "Milwaukee"   111 104
11 26 H "New_Orleans"  95 86
11 30 A "Utah"		115 102
12  1 H "Cleveland"   120 101
12  3 H "Minnesota"	93 97
12  5 A "Portland"	121 99
12  7 H "Golden_State" 118 104
12  8 A "LA_Lakers"   113 110
12 10 H "Charlotte"   115 106
12 13 H "Orlando"	 121 100
12 15 H "Utah"		108 86
12 17 A "Seattle"	 112 110
12 18 H "Washington"  110 96
12 20 A "Denver"	  107 105
12 23 H "Memphis"	 109 102
12 26 H "Toronto"	 106 94
12 28 A "San_Antonio"  94 115
12 29 A "New_Orleans" 107 96
 1  2 H "Portland"	117 98
 1  4 A "Minnesota"   122 115
 1  5 A "Houston"	 108 98
 1  8 A "LA_Clippers" 108 91
 1  9 H "Indiana"	 124 89
 1 11 H "Miami"	   122 107
 1 12 A "Utah"		108 115
 1 14 A "Indiana"	  83 101
 1 15 A "Washington"  103 108
 1 17 A "Detroit"	  80 94
 1 19 H "Memphis"	  79 88
 1 21 H "San_Antonio" 123 128
 1 23 H "New_Jersey"  113 105
 1 25 A "New_York"	133 118
 1 26 A "Milwaukee"   121 115
 1 28 A "Boston"	  128 119


C Code
#include <stdio.h>	 // bring in read/write abilities from standard library
#include <ctype.h>	// bring in toupper character function
#include <conio.h>	 // console input/output functions
#include <stdlib.h>


//Struct that holds the data of the Suns Record
struct SunsRecord
{   int	 month;
	int	 day;
	char	placeofgame;
	char	opponentsname[16];
	int	 sunsscore;
	int	 opposingscore;
	int	 pointdifference;
};								// note; at end of struct definition

typedef struct SunsRecord sunRec;

//Have to declare these in main so that I can use them in printToFile and in Load.
int numberofhomegames = 0;
int numberofawaygames = 0;
int overallwin = 0;
int overallloss = 0;
int totalwinningpointdifference = 0;
int totallosingpointdifference = 0;
//function Prototypes
int Load(sunRec[]);
void Sort(sunRec[], int);
void Printout(const sunRec[], int);
void printToFile(const sunRec[], int length);

int main(void)
{
sunRec gameStruct[86]; // array of sunsrecord records 86 because the number of games in a season
int length = 0;					  // number of actual records in file
printf ("\n\n\tSuns Records");

//printf("\tfkdlsf");
length = Load(gameStruct); // read file into array of structs
//printf("\n\tblah");
//Sort(gameStruct, length);		  // sort the array
Printout(gameStruct, length);
printToFile(gameStruct, length);	  // print the array
return 0;
}

//*******************************************************************
// read from the file into the array of records
//*******************************************************************
int Load(sunRec gameStruct[])
// read from the file into the array of records
{
	FILE *gamefile;
	int length = 0;
	if ( ( gamefile = fopen( "baskets.sun", "r" ) ) == NULL )
		{printf( "File could not be opened\n" );
		 return(length);
		}
	else {
		while ( !feof( gamefile ) )
		{
			fscanf( gamefile, "%d %d %c %s %d %d", &gameStruct[length].month,
				&gameStruct[length].day, &gameStruct[length].placeofgame, gameStruct[length].opponentsname, &gameStruct[length].sunsscore, &gameStruct[length].opposingscore);
		 gameStruct[length].pointdifference = gameStruct[length].sunsscore - gameStruct[length].opposingscore;
		 if(gameStruct[length].sunsscore > gameStruct[length].opposingscore)
		 {
			 overallwin++;
			 totalwinningpointdifference += gameStruct[length].pointdifference;
		 }
		 else if(gameStruct[length].sunsscore < gameStruct[length].opposingscore)
		 {
			 overallloss++;
			 totallosingpointdifference += gameStruct[length].pointdifference;

		 }
		 if(gameStruct[length].placeofgame == 'H')
		   {
			 numberofhomegames++;
		   }
		   else if(gameStruct[length].placeofgame == 'A')
		   {
			   numberofawaygames++;
		   }



			  length++;
		}
		fclose( gamefile );
	}
	length = length - 1;		//adjust back one for overshooting before stopping

 return(length);
}
//************************************************
/*void Sort(sunRec gameStruct[], int length)
// selection sort of the filled structs in the array by gpa
{int passcount;
 int place;
 int minindex;
 sunRec temp;			  // temp record for swapping

  for(passcount = 0; passcount < length - 1; passcount++)
	 {minindex = passcount;		// start with initial min of first gpa
	 for(place = passcount + 1; place < length; place++)
	 if(gameStruct[place].opponentsname < gameStruct[minindex].opponentsname) // gpa = sort key
		minindex = place;
	 temp = gameStruct[minindex];
	 gameStruct[minindex] = gameStruct[passcount];
	 gameStruct[passcount] = temp;
	}
 return;
}*/
//****************************************************************
void Printout(const sunRec gamesStruct[], int length)
// prints the filled records in the array of student records
{int count;
 for(count = 0; count < length; count++)
	{printf("\n\t%-4d", gamesStruct[count].month);
	 printf("\t%-4d", gamesStruct[count].day);
	 printf("\t%-1c", gamesStruct[count].placeofgame);
	 printf("\t%-15s", gamesStruct[count].opponentsname);
	 printf("\t%-4d", gamesStruct[count].sunsscore);
	 printf("\t%-4d", gamesStruct[count].opposingscore);
	 printf("\t%-1d", gamesStruct[count].pointdifference);
	 }
 printf("\n\n\t%-5d", length);
 return;
}
//*******************************************************************
void printToFile(const sunRec gamesStruct[], int length)
// prints the filled records in the array of student records
{int count;
float percentageofwins;
int winningpointmargin;
int losingpointmargin;

percentageofwins = (float)overallwin / length; //tricky spot have to typecast
winningpointmargin = totalwinningpointdifference / overallwin;
losingpointmargin = totallosingpointdifference / overallloss;

printf("\n\tOverall Wins %-4d", overallwin, "\n");
printf("\n\tOverall Losses %-4d",  overallloss, "\n");
printf("\n\tNumber of Home Games %-4d",  numberofhomegames, "\n");
printf("\n\tNumber of Away Games %-4d",  numberofawaygames, "\n");
printf("\n\tWinning Point Differences %-4d",  totalwinningpointdifference, "\n");
printf("\n\tLossing Point Differences %-4d",  totallosingpointdifference, "\n");
printf("\n\tPercentage of Wins %-4f",  percentageofwins, "\n");
printf("\n\tAverage Winning Pont Margin %-4d",  winningpointmargin, "\n");
printf("\n\tAverage Losing Pont Margin %-4d",  losingpointmargin, "\n");


//FILE *file;
//file = fopen("ShabanP5.dta","a+"); /* apend file (add text to
//a file or create a file if it does not exist.*/
/*fprintf(file,"%s", "\n\n\t\t\t\tSuns Basketball Record\n\n");
fprintf(file,"%s", "\n\tOpponents Name\t\tMonth\tPlace of Game\tPoint Difference\n");
for(count = 0; count < length; count++)
	{tempstring = games[count].opponentsname + "			";
	 fprintf(file, "%c", '\t', "%c", tempstring.data(), "%s", "	  ");
	 fprintf(file, "%s", "\t", "%-5d", games[count].month);
	 fprintf(file, "%s", "\t", "%-5c", games[count].placeofgame);
	 fprintf(file, "%s", "\t\t", "%-8d", games[count].pointdifference);
	}
fclose(file); /*done!*/
/*printf("\n\n\tYour report has been written to file ShabanP5.dta\n\t");
printf("\n\n\t");*/
return;
}


Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,613
  • Joined: 23-August 08

Re: Converting a C++ File into a C file

Posted 01 October 2009 - 05:04 AM

I believe your answer is here: Why it's bad to use feof() to control a loop.
Was This Post Helpful? 1
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5907
  • View blog
  • Posts: 12,813
  • Joined: 16-October 07

Re: Converting a C++ File into a C file

Posted 01 October 2009 - 05:26 AM

View Postxxwolfsrainxx, on 1 Oct, 2009 - 12:05 AM, said:

Now I have a problem where the reading of the file takes in another line, no idea what line but here's the new code.


Short answer: put a space at the end of your fscanf, you need to finish off the white space.

Long answer: Nothing personal, your code was driving me nuts. The line feeds at the beginning of the printf were particularly nasty. And then globals?!?

Anyway, I "fixed" it. ;)

#include <stdlib.h>
#include <stdio.h>
//#include <ctype.h>
// BAD never BAD #include <conio.h>

typedef struct {
	int	 month;
	int	 day;
	char	placeofgame;
	char	opponentsname[16];
	int	 sunsscore;
	int	 opposingscore;
	int	 pointdifference;
} SunsRecord;

// you lost your case?
// types, upper, variables lower
// typedef struct SunsRecord sunRec;

// Have to declare these in main so that I can use them in printToFile and in Load.
// NOoooo! Globals, bad.
/*
int numberofhomegames = 0;
int numberofawaygames = 0;
int overallwin = 0;
int overallloss = 0;
int totalwinningpointdifference = 0;
int totallosingpointdifference = 0;
*/

#define MAX_GAME_COUNT 86

typedef struct {
	int gameCount;
	int numberofhomegames; // don't need this, it's boolean numberofawaygames;
	int overallwin; // don't need this, it's boolean overallloss;
	int totalwinningpointdifference, totallosingpointdifference;
	SunsRecord games[MAX_GAME_COUNT];
} GameInfo;



GameInfo Load(const char *fileName) {
	GameInfo gameInfo = {0,0,0,0,0};
	FILE *gamefile;
	int i;
	
	if ( ( gamefile = fopen( fileName, "r" ) ) == NULL ){
		printf( "File could not be opened\n" );
		return gameInfo;
	}
	
	while ( !feof( gamefile ) ) {
		// the rec thing is just to get a short hand on our current, also handly elsewhere
		SunsRecord *rec = &(gameInfo.games[gameInfo.gameCount++]);
		
		fscanf( gamefile, "%d %d ", &rec->month, &rec->day);
		fscanf( gamefile, "%c %s ", &rec->placeofgame, rec->opponentsname);
		fscanf( gamefile, "%d %d ", &rec->sunsscore, &rec->opposingscore);
	}
	fclose(gamefile);
	
	for(i=0; i<gameInfo.gameCount; i++) {
		SunsRecord *rec = &(gameInfo.games[i]);
		rec->pointdifference = rec->sunsscore - rec->opposingscore;

		if(rec->sunsscore > rec->opposingscore) {
			gameInfo.overallwin++;
			gameInfo.totalwinningpointdifference += rec->pointdifference;
		 } else {
			gameInfo.totallosingpointdifference += rec->pointdifference;
		 }
		 
		 if(rec->placeofgame == 'H') { gameInfo.numberofhomegames++; }
	}
	//gameInfo.gameCount--;
	return gameInfo;
}


void printGames(const SunsRecord *games, int length) {
	SunsRecord *rec = (SunsRecord *)games;
	while(length-- > 0) {
		printf("\t%-4d", rec->month);
		printf("\t%-4d", rec->day);
		printf("\t%-1c", rec->placeofgame);
		printf("\t%-15s", rec->opponentsname);
		printf("\t%-4d", rec->sunsscore);
		printf("\t%-4d", rec->opposingscore);
		printf("\t%-1d\n", rec->pointdifference);
		rec++;
	}
}

void printStats(const GameInfo *gameInfo){
	printf("\tTotal Games %-4d\n", gameInfo->gameCount);
	printf("\tOverall Wins %-4d\n", gameInfo->overallwin);
	printf("\tOverall Losses %-4d\n",  gameInfo->gameCount - gameInfo->overallwin);
	printf("\tNumber of Home Games %-4d\n",  gameInfo->numberofhomegames);
	printf("\tNumber of Away Games %-4d\n",  gameInfo->gameCount - gameInfo->numberofhomegames);
	printf("\tWinning Point Differences %-4d\n",  gameInfo->totalwinningpointdifference);
	printf("\tLossing Point Differences %-4d\n",  gameInfo->totallosingpointdifference);
	printf("\tPercentage of Wins %-4f\n",  (float)gameInfo->overallwin / gameInfo->gameCount);
	printf("\tAverage Winning Pont Margin %-4d\n",  gameInfo->totalwinningpointdifference / gameInfo->overallwin);
	printf("\tAverage Losing Pont Margin %-4d\n",  gameInfo->totallosingpointdifference / (gameInfo->gameCount - gameInfo->overallwin));
	printGames(gameInfo->games, gameInfo->gameCount);
}

int main(void) {
	GameInfo gameInfo = {0,0,0,0,0,0};
	
	printf ("\tSuns Records\n");
	gameInfo = Load("baskets.sun");
	if (gameInfo.gameCount>0) {
		printStats(&gameInfo);
	}
	return 0;
}



Hope this helps.

This post has been edited by baavgai: 01 October 2009 - 05:27 AM

Was This Post Helpful? 1
  • +
  • -

#12 xxwolfsrainxx  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 93
  • Joined: 04-December 08

Re: Converting a C++ File into a C file

Posted 01 October 2009 - 07:26 AM

Are the use of global variables bad programming?

This post has been edited by xxwolfsrainxx: 01 October 2009 - 07:47 AM

Was This Post Helpful? 0
  • +
  • -

#13 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5907
  • View blog
  • Posts: 12,813
  • Joined: 16-October 07

Re: Converting a C++ File into a C file

Posted 01 October 2009 - 07:37 AM

View Postxxwolfsrainxx, on 1 Oct, 2009 - 08:26 AM, said:

Are global variables bad other than the reason they can be accessed by everything XD?


No, that's about it.

Reading isn't that bad; global constants are generally fine. It's the writing by everything that's the killer. If any part of your program can change a value the entire program uses at any time, finding where is screws up is challenging.

Limiting the scope of variables as much as possible is an accepted best practice. You'll notice I even limit variable declaration to inside blocks. Same idea; don't have variables just floating around when you're done with them. It also forces a more modular approach.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1