code critique please.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • matt

    #1

    code critique please.

    this is my first program in this language ever (besides 'hello
    world'), can i get a code critique, please? it's purpose is to read
    through an input file character by character and tally the occurrence
    of each input character. it seems to compile and run, so i'm looking
    for the opinions of old-timers here plz.

    /*
    * File: occurrenceTally .cpp
    * Author: matthew
    *
    * Created on July 9, 2008, 6:15 PM
    */
    #include <stdlib.h>
    #include <fstream.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int main(int argCount, char** argvArray) {
    printf("filenam e ");
    printf("%s",arg vArray[1]);
    printf("\n");
    ifstream OpenFile(argvAr ray[1]);

    char inputCharacter;
    int characters[256];
    int i;
    for (i = 1; i < 256; i++) {
    characters[i]=0;
    }
    while(!OpenFile .eof())
    {
    OpenFile.get(in putCharacter);
    //printf("%c",inp utCharacter);
    characters[int(inputCharac ter)]++;
    }
    OpenFile.close( );
    for (i = 32; i < 126; i++) {
    char outputCharacter ;
    outputCharacter = char(i);
    if ( characters[i]>0 ) {
    printf("%c ", outputCharacter );
    printf("%i\n", characters[i]);}
    }
    return (EXIT_SUCCESS);
    }
  • Gianni Mariani

    #2
    Re: code critique please.

    Alf P. Steinbach wrote:
    * matt:
    >this is my first program in this language ever (besides 'hello
    >world'), can i get a code critique, please? it's purpose is to read
    >through an input file character by character and tally the occurrence
    >of each input character. it seems to compile and run, so i'm looking
    >for the opinions of old-timers here plz.
    >>
    >/*
    > * File: occurrenceTally .cpp
    > * Author: matthew
    > *
    > * Created on July 9, 2008, 6:15 PM
    > */
    >#include <stdlib.h>
    >#include <fstream.h>
    >
    This is not a standard header.
    >
    >
    >#include <stdio.h>
    >#include <string.h>
    >#include <stdlib.h>
    >
    As a newbie you really should use C++ iostreams instead of C low-level i/o.
    >
    >
    >int main(int argCount, char** argvArray) {
    > printf("filenam e ");
    > printf("%s",arg vArray[1]);
    argvArray[1] may not be initialized.

    ....

    Comment

    • matt

      #3
      Re: code critique please.

      On Jul 9, 8:49 pm, Gianni Mariani <gi4nos...@mari ani.wswrote:
      Alf P. Steinbach wrote:
      * matt:
      this is my first program in this language ever (besides 'hello
      world'), can i get a code critique, please? it's purpose is to read
      through an input file character by character and tally the occurrence
      of each input character. it seems to compile and run, so i'm looking
      for the opinions of old-timers here plz.
      >
      /*
       * File:   occurrenceTally .cpp
       * Author: matthew
       *
       * Created on July 9, 2008, 6:15 PM
       */
      #include <stdlib.h>
      #include <fstream.h>
      >
      This is not a standard header.
      >
      #include <stdio.h>
      #include <string.h>
      #include <stdlib.h>
      >
      As a newbie you really should use C++ iostreams instead of C low-level i/o.
      >
      int main(int argCount, char** argvArray) {
          printf("filenam e ");
          printf("%s",arg vArray[1]);
      >
      argvArray[1] may not be initialized.
      >
      ...
      also good. you guys rock!

      Comment

      Working...