First C Program- MastermindFirst Java program critique (Game of Life)First program: a simple calculatorFirst C# program (Snake game)First time ATM machine programA simple Mastermind game in CA simple mastermind cloneFirst encryption programSimple Mastermind in c++First Python program: SnakeFirst python program: basic calculator

Why is so much work done on numerical verification of the Riemann Hypothesis?

Why is short-wave infrared portion of electromagnetic spectrum so sensitive to fire?

Why should universal income be universal?

Fear of getting stuck on one programming language / technology that is not used in my country

Has any country ever had 2 former presidents in jail simultaneously?

This is why we puzzle

Bridge building with irregular planks

How to create table with 2D function values?

Unexpected behavior of the procedure `Area` on the object 'Polygon'

Mixing PEX brands

Why is the "ls" command showing permissions of files in a FAT32 partition?

What are the advantages of simplicial model categories over non-simplicial ones?

Open a doc from terminal, but not by its name

Is there a way to get `mathscr' with lower case letters in pdfLaTeX?

Did arcade monitors have same pixel aspect ratio as TV sets?

Non-trope happy ending?

What is the highest possible scrabble score for placing a single tile

How can mimic phobia be cured?

How should I respond when I lied about my education and the company finds out through background check?

Calculate sum of polynomial roots

Can I say "fingers" when referring to toes?

Does an advisor owe his/her student anything? Will an advisor keep a PhD student only out of pity?

PTIJ: Haman's bad computer

Strong empirical falsification of quantum mechanics based on vacuum energy density?



First C Program- Mastermind


First Java program critique (Game of Life)First program: a simple calculatorFirst C# program (Snake game)First time ATM machine programA simple Mastermind game in CA simple mastermind cloneFirst encryption programSimple Mastermind in c++First Python program: SnakeFirst python program: basic calculator













2












$begingroup$


This is my first full program I've written in C. It took me about 2 hours to pull together today. Looking for any sort of feedback on the code, formatting, cleanliness etc. I didn't follow any guides or tutorials, but did have some existing C++ knowledge and knowledge in other languages



Thanks



#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <ctype.h>

#define RESET "x1B[0m"
#define BLOCK "█"

#define RED "x1B[31m" BLOCK RESET
#define GRN "x1B[32m" BLOCK RESET
#define YEL "x1B[33m" BLOCK RESET
#define BLU "x1B[34m" BLOCK RESET
#define MAG "x1B[35m" BLOCK RESET
#define CYN "x1B[36m" BLOCK RESET
#define WHT "x1B[37m" BLOCK RESET

#define RED_C "x1B[31m"
#define GRN_C "x1B[32m"
#define YEL_C "x1B[33m"
#define BLU_C "x1B[34m"
#define MAG_C "x1B[35m"
#define CYN_C "x1B[36m"
#define WHT_C "x1B[37m"

#define COLORS 6
#define LENGTH 4

enum Colors
red,
green,
yellow,
blue,
magenta,
cyan,
white
;

static const enum Colors Color_map[] = red, green, yellow, blue, magenta, cyan, white;

void *generate_colors(enum Colors *buffer)

int power = pow(COLORS + 1, LENGTH);
int colors_integer = rand() % power;

for (int i = 0; i < LENGTH; ++i)

int remainder = colors_integer % (COLORS + 1);
int divisor = colors_integer / (COLORS + 1);

buffer[i] = Color_map[remainder];

colors_integer = divisor;



int convert_input(char *input, enum Colors *buffer)

for (int c = 0; c < strlen(input); ++c)

char character = tolower(input[c]);

switch (character)

case 'r':
buffer[c] = red;
break;

case 'g':
buffer[c] = green;
break;

case 'y':
buffer[c] = yellow;
break;

case 'b':
buffer[c] = blue;
break;

case 'm':
buffer[c] = magenta;
break;

case 'c':
buffer[c] = cyan;
break;

case 'w':
buffer[c] = white;
break;

default:
return 1;



return 0;


char *color_to_char(enum Colors color)

switch (color)

case red:
return RED;
case green:
return GRN;
case yellow:
return YEL;
case blue:
return BLU;
case magenta:
return MAG;
case cyan:
return CYN;
default:
return WHT;



int main()

srand(time(NULL));

enum Colors selected_colors[4];
generate_colors(selected_colors);

int guessed = 0;

do
char input[LENGTH];
scanf("%s", input);

if (strlen(input) == LENGTH)

enum Colors converted[LENGTH];
int contains_unmatched = convert_input(input, converted);

int correct_place = 0;
int correct_color = 0;

if (contains_unmatched)

printf("Please only choose characters from " RED_C "R, " GRN_C "G, " YEL_C "Y, " BLU_C "B, " MAG_C "M, " CYN_C "C, " WHT_C "W" RESET ".n");

else

printf("You guessed: ");

for (int i = 0; i < LENGTH; ++i)

enum Colors color = converted[i];
enum Colors actual = selected_colors[i];

if (color == actual)
++correct_place;

else

for (int j = 0; j < LENGTH; ++j)

if (j != i)

enum Colors current = selected_colors[j];

if (color == current)

++correct_color;
break;





printf("%s", color_to_char(converted[i]));


if (correct_place == LENGTH)

printf("Well done! You got it right. Goodbye");
return 0;

else

printf("n %d correct colorn %d correct place and colorn", correct_color, correct_place);



else

printf("Please enter 4 characters.n");

while (!guessed);

return 0;

```









share|improve this question









$endgroup$
















    2












    $begingroup$


    This is my first full program I've written in C. It took me about 2 hours to pull together today. Looking for any sort of feedback on the code, formatting, cleanliness etc. I didn't follow any guides or tutorials, but did have some existing C++ knowledge and knowledge in other languages



    Thanks



    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    #include <string.h>
    #include <ctype.h>

    #define RESET "x1B[0m"
    #define BLOCK "█"

    #define RED "x1B[31m" BLOCK RESET
    #define GRN "x1B[32m" BLOCK RESET
    #define YEL "x1B[33m" BLOCK RESET
    #define BLU "x1B[34m" BLOCK RESET
    #define MAG "x1B[35m" BLOCK RESET
    #define CYN "x1B[36m" BLOCK RESET
    #define WHT "x1B[37m" BLOCK RESET

    #define RED_C "x1B[31m"
    #define GRN_C "x1B[32m"
    #define YEL_C "x1B[33m"
    #define BLU_C "x1B[34m"
    #define MAG_C "x1B[35m"
    #define CYN_C "x1B[36m"
    #define WHT_C "x1B[37m"

    #define COLORS 6
    #define LENGTH 4

    enum Colors
    red,
    green,
    yellow,
    blue,
    magenta,
    cyan,
    white
    ;

    static const enum Colors Color_map[] = red, green, yellow, blue, magenta, cyan, white;

    void *generate_colors(enum Colors *buffer)

    int power = pow(COLORS + 1, LENGTH);
    int colors_integer = rand() % power;

    for (int i = 0; i < LENGTH; ++i)

    int remainder = colors_integer % (COLORS + 1);
    int divisor = colors_integer / (COLORS + 1);

    buffer[i] = Color_map[remainder];

    colors_integer = divisor;



    int convert_input(char *input, enum Colors *buffer)

    for (int c = 0; c < strlen(input); ++c)

    char character = tolower(input[c]);

    switch (character)

    case 'r':
    buffer[c] = red;
    break;

    case 'g':
    buffer[c] = green;
    break;

    case 'y':
    buffer[c] = yellow;
    break;

    case 'b':
    buffer[c] = blue;
    break;

    case 'm':
    buffer[c] = magenta;
    break;

    case 'c':
    buffer[c] = cyan;
    break;

    case 'w':
    buffer[c] = white;
    break;

    default:
    return 1;



    return 0;


    char *color_to_char(enum Colors color)

    switch (color)

    case red:
    return RED;
    case green:
    return GRN;
    case yellow:
    return YEL;
    case blue:
    return BLU;
    case magenta:
    return MAG;
    case cyan:
    return CYN;
    default:
    return WHT;



    int main()

    srand(time(NULL));

    enum Colors selected_colors[4];
    generate_colors(selected_colors);

    int guessed = 0;

    do
    char input[LENGTH];
    scanf("%s", input);

    if (strlen(input) == LENGTH)

    enum Colors converted[LENGTH];
    int contains_unmatched = convert_input(input, converted);

    int correct_place = 0;
    int correct_color = 0;

    if (contains_unmatched)

    printf("Please only choose characters from " RED_C "R, " GRN_C "G, " YEL_C "Y, " BLU_C "B, " MAG_C "M, " CYN_C "C, " WHT_C "W" RESET ".n");

    else

    printf("You guessed: ");

    for (int i = 0; i < LENGTH; ++i)

    enum Colors color = converted[i];
    enum Colors actual = selected_colors[i];

    if (color == actual)
    ++correct_place;

    else

    for (int j = 0; j < LENGTH; ++j)

    if (j != i)

    enum Colors current = selected_colors[j];

    if (color == current)

    ++correct_color;
    break;





    printf("%s", color_to_char(converted[i]));


    if (correct_place == LENGTH)

    printf("Well done! You got it right. Goodbye");
    return 0;

    else

    printf("n %d correct colorn %d correct place and colorn", correct_color, correct_place);



    else

    printf("Please enter 4 characters.n");

    while (!guessed);

    return 0;

    ```









    share|improve this question









    $endgroup$














      2












      2








      2





      $begingroup$


      This is my first full program I've written in C. It took me about 2 hours to pull together today. Looking for any sort of feedback on the code, formatting, cleanliness etc. I didn't follow any guides or tutorials, but did have some existing C++ knowledge and knowledge in other languages



      Thanks



      #include <stdio.h>
      #include <stdlib.h>
      #include <time.h>
      #include <math.h>
      #include <string.h>
      #include <ctype.h>

      #define RESET "x1B[0m"
      #define BLOCK "█"

      #define RED "x1B[31m" BLOCK RESET
      #define GRN "x1B[32m" BLOCK RESET
      #define YEL "x1B[33m" BLOCK RESET
      #define BLU "x1B[34m" BLOCK RESET
      #define MAG "x1B[35m" BLOCK RESET
      #define CYN "x1B[36m" BLOCK RESET
      #define WHT "x1B[37m" BLOCK RESET

      #define RED_C "x1B[31m"
      #define GRN_C "x1B[32m"
      #define YEL_C "x1B[33m"
      #define BLU_C "x1B[34m"
      #define MAG_C "x1B[35m"
      #define CYN_C "x1B[36m"
      #define WHT_C "x1B[37m"

      #define COLORS 6
      #define LENGTH 4

      enum Colors
      red,
      green,
      yellow,
      blue,
      magenta,
      cyan,
      white
      ;

      static const enum Colors Color_map[] = red, green, yellow, blue, magenta, cyan, white;

      void *generate_colors(enum Colors *buffer)

      int power = pow(COLORS + 1, LENGTH);
      int colors_integer = rand() % power;

      for (int i = 0; i < LENGTH; ++i)

      int remainder = colors_integer % (COLORS + 1);
      int divisor = colors_integer / (COLORS + 1);

      buffer[i] = Color_map[remainder];

      colors_integer = divisor;



      int convert_input(char *input, enum Colors *buffer)

      for (int c = 0; c < strlen(input); ++c)

      char character = tolower(input[c]);

      switch (character)

      case 'r':
      buffer[c] = red;
      break;

      case 'g':
      buffer[c] = green;
      break;

      case 'y':
      buffer[c] = yellow;
      break;

      case 'b':
      buffer[c] = blue;
      break;

      case 'm':
      buffer[c] = magenta;
      break;

      case 'c':
      buffer[c] = cyan;
      break;

      case 'w':
      buffer[c] = white;
      break;

      default:
      return 1;



      return 0;


      char *color_to_char(enum Colors color)

      switch (color)

      case red:
      return RED;
      case green:
      return GRN;
      case yellow:
      return YEL;
      case blue:
      return BLU;
      case magenta:
      return MAG;
      case cyan:
      return CYN;
      default:
      return WHT;



      int main()

      srand(time(NULL));

      enum Colors selected_colors[4];
      generate_colors(selected_colors);

      int guessed = 0;

      do
      char input[LENGTH];
      scanf("%s", input);

      if (strlen(input) == LENGTH)

      enum Colors converted[LENGTH];
      int contains_unmatched = convert_input(input, converted);

      int correct_place = 0;
      int correct_color = 0;

      if (contains_unmatched)

      printf("Please only choose characters from " RED_C "R, " GRN_C "G, " YEL_C "Y, " BLU_C "B, " MAG_C "M, " CYN_C "C, " WHT_C "W" RESET ".n");

      else

      printf("You guessed: ");

      for (int i = 0; i < LENGTH; ++i)

      enum Colors color = converted[i];
      enum Colors actual = selected_colors[i];

      if (color == actual)
      ++correct_place;

      else

      for (int j = 0; j < LENGTH; ++j)

      if (j != i)

      enum Colors current = selected_colors[j];

      if (color == current)

      ++correct_color;
      break;





      printf("%s", color_to_char(converted[i]));


      if (correct_place == LENGTH)

      printf("Well done! You got it right. Goodbye");
      return 0;

      else

      printf("n %d correct colorn %d correct place and colorn", correct_color, correct_place);



      else

      printf("Please enter 4 characters.n");

      while (!guessed);

      return 0;

      ```









      share|improve this question









      $endgroup$




      This is my first full program I've written in C. It took me about 2 hours to pull together today. Looking for any sort of feedback on the code, formatting, cleanliness etc. I didn't follow any guides or tutorials, but did have some existing C++ knowledge and knowledge in other languages



      Thanks



      #include <stdio.h>
      #include <stdlib.h>
      #include <time.h>
      #include <math.h>
      #include <string.h>
      #include <ctype.h>

      #define RESET "x1B[0m"
      #define BLOCK "█"

      #define RED "x1B[31m" BLOCK RESET
      #define GRN "x1B[32m" BLOCK RESET
      #define YEL "x1B[33m" BLOCK RESET
      #define BLU "x1B[34m" BLOCK RESET
      #define MAG "x1B[35m" BLOCK RESET
      #define CYN "x1B[36m" BLOCK RESET
      #define WHT "x1B[37m" BLOCK RESET

      #define RED_C "x1B[31m"
      #define GRN_C "x1B[32m"
      #define YEL_C "x1B[33m"
      #define BLU_C "x1B[34m"
      #define MAG_C "x1B[35m"
      #define CYN_C "x1B[36m"
      #define WHT_C "x1B[37m"

      #define COLORS 6
      #define LENGTH 4

      enum Colors
      red,
      green,
      yellow,
      blue,
      magenta,
      cyan,
      white
      ;

      static const enum Colors Color_map[] = red, green, yellow, blue, magenta, cyan, white;

      void *generate_colors(enum Colors *buffer)

      int power = pow(COLORS + 1, LENGTH);
      int colors_integer = rand() % power;

      for (int i = 0; i < LENGTH; ++i)

      int remainder = colors_integer % (COLORS + 1);
      int divisor = colors_integer / (COLORS + 1);

      buffer[i] = Color_map[remainder];

      colors_integer = divisor;



      int convert_input(char *input, enum Colors *buffer)

      for (int c = 0; c < strlen(input); ++c)

      char character = tolower(input[c]);

      switch (character)

      case 'r':
      buffer[c] = red;
      break;

      case 'g':
      buffer[c] = green;
      break;

      case 'y':
      buffer[c] = yellow;
      break;

      case 'b':
      buffer[c] = blue;
      break;

      case 'm':
      buffer[c] = magenta;
      break;

      case 'c':
      buffer[c] = cyan;
      break;

      case 'w':
      buffer[c] = white;
      break;

      default:
      return 1;



      return 0;


      char *color_to_char(enum Colors color)

      switch (color)

      case red:
      return RED;
      case green:
      return GRN;
      case yellow:
      return YEL;
      case blue:
      return BLU;
      case magenta:
      return MAG;
      case cyan:
      return CYN;
      default:
      return WHT;



      int main()

      srand(time(NULL));

      enum Colors selected_colors[4];
      generate_colors(selected_colors);

      int guessed = 0;

      do
      char input[LENGTH];
      scanf("%s", input);

      if (strlen(input) == LENGTH)

      enum Colors converted[LENGTH];
      int contains_unmatched = convert_input(input, converted);

      int correct_place = 0;
      int correct_color = 0;

      if (contains_unmatched)

      printf("Please only choose characters from " RED_C "R, " GRN_C "G, " YEL_C "Y, " BLU_C "B, " MAG_C "M, " CYN_C "C, " WHT_C "W" RESET ".n");

      else

      printf("You guessed: ");

      for (int i = 0; i < LENGTH; ++i)

      enum Colors color = converted[i];
      enum Colors actual = selected_colors[i];

      if (color == actual)
      ++correct_place;

      else

      for (int j = 0; j < LENGTH; ++j)

      if (j != i)

      enum Colors current = selected_colors[j];

      if (color == current)

      ++correct_color;
      break;





      printf("%s", color_to_char(converted[i]));


      if (correct_place == LENGTH)

      printf("Well done! You got it right. Goodbye");
      return 0;

      else

      printf("n %d correct colorn %d correct place and colorn", correct_color, correct_place);



      else

      printf("Please enter 4 characters.n");

      while (!guessed);

      return 0;

      ```






      beginner c






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 10 mins ago









      JellyWXJellyWX

      705




      705




















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216020%2ffirst-c-program-mastermind%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216020%2ffirst-c-program-mastermind%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          瀋陽號驅逐艦 目录 接收與服役 配置反潛直升機 武進三型性能升級 歷史 除役 參考資料 外部連結 导航菜单Taiwan Air Power海疆老兵-陽字號驅逐艦沿革World Navies Today: Taiwan (Republic of China)DD-839 USS POWER

          Memorizing the KeyboardThe Norwegian Foreman''If the B…''The Consonant EaterThe Cherry TreeElle Rend Le Coeur Plus AmoureuxFill in the blanks with the number in wordsState of the UnionFind the missing elementsCircuit DiagramWhat's the name of the game show?

          名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局