运行TinyFileDialogs的Visual Studio C程序,但最喜欢的颜色弹出窗口不起作用

icnyk63a  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(165)

我一直在做tinyfiledialogs代码项目,我已经设置好了环境,代码也编译好了。

  1. //Number of beeps
  2. void beeper(int num_beeps) {
  3. char message[100];
  4. sprintf(message, "Beeping %d times! \n Press OK to initiate.", num_beeps);
  5. tinyfd_messageBox("Notification", message, "info", "OK", 1);
  6. //beeps
  7. for (int i = 0; i < num_beeps; i++) {
  8. Beep(800, 300); // Frequency and duration of the beep sound
  9. Sleep(300); // Pause between beeps (in milliseconds)
  10. }
  11. }
  1. //Favorite color and store RGB values
  2. void mood_ring(struct FavoriteColor* color) {
  3. char const* options[8] = { "Red", "Orange", "Yellow", "Green", "Blue", "Purple", "Pink", "Cancel" };
  4. int choice = tinyfd_openFileDialog("Pick your favorite color", "Choose a color:", 8, options, NULL, 1);
  5. //User cancel
  6. if (choice == -1 || choice == 7) {
  7. tinyfd_messageBox("Error", "You cancelled the color selection.", "error", "OK", 1);
  8. return;
  9. }
  10. //RGB values
  11. switch (choice) {
  12. case 0: // Red
  13. color->red = 204;
  14. color->green = 0;
  15. color->blue = 0;
  16. break;
  17. case 1: // Orange
  18. color->red = 255;
  19. color->green = 153;
  20. color->blue = 0;
  21. break;
  22. case 2: // Yellow
  23. color->red = 255;
  24. color->green = 255;
  25. color->blue = 0;
  26. break;
  27. case 3: // Green
  28. color->red = 0;
  29. color->green = 128;
  30. color->blue = 0;
  31. break;
  32. case 4: // Blue
  33. color->red = 0;
  34. color->green = 0;
  35. color->blue = 204;
  36. break;
  37. case 5: // Purple
  38. color->red = 102;
  39. color->green = 0;
  40. color->blue = 153;
  41. break;
  42. case 6: // Pink
  43. color->red = 255;
  44. color->green = 20;
  45. color->blue = 147;
  46. break;
  47. }
  48. }
  1. int main(int argc, char* argv[]) {
  2. if (argc < 3) {
  3. return -1;
  4. }
  5. //Number of beeps
  6. int num_beeps = atoi(argv[2]);
  7. beeper(num_beeps);
  8. //Favorite color
  9. struct FavoriteColor person_color;
  10. strncpy(person_color.name, argv[1], sizeof(person_color.name) - 1);
  11. person_color.name[sizeof(person_color.name) - 1] = '\0';
  12. mood_ring(&person_color);
  13. char message[256];
  14. printf(message, sizeof(message), "Name: %s\nRed: %d\nGreen: %d\nBlue: %d", person_color.name, person_color.red, person_color.green, person_color.blue);
  15. tinyfd_messageBox("Favorite Color", message, "info", "OK", 1);
  16. return 0;
  17. }

编辑!我的参数数量是错误的,现在它打印我的名字,并打开一个弹出的哔哔声,并做哔哔声!但没有其他弹出。对于上下文应该有更多的弹出关于最喜欢的颜色,但这并没有发生。命令行打印这一点。

  1. ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠MacKenna


我所期望的是类似于这样的输出:

  1. Input: ./P_MiniProject3 John 5
  2. (Output dialog box appears)
  3. Notification: Beeping 5 times!
  4. (Color selection dialog box appears)
  5. User selects Green as their favorite color.
  6. (Message box appears)
  7. Favorite Color:
  8. Name: John
  9. Red: 0
  10. Green: 255
  11. Blue: 0


或者至少像这样的错误消息:

  1. Input: ./P_MiniProject3 Bob
  2. (Output dialog box appears)
  3. Error: Incorrect number of arguments provided. Please provide a name and the number of beeps.

t30tvxxf

t30tvxxf1#

在尝试对代码进行故障排除时,我不得不做出一些有根据的猜测,并进行一些代码替换,因为我没有使用“tinyfd”小部件集(对“GTK”更熟悉)。
以下是代码的重构版本,以便复制所需的功能,同时考虑了一些假设和有根据的猜测。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. struct FavoriteColor { /* Educated guess of the structure definition as it was not included in the issue */
  5. char name[100];
  6. int red;
  7. int green;
  8. int blue;
  9. };
  10. // Number of beeps
  11. void beeper(int num_beeps)
  12. {
  13. char message[100];
  14. sprintf(message, "Beeping %d times! \nPress OK to initiate.\n", num_beeps);
  15. //tinyfd_messageBox("Notification", message, "info", "OK", 1);
  16. printf("%s", message); /* Substituted this for the message box */
  17. // Beeps
  18. for (int i = 0; i < num_beeps; ++i)
  19. {
  20. printf("Beep\n");
  21. }
  22. }
  23. // Favorite color and store RGB values
  24. void mood_ring(struct FavoriteColor* color)
  25. {
  26. char const* options[8] = { "Red", "Orange", "Yellow", "Green", "Blue", "Purple", "Pink", "Cancel" };
  27. //int choice = tinyfd_openFileDialog("Pick your favorite color", "Choose a color:", 8, options, NULL, 1); /* Suspect line of code */
  28. //char *selection = tinyfd_openFileDialog("Pick your favorite color", "Choose a color:", 8, options, NULL, 1); /* Likely format for using this dialog box */
  29. int choice;
  30. char *selection = "Purple"; /* Simulate making a selection from the dialog box */
  31. for (choice = 0; choice < 8; choice++) /* Evaluating the selection to an integer value */
  32. {
  33. if (strcmp(selection, options[choice]) == 0)
  34. break;
  35. }
  36. // User cancel
  37. if (choice == -1 || choice == 7)
  38. {
  39. printf("You cancelled the color selection\n");
  40. }
  41. // RGB values
  42. switch (choice)
  43. {
  44. case 0: // Red
  45. color->red = 204;
  46. color->green = 0;
  47. color->blue = 0;
  48. break;
  49. case 1: // Orange
  50. color->red = 255;
  51. color->green = 153;
  52. color->blue = 0;
  53. break;
  54. case 2: // Yellow
  55. color->red = 255;
  56. color->green = 255;
  57. color->blue = 0;
  58. case 3: // Green
  59. color->red = 0;
  60. color->green = 128;
  61. color->blue = 0;
  62. break;
  63. case 4: // Blue
  64. color->red = 0;
  65. color->green = 0;
  66. color->blue = 204;
  67. break;
  68. case 5: // Purple
  69. color->red = 102;
  70. color->green = 0;
  71. color->blue = 153;
  72. break;
  73. case 6: // Pink
  74. color->red = 255;
  75. color->green = 20;
  76. color->blue = 147;
  77. break;
  78. default: /* Default - such as when cancel is selected */
  79. color->red = 128;
  80. color->green = 128;
  81. color->blue = 128;
  82. break;
  83. }
  84. }
  85. int main(int argc, char* argv[])
  86. {
  87. if (argc < 3) /* Noted correction in the comments */
  88. {
  89. return -1;
  90. }
  91. // Number of beeps
  92. int num_beeps = atoi(argv[2]);
  93. beeper(num_beeps);
  94. // Favorite color
  95. struct FavoriteColor person_color;
  96. strncpy(person_color.name, argv[1], sizeof(person_color.name) - 1);
  97. person_color.name[sizeof(person_color.name) - 1] = '\0';
  98. mood_ring(&person_color);
  99. //char message[256]; /* This string variable was not being populated and could cause undefined behaviour */
  100. //printf(message, sizeof(message), "Name: %s\nRed: %d\nGreen: %d\nBlue: %d", person_color.name, person_color.red, person_color.green, person_color.blue);
  101. printf("Name: %s\nRed: %d\nGreen: %d\nBlue: %d\n", person_color.name, person_color.red, person_color.green, person_color.blue); /* In lieu of message box */
  102. //tinyfd_messageBox("Favorite Color", message, "info", "ok", 1);
  103. return 0;
  104. }

字符串
首先,我很抱歉包含了完整的代码,但是重构的各个部分在整个程序中都是分散的。

  • 在代码示例中没有“FavoriteColor”的结构定义,但定义了一个结构定义,以便至少允许在程序中捕获所需的信息。
  • 如前所述,对“tinyfd”小部件(消息、对话框等)的任何引用都被注解掉了,取而代之的是打印或模拟输入数据的等效方法,保持了核心功能的完整性。
  • 参考“tinyMessageBox”小部件可能返回的值的类型,包含了一行注解掉的代码,作为重构程序以给予其适当功能的可能建议。
  • 正如其他支持者最初的好评中所指出的那样,参数计数检查已被更正,以测试命令行参数的正确数量。
  • 此外,似乎有可能在“main”函数中使用“message”字符数组的定义存在未定义的行为,因此该位被停用(不知道这是否是示例中缺少代码的结果)。

在查看了编辑过的代码和注解之后,我确实对“tinyfd_messageBox”函数的使用做了一些研究,并注意到这个函数返回一个字符数组(字符串)返回一个字符串值到一个整数变量很可能是您的争论点,因为您可能在此遇到了未定义的行为点
然后,下面是一个带有终端输出的样本运行(紫色是测试选择的颜色)。

  1. craig@Vera:~/C_Programs/Console/Beeper/bin/Release$ ./Beeper Craig 5
  2. Beeping 5 times!
  3. Press OK to initiate.
  4. Beep
  5. Beep
  6. Beep
  7. Beep
  8. Beep
  9. Name: Craig
  10. Red: 102
  11. Green: 0
  12. Blue: 153


这里需要注意的是,可能需要进一步研究“tinyfd”工具包的正确用法,沿着可能需要进一步使用一些“C”教程文献,因为它与变量定义和用法有关。
希望您能够分析此示例代码沿着解释某些替换原因的附加注解。

展开查看全部

相关问题