Good People

“A good person does not need the promise of heaven to see the rewards of being a good person.”

3 thoughts on “Good People

  1. Hey there,

    My mom, Nina, referred you to me. Apparently you tutor my younger brother as well. I am a student at CSU right now and in intro to C Programming. I am having a lot of problems with my homework right now and what wondering if you could help me. our assignment is here:
    https://www.cs.colostate.edu/~cs156/Spring14/wiki/HW4

    and my program thus far

    #include
    #include
    int main ( void )
    {

    // int c;
    // int size[80];
    int valid_lines=0;
    int all_lines=0;
    // int last = ‘\n';
    char domain[80];
    int alexa;
    char name[80];
    int org_lines;
    int net_lines;
    int com_lines;
    int rank;
    char best_alexa_domain[80];
    char best_alexa_name[80];
    int best_alexa;
    char best_rank_name[80];
    char best_rank_domain[80];
    int best_rank;
    char other_lines;

    FILE *file = fopen ( “web-data”, “r” );

    if ( file != NULL )
    {
    char line [ 80 ];

    /* loop through the file */

    while ( fgets ( line, sizeof line, file ) != NULL )
    {
    fscanf(line, “%s %d %d %s”, domain, &alexa, &rank, name);
    all_lines++;
    /* skip blank lines */
    /* skip bad lines */
    /* good lines below */
    /* increment counters */
    valid_lines++;
    /* if dot com */
    if (strstr(domain, “.com”) != NULL)
    {
    com_lines++;
    }

    /* if dot net */
    if (strstr(domain, “.net”) != NULL)
    {
    net_lines++;
    }

    /* if dot org */
    if (strstr(domain, “.org”) != NULL)
    {
    org_lines++;
    }
    if (best_alexa < alexa)
    {
    best_alexa = alexa;
    best_alexa_domain = domain; /* may need to use strcpy() here */
    best_alexa_name = name; /* may need to use strcpy() here */
    }
    if (best_rank < rank)
    {
    best_rank = rank;
    best_rank_domain = domain; /* may need to use strcpy() here */
    best_rank_name = name; /* may need to use strcpy() here */
    }

    } /* end while loop */
    fclose ( file );

    printf("Lines: %d",valid_lines);
    printf("Best Rank: %d %s",best_rank,best_rank_domain);
    printf("Best Alexa: %d %s",best_alexa,best_alexa_domain);
    printf(".com: %d",com_lines);
    printf(".net: %d",net_lines);
    printf(".org: %d",org_lines);
    printf("Other: %d",other_lines);

    }
    else /* open error */
    {
    printf("sorry, I couldn't open web-data\n");
    return 1;

    }
    return 0;
    }

    any comments and corrections would be really appreciated!

    Thanks,
    Lexi

    • one more thing,
      I had originially written the beginning of my code as

      #include

      int main()
      {
      int c;
      int size[80];
      int line=0;
      int last = ‘\n';

      FILE *first = fopen(“web-data”, “r”);
      if (first == NULL)
      {
      printf(“sorry, I couldn’t open web-data\n”);
      return 0;
      }
      if (first != NULL)
      {
      if(size[0] != ”)
      {
      –line;
      }
      while ((c=fgetc(first)) != EOF)
      {
      if(c==’#’)
      {
      –line;
      }
      else if(c==last)
      {
      ++line;
      }
      }
      printf(“lines: %d\n”, line);
      }
      return 0;
      }

      which satisfied the first section of our requirements

  2. Hello Lexi –

    I think I see you problem – but my C skills are a bit rusty, so bare with me…

    The crux of the problem is fscanf … you are feeding it the contents of the variable ‘line’ which is a char* but it requires a stream FILE* ( ‘file’ in this case )

    The other thing you have going is that in strstr(), you must cast to use const char* like so:
    if (strstr((const char*)domain, “.net”) != NULL)

    See how I cast domain to be a const char* ?

    you are also trying to do char* assignments, and your comments about using strcpy(), and you are correct.

    read the man page on strcpy and you will find some others ( like strdup, memcpy and others that are similar )

    Man pages are your friend, and the compiler warnings – though cryptic at first – are worth your time to figure out what they are saying because they are your golden ticket to moving forward fast.

    I sent you an email as well, so you can email me anytime you need.

Leave a Reply

Allowed HTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>