Thread: Help with for-loop (FILE INPUT/OUTPUT)

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    51

    Help with for-loop (FILE INPUT/OUTPUT)

    The input file is just 10 random uppercased/lowercased names. I already converted all of them to uppercase and now i am trying to loop through the uppercased names and see how many times each one occurs. That is what I am trying to achieve with the
    Code:
    a = "BOB"
    , etc. However I am getting a warning at all the
    Code:
    a = " "
    :
    Code:
    Warning: Comparison between pointer and integer
    Can you guys give me a hand and point me in the right direction as to how I can increment the name counter so the output goes like:
    Code:
    BOB 3BAB 1
    ALEX 1
    ALEXIS 1
    MARY 3
    FRED 1

    Here is the code that I have so far:

    Code:
    # include <stdio.h># include <string.h>
    # include <ctype.h>
    
    
    // To uppercase function.
    void to_upper(char *names);
    
    
    int main()
    {
        // initializing the variables.
        int BOB_count, BAB_count, ALEX_count, ALEXIS_count, MARY_count, FRED_count, i, j;
        char names[10], a;
        const int MAX_NAME_LENGTH = 10;
    
    
        // Opening the file.
        FILE *ifp = fopen("input.txt","r");
    
    
        // Checking if the file opens.
        if(!ifp)
        {
            printf("Unable to open file.\n");
            return -1;
        }
    
    
    
    
        for (i = 0; i<=MAX_NAME_LENGTH; i++)
        {
            // Reads the file and stores the given names to names
            fscanf(ifp, "%s", names);
            to_upper(names);
    
    
            // A is a temp variable to check if name[i] is equal to the given name, and if it is equal, increment the counter.
            a = names[i];
    
    
            if (a == "BOB")
            {
                BOB_count++;
            }
    
    
            if (a == "BAB")
            {
                BAB_count++;
            }
    
    
            if (a == "ALEX")
            {
                ALEX_count++;
            }
    
    
            if (a == "ALEXIS")
            {
                ALEXIS_count++;
            }
    
    
            if (a == "MARY")
            {
                MARY_count++;
            }
    
    
            if (a == "FRED")
            {
                FRED_count++;
            }
    
    
        }
    
    
        if (BOB_count != 0)
        {
            printf("BOB   %d\n", BOB_count);
        }
    
    
        if (BAB_count != 0)
        {
            printf("BAB   %d\n", BAB_count);
        }
    
    
        if (ALEX_count != 0)
        {
            printf("ALEX   %d\n", ALEX_count);
        }
    
    
        if (ALEXIS_count != 0)
        {
            printf("ALEXIS   %d\n", ALEXIS_count);
        }
    
    
        if (MARY_count != 0)
        {
            printf("MARY   %d\n", MARY_count);
        }
    
    
        if (FRED_count != 0)
        {
            printf("FRED   %d\n", FRED_count);
        }
    
    
        // Closes the file.
        fclose(ifp);
    
    
        return 0;
    
    
    }
    // Converts the file content to uppercase
    void to_upper(char *names)
    {
        int i = 0;
    
    
        while (names[i] != '\0')
        {
            names[i] = toupper(names[i]);
            i++;
        }
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Code:
    a = names[i];
    - 'a' is a single character, and cannot hold a string.
    - "names[i]" is only a single character within that string

    Therefore, this is not an effective way to assign strings.

    Furthermore, strings in 'C' cannot be assigned with the '=' (if you had made 'a' a character array, for example).

    ---

    My advice would be to ditch the variable 'a', and replace all of these:

    Code:
    if (a == "BOB")
    ... with a comparison between "names" and the string literal (i.e. "BOB").

    For this, you would want to use "strcmp()". Pay careful attention to the expected return value of this function if the two strings are the same.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    I changed the for loop to this, but no luck for a reason unknown to me.

    Code:
    for (i = 0; i<=MAX_NAME_LENGTH; i++)
        {
            // Reads the file and stores the given names to names
            fscanf(ifp, "%s", names);
            to_upper(names);
    
    
            if (strcmp(names, "BOB") == 0)
            {
                BOB_count++;
            }
    
    
            if (strcmp(names, "BAB") == 0)
            {
                BAB_count++;
            }
    
    
            if (strcmp(names, "ALEX") == 0)
            {
                ALEX_count++;
            }
    
    
            if (strcmp(names, "ALEXIS") == 0)
            {
                ALEXIS_count++;
            }
    
    
            if (strcmp(names, "MARY") == 0)
            {
                MARY_count++;
            }
    
    
            if (strcmp(names, "FRED") == 0)
            {
                FRED_count++;
            }
    
    
        }

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    You'll have to be more explicit when explaining your problem - "no luck" tells us nothing. What are you expecting? What are you getting? Post the contents of your test file ("input.txt") so we have something to work with to replicate the issue you are seeing.

    One thing I noticed - your "count" variables are not initialized in the program from post #1. Therefore, if the program compiles and runs, you're going to get garbage values in those variables.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    FILE:
    Code:
    10
    Bob
    BOB
    bab
    BoB
    AleX
    Alexis
    Mary
    MARY
    MaRY
    Fred
    Output should be:
    Code:
    BOB   3
    BAB   1
    ALEX  1
    ALEXIS   1
    MARY 3
    FRED  1

    can you iterate on the initializer please?

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    NVM on the initializing..

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    Problem solved!!! MAN I LOVE coding. The best feeling in the world right now. Thanks so much for your help Matticus!!

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    When you declare your variables, they just contain whatever values happened to be in memory before your program ran. For the purposes of this conversation, they're completely random and unpredictable. You need to "initialize" them by assigning them a sensible value before you begin your compution - typically this is 0 (and in your case, since you're just going to increment the value when you encounter a number - you all want to initialize them to 0). So what results are you getting now? As said above, you need to ask a more specific question - we're not going to download your code, try run it, and assume we're seeing the same results as you.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    You're very welcome. I'm glad to see that you're excited about your accomplishments! Keep up the good work, and happy coding!

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    Hi friends, I talked to the professor today and i didn't do it right because the code has to work for any random names that are contained within the input file. I am wondering if you guys had an idea how I can achieve the code working on any names. Obviously, I don't expect you to write the code for me, but maybe you can let me know the steps i might need to take. Thanks for all your help so far.

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You should use something like a map: A data structure that maps names to a count of the number of times that name has occurred, and the ability to add new names and increment existing counts. You could do this very simply by maintaining an array of names and an array of integers. You would need a function to add names to your array (and reallocate it if necessary), search your array, look up the count for a given name, etc. Real world programs use slightly more sophisticated data structures like hash maps and binary trees for this purpose, and there are libraries available that implement these data structures (if you're allowed to use them in your assignment).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ File Input Output VS C file Input Output
    By forumuser in forum C++ Programming
    Replies: 1
    Last Post: 09-30-2009, 06:46 AM
  2. File Input/output
    By JJFMJR in forum C++ Programming
    Replies: 4
    Last Post: 07-09-2007, 07:50 AM
  3. Need help : eof, loop, output file
    By henz321 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2005, 06:54 PM
  4. file input/output
    By Bigbio2002 in forum C Programming
    Replies: 3
    Last Post: 10-29-2003, 01:45 AM
  5. File Input / Output Help?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-18-2002, 10:20 AM