Recursive
07-08-2012, 10:05 PM
What does it do? It will read from an input file I attached below, calculate average age and number of players then the output will be in this format:
Country | Num of athletes | Aver age |
----------------------------------------------
| | |
| | |
| | |
| | |
| | |
| | |
| | |
And transfered to the output file
Here is the code:
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
void Readwrite()
{
FILE *infile;
FILE *outfile;
int year;
int CUSA = 0, USave_age = 0;
int CRUSS = 0, RUave_age = 0;
int CCUBA = 0, CUBave_age = 0;
int CChina = 0, Chinave_age = 0;
int CFrance = 0, FRave_age = 0;
int CCanada = 0, Canave_age = 0;
int CItaly = 0, Itaave_age = 0;
char firstline[50] = {"Country | Num of athletes | Aver age |"};
char secondline[50] = {"------------------------------------------"};
char country[30];
char firstname[30];
char lastname[30];
outfile = fopen("C:/Users/Minenotyours/Desktop/a1part2-output.txt", "w");//the filename will be created in this process and placed in the location
//(C:/Users/Chig/Desktop/) and called a1part2-output.txt".
//the w means we will be writing into this file after it is created
infile = fopen("C:/Users/Chig/Desktop/a1part2-input.txt", "r");//the filename is already declared "C:/Users/Minenotyours/Desktop/a1part2-input.txt".
//The ’r’ means we will read file.
if (infile == NULL||outfile == NULL)
{
perror("File failed to open");//error such as file does not exist in directory or cannot be created
}
else
{
do
{
fscanf(infile, "%s %s %s %i", firstname, lastname, country, &year);
if (strcmp(country, "USA"))
{
CUSA++;
USave_age = (USave_age + (2012-year));
}
if (strcmp(country, "Canada"))
{
CCanada++;
Canave_age = (Canave_age + (2012-year));
}
if (strcmp(country, "Russia"))
{
CRUSS++;
RUave_age = (RUave_age + (2012-year));
}
if (strcmp(country, "China"))
{
CChina++;
Chinave_age = (Chinave_age + (2012-year));
}
if (strcmp(country, "Italy"))
{
CItaly++;
Itaave_age = (Itaave_age + (2012-year));
}
if (strcmp(country, "France"))
{
CFrance++;
FRave_age = (FRave_age + (2012-year));
}
if (strcmp(country, "Cuba"))
{
CCUBA++;
CUBave_age = (CUBave_age + (2012-year));
}
printf("%s %s %s %i\n", firstname, lastname, country, year);//prints to console.
}
while(!feof(infile));
fprintf(outfile, "%s\n%s\n", firstline, secondline);//this will print the first 2 lines we need
fprintf(outfile, "USA |%i |%i |\n", CUSA, (USave_age/CUSA));
fprintf(outfile, "Russia |%i |%i |\n", CRUSS, (RUave_age/CRUSS));
fprintf(outfile, "Cuba |%i |%i |\n", CCUBA, (CUBave_age/CCUBA));
fclose(infile);//closes the file we read from
fclose(outfile);//closes the file we just created and wrote in
}
}
int main(void)
{
Readwrite();
return EXIT_SUCCESS;
}
Problem is: Whenever I run it, I get this(note not finished yet):
Country | Num of athletes | Aver age |
----------------------------------------------
USA |12 |18 |
Russia |21 |21 |
Cuba |23 |20 |
The number of athlethes is wrong and the average age is wrong too!
Country | Num of athletes | Aver age |
----------------------------------------------
| | |
| | |
| | |
| | |
| | |
| | |
| | |
And transfered to the output file
Here is the code:
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
void Readwrite()
{
FILE *infile;
FILE *outfile;
int year;
int CUSA = 0, USave_age = 0;
int CRUSS = 0, RUave_age = 0;
int CCUBA = 0, CUBave_age = 0;
int CChina = 0, Chinave_age = 0;
int CFrance = 0, FRave_age = 0;
int CCanada = 0, Canave_age = 0;
int CItaly = 0, Itaave_age = 0;
char firstline[50] = {"Country | Num of athletes | Aver age |"};
char secondline[50] = {"------------------------------------------"};
char country[30];
char firstname[30];
char lastname[30];
outfile = fopen("C:/Users/Minenotyours/Desktop/a1part2-output.txt", "w");//the filename will be created in this process and placed in the location
//(C:/Users/Chig/Desktop/) and called a1part2-output.txt".
//the w means we will be writing into this file after it is created
infile = fopen("C:/Users/Chig/Desktop/a1part2-input.txt", "r");//the filename is already declared "C:/Users/Minenotyours/Desktop/a1part2-input.txt".
//The ’r’ means we will read file.
if (infile == NULL||outfile == NULL)
{
perror("File failed to open");//error such as file does not exist in directory or cannot be created
}
else
{
do
{
fscanf(infile, "%s %s %s %i", firstname, lastname, country, &year);
if (strcmp(country, "USA"))
{
CUSA++;
USave_age = (USave_age + (2012-year));
}
if (strcmp(country, "Canada"))
{
CCanada++;
Canave_age = (Canave_age + (2012-year));
}
if (strcmp(country, "Russia"))
{
CRUSS++;
RUave_age = (RUave_age + (2012-year));
}
if (strcmp(country, "China"))
{
CChina++;
Chinave_age = (Chinave_age + (2012-year));
}
if (strcmp(country, "Italy"))
{
CItaly++;
Itaave_age = (Itaave_age + (2012-year));
}
if (strcmp(country, "France"))
{
CFrance++;
FRave_age = (FRave_age + (2012-year));
}
if (strcmp(country, "Cuba"))
{
CCUBA++;
CUBave_age = (CUBave_age + (2012-year));
}
printf("%s %s %s %i\n", firstname, lastname, country, year);//prints to console.
}
while(!feof(infile));
fprintf(outfile, "%s\n%s\n", firstline, secondline);//this will print the first 2 lines we need
fprintf(outfile, "USA |%i |%i |\n", CUSA, (USave_age/CUSA));
fprintf(outfile, "Russia |%i |%i |\n", CRUSS, (RUave_age/CRUSS));
fprintf(outfile, "Cuba |%i |%i |\n", CCUBA, (CUBave_age/CCUBA));
fclose(infile);//closes the file we read from
fclose(outfile);//closes the file we just created and wrote in
}
}
int main(void)
{
Readwrite();
return EXIT_SUCCESS;
}
Problem is: Whenever I run it, I get this(note not finished yet):
Country | Num of athletes | Aver age |
----------------------------------------------
USA |12 |18 |
Russia |21 |21 |
Cuba |23 |20 |
The number of athlethes is wrong and the average age is wrong too!