Thread: help with school project, please

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    9

    help with school project, please

    Im doing a school project and need help please. So from a file full of names like:

    anna(2 spaces)marie(3 spaces)paul
    logan(3 spaces)simon(2 spaces)natalie
    ian(3 spaces)jasmine(3 spaces)jamie

    I need to check if these names are in a list of names that is into another file like the one below and print them

    Anna
    Jamie
    Ian

    The thing is that I cannot use functions as strcmp or memcmp or strncmp, etc. Just beginners ones as if, else, for. And the names on the former list are separated by two or three spaces (my teacher told me that if I know how to code that wouldnt makes difference).

    Here is my try. It doesnt working at all, so help please

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define MAX_SIZE_NAME 10
    #define NUMB_NAMES 20
    #define MAX_STRING_SIZE 100
    
    typedef struct {
            char names[MAX_SIZE_NAME];
        }listn;
        listn n[NUMB_NAMES];
    
    char process (const char *line)
    {
      int i, j;
      for ( i = 0; line[i]; ++i )
      {
          for ( j = 0; j < NUMB_NAMES; ++j ) //vector for the list of names in the struct
          {
            do {
                    if (line[i] == n[j].names[i]){
                        printf("%s  ", n[j].names);}
    
               }while (line[i]!=' ');
          }
      }
    }
    
    int main()
    {
        int i=0;
    
        FILE *arch;
    
        arch = fopen("names.txt", "r");
    
        if (arch==NULL){
            printf("ERROR");
        }
        else{
            while (!feof(arch)){
                fscanf(arch, "%s\n", n[i].names);
                i++;
                // store the names into the struct
            }
        }
        fclose(arch);
    
    
        char line[MAX_STRING_SIZE];
        FILE *fp;
        fp = fopen("check_names.txt", "r");
        if (fp==NULL){
            printf("ERROR");
        }while (fgets(line, MAX_STRING_SIZE,fp) != NULL){
        process(line);}
        fclose(fp);
    
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,852
    Quote Originally Posted by Mungo
    The thing is that I cannot use functions as strcmp or memcmp or strncmp, etc.
    You should use strcmp or similiar when comparing the strings, but of course not when reading them. If you aren't permitted to do so, then you should write your own version of strcmp first (i.e., the reason why you aren't supposed to use strcmp is to give you practice in writing your own version). If you aren't permitted to do that either, then your teacher is either a poor teacher or a poor programmer, or both, but hopefully it doesn't come to that.

    Quote Originally Posted by Mungo
    And the names on the former list are separated by two or three spaces (my teacher told me that if I know how to code that wouldnt makes difference).
    That's right, because you're just dealing with single "word" names, so you can use fscanf. But, you need to know the maximum length of a name so that you can properly specify the field width when using %s with fscanf.

    Quote Originally Posted by Mungo
    Here is my try. It doesnt working at all, so help please
    You should control the input loop by checking the return value of fscanf, not by calling feof. Also, you don't need the "\n". For example:
    Code:
    while (fscanf(arch, "%s", n[i].names) == 1) {
        i++;
    }
    You need to use descriptive variable names: n will not do. I would call the list of names in the second file something like reference_names, and store them in a struct with both an array member and an integer member for the number of names stored. Then I would read the first file and search reference_names for each name in the first file (i.e., your logic is reversed since you "need to check if these names [in the first file] are in a list of names that is into another file [reference_names]").

    For this exercise, I doubt you need to use do while loops at all.

    process should not return a char. I would declare it like this:
    Code:
    typedef struct {
        char names[NAME_LIST_MAX_SIZE][NAME_MAX_SIZE];
        size_t size;
    } name_list;
    
    int find_name(const struct name_list *reference_names, const char *name);
    The idea is that find_name will return 1 (true) if name can be found in reference_names->names, otherwise it will return 0 (false). You need the size member variable because names might have fewer elements in use than NAME_LIST_MAX_SIZE.

    In your main function, you will then call it like this:
    Code:
    while (fscanf(first_fp, "%s", name) == 1) {
        if (find_name(&reference_names, name)) {
            printf("%s\n", name);
        }
    }
    Notice that I pass reference_names as an argument rather than have it as a global variable. I pass a pointer to it because it is potentially expensive to copy.

    To populate reference_names, your use of fgets in a while loop could work, but note that fgets leaves the newline in the buffer. I think that it may be simpler to just use fscanf as with the first file, unless your teacher wants you to validate that each line does only contain one name.
    Last edited by laserlight; 01-05-2020 at 04:09 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone help me with a school project
    By lexprdn in forum C Programming
    Replies: 2
    Last Post: 01-31-2016, 03:32 PM
  2. Need help with my school project!!!
    By Maciek Grodzki in forum C Programming
    Replies: 5
    Last Post: 01-07-2014, 07:53 AM
  3. School Project Help
    By Joshua Hess in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2013, 07:27 PM
  4. School project help
    By blckgoat in forum C Programming
    Replies: 8
    Last Post: 11-14-2011, 06:03 PM
  5. Help with School Project
    By jtouron in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2003, 12:27 AM

Tags for this Thread