Skip to main content
added further fault
Source Link
William Morris
  • 9.4k
  • 19
  • 43

A few issues, in no particular order:

  • put main last to avoid the need for a prototype.

  • main normally takes argc/argv parameters

  • special_chars should be static - but it is debateable whether a separate function is necessary in a short program like this.

  • main defines variable x and passes it to special_chars. This is unnecessary. Just define ch in special_chars and define the function as taking void (i.e. nothing)

  • why does special_chars return a value?

  • indenting is wrong

  • too many blank lines (8)

  • missing {} brackets after if (pairsNum == 10)

  • mixed naming styles (pairsNum should be pairs_num or special_chars should be specialChars - I prefer the former)

  • special_chars is a bad name for the function.

  • pairsNum is an odd name; n_pairs would be my choice.

  • you don't print a final \n at the end of the program

  • you don't start a new line after reading a \n, as required - it appears that you do when you type at the keyboard because entering a \n causes a new line. But if you redirect input into your program it does not. Also you don't start a line after each ten, only after the first ten. e.g. if your program executable is called program

      $ cat x
      abcdefgh
      ijklmnop
      qrstuvwxyz
    
      $./program < x.
      please enter a some characters, and ctrl + d to quit
      a,97 b,98 c,99 d,100 e,101 f,102 g,103 h,104 \n i,105 
      j,106 k,107 l,108 m,109 n,110 o,111 p,112 \n q,113 r,114 s,115 t,116 u,117 v,118 w,119 x,120 y,121 z,122 \n $ 
    

A few issues, in no particular order:

  • put main last to avoid the need for a prototype.

  • main normally takes argc/argv parameters

  • special_chars should be static - but it is debateable whether a separate function is necessary in a short program like this.

  • main defines variable x and passes it to special_chars. This is unnecessary. Just define ch in special_chars and define the function as taking void (i.e. nothing)

  • why does special_chars return a value?

  • indenting is wrong

  • too many blank lines (8)

  • missing {} brackets after if (pairsNum == 10)

  • mixed naming styles (pairsNum should be pairs_num or special_chars should be specialChars - I prefer the former)

  • special_chars is a bad name for the function.

  • pairsNum is an odd name; n_pairs would be my choice.

  • you don't print a final \n at the end of the program

  • you don't start a new line after reading a \n, as required - it appears that you do when you type at the keyboard because entering a \n causes a new line. But if you redirect input into your program it does not. e.g. if your program executable is called program

      $ cat x
      abcdefgh
      ijklmnop
      qrstuvwxyz
    
      $./program < x.
      please enter a some characters, and ctrl + d to quit
      a,97 b,98 c,99 d,100 e,101 f,102 g,103 h,104 \n i,105 
      j,106 k,107 l,108 m,109 n,110 o,111 p,112 \n q,113 r,114 s,115 t,116 u,117 v,118 w,119 x,120 y,121 z,122 \n $ 
    

A few issues, in no particular order:

  • put main last to avoid the need for a prototype.

  • main normally takes argc/argv parameters

  • special_chars should be static - but it is debateable whether a separate function is necessary in a short program like this.

  • main defines variable x and passes it to special_chars. This is unnecessary. Just define ch in special_chars and define the function as taking void (i.e. nothing)

  • why does special_chars return a value?

  • indenting is wrong

  • too many blank lines (8)

  • missing {} brackets after if (pairsNum == 10)

  • mixed naming styles (pairsNum should be pairs_num or special_chars should be specialChars - I prefer the former)

  • special_chars is a bad name for the function.

  • pairsNum is an odd name; n_pairs would be my choice.

  • you don't print a final \n at the end of the program

  • you don't start a new line after reading a \n, as required - it appears that you do when you type at the keyboard because entering a \n causes a new line. But if you redirect input into your program it does not. Also you don't start a line after each ten, only after the first ten. e.g. if your program executable is called program

      $ cat x
      abcdefgh
      ijklmnop
      qrstuvwxyz
    
      $./program < x
      please enter a some characters, and ctrl + d to quit
      a,97 b,98 c,99 d,100 e,101 f,102 g,103 h,104 \n i,105 
      j,106 k,107 l,108 m,109 n,110 o,111 p,112 \n q,113 r,114 s,115 t,116 u,117 v,118 w,119 x,120 y,121 z,122 \n $ 
    
Source Link
William Morris
  • 9.4k
  • 19
  • 43

A few issues, in no particular order:

  • put main last to avoid the need for a prototype.

  • main normally takes argc/argv parameters

  • special_chars should be static - but it is debateable whether a separate function is necessary in a short program like this.

  • main defines variable x and passes it to special_chars. This is unnecessary. Just define ch in special_chars and define the function as taking void (i.e. nothing)

  • why does special_chars return a value?

  • indenting is wrong

  • too many blank lines (8)

  • missing {} brackets after if (pairsNum == 10)

  • mixed naming styles (pairsNum should be pairs_num or special_chars should be specialChars - I prefer the former)

  • special_chars is a bad name for the function.

  • pairsNum is an odd name; n_pairs would be my choice.

  • you don't print a final \n at the end of the program

  • you don't start a new line after reading a \n, as required - it appears that you do when you type at the keyboard because entering a \n causes a new line. But if you redirect input into your program it does not. e.g. if your program executable is called program

      $ cat x
      abcdefgh
      ijklmnop
      qrstuvwxyz
    
      $./program < x.
      please enter a some characters, and ctrl + d to quit
      a,97 b,98 c,99 d,100 e,101 f,102 g,103 h,104 \n i,105 
      j,106 k,107 l,108 m,109 n,110 o,111 p,112 \n q,113 r,114 s,115 t,116 u,117 v,118 w,119 x,120 y,121 z,122 \n $