Results 1 to 3 of 3

Thread: Arrays, String comparison and copy, and I/O in Procedural C++

  1. #1
    Join Date
    Dec 2011
    Location
    -bash
    Posts
    515
    Mentioned
    0 Post(s)
    Quoted
    27 Post(s)

    Default Arrays, String comparison and copy, and I/O in Procedural C++

    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:
    C++ 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!

  2. #2
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    That's because you never looked at the return value of strcmp. Pay a bit more attention to detail. It returns a value of 0 if the strings are completely equal, character for character. So if you want to implicitly convert this to a bool, 0 = false, 1 = true then you must use the logical Not operator (!).

    !0 = true. !1 = false. It negates them. So for strcmpy you'd do if (!strcmp) which means they are equal.

    C++ Code:
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>

    using namespace std;

    void Readwrite()
    {
        FILE *infile;
        FILE *outfile;

        int year = 0;
        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 += (2012 - year);
                }
                if (!strcmp(country, "Canada"))
                {
                    ++CCanada;
                    Canave_age += (2012-year);
                }
                if (!strcmp(country, "Russia"))
                {
                    ++CRUSS;
                    RUave_age += (2012-year);
                }
                if (!strcmp(country, "China"))
                {
                    ++CChina;
                    Chinave_age += (2012-year);
                }
                if (!strcmp(country, "Italy"))
                {
                    ++CItaly;
                    Itaave_age += (2012-year);
                }
                if (!strcmp(country, "France"))
                {
                    ++CFrance;
                    FRave_age += (2012-year);
                }
                if (!strcmp(country, "Cuba"))
                {
                    ++CCUBA;
                    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;
    }
    I am Ggzz..
    Hackintosher

  3. #3
    Join Date
    Dec 2011
    Location
    -bash
    Posts
    515
    Mentioned
    0 Post(s)
    Quoted
    27 Post(s)

    Default

    Lol yea realised my mistake and already fixed it, but thanks for the input

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •