MacKenna Bochnak
MacKenna Bochnak

Reputation: 27

Running Visual Studio C program for TinyFileDialogs but the favorite color popups don't work

I've been working on the tinyfiledialogs code project, I have the environment all set up and the code compiles.

//Number of beeps
void beeper(int num_beeps) {
    char message[100];
    sprintf(message, "Beeping %d times! \n Press OK to initiate.", num_beeps);
    tinyfd_messageBox("Notification", message, "info", "OK", 1);
    
    //beeps
   for (int i = 0; i < num_beeps; i++) {
        Beep(800, 300);  // Frequency and duration of the beep sound
        Sleep(300);      // Pause between beeps (in milliseconds)
    }
    
}
//Favorite color and store RGB values
void mood_ring(struct FavoriteColor* color) {
    char const* options[8] = { "Red", "Orange", "Yellow", "Green", "Blue", "Purple", "Pink", "Cancel" };
    int choice = tinyfd_openFileDialog("Pick your favorite color", "Choose a color:", 8, options, NULL, 1);

    //User cancel
    if (choice == -1 || choice == 7) {
        tinyfd_messageBox("Error", "You cancelled the color selection.", "error", "OK", 1);
        return;
    }

    //RGB values
    switch (choice) {
    case 0: // Red
        color->red = 204;
        color->green = 0;
        color->blue = 0;
        break;
    case 1: // Orange
        color->red = 255;
        color->green = 153;
        color->blue = 0;
        break;
    case 2: // Yellow
        color->red = 255;
        color->green = 255;
        color->blue = 0;
        break;
    case 3: // Green
        color->red = 0;
        color->green = 128;
        color->blue = 0;
        break;
    case 4: // Blue
        color->red = 0;
        color->green = 0;
        color->blue = 204;
        break;
    case 5: // Purple
        color->red = 102;
        color->green = 0;
        color->blue = 153;
        break;
    case 6: // Pink
        color->red = 255;
        color->green = 20;
        color->blue = 147;
        break;
    }
}

int main(int argc, char* argv[]) {
    if (argc < 3) {
        return -1;
    }

    //Number of beeps
    int num_beeps = atoi(argv[2]);
    beeper(num_beeps);


    //Favorite color
    struct FavoriteColor person_color;

    strncpy(person_color.name, argv[1], sizeof(person_color.name) - 1);
    person_color.name[sizeof(person_color.name) - 1] = '\0';

    mood_ring(&person_color);

    char message[256];
    printf(message, sizeof(message), "Name: %s\nRed: %d\nGreen: %d\nBlue: %d", person_color.name, person_color.red, person_color.green, person_color.blue);

    tinyfd_messageBox("Favorite Color", message, "info", "OK", 1);

    return 0;
}

EDIT! My number of arguments was wrong and now it prints my name and opens a pop up for the beeps and does the beeps! but nothing else pops up. For context there should be more popups regarding favorite color but that doesn't happen. The command line prints this.

╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠MacKenna

What I expect is an output similar to this:

Input: ./P_MiniProject3 John 5
(Output dialog box appears)
Notification: Beeping 5 times!
(Color selection dialog box appears)
User selects Green as their favorite color.
(Message box appears)
Favorite Color:
Name: John
Red: 0
Green: 255
Blue: 0

or at least error messages like:

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

Upvotes: 0

Views: 92

Answers (1)

NoDakker
NoDakker

Reputation: 4816

In attempting to troubleshoot your code to zero in on the problem, I had to make a few educated guesses and do some code substitution as I have not utilized the "tinyfd" widget set (more familiar with "GTK"). With that in mind, I did get the apparent functionality to work as it pertains to storing command line information and color choices within a defined structure.

Following is a refactored version of your code in order to replicate the desired functionality, taking into account some assumptions and educated guesses.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct FavoriteColor {          /* Educated guess of the structure definition as it was not included in the issue */
    char name[100];
    int red;
    int green;
    int blue;
};

// Number of beeps
void beeper(int num_beeps)
{
    char message[100];
    sprintf(message, "Beeping %d times! \nPress OK to initiate.\n", num_beeps);
    //tinyfd_messageBox("Notification", message, "info", "OK", 1);
    printf("%s", message);      /* Substituted this for the message box */

    // Beeps
    for (int i = 0; i < num_beeps; ++i)
    {
        printf("Beep\n");
    }
}

// Favorite color and store RGB values
void mood_ring(struct FavoriteColor* color)
{
    char const* options[8] = { "Red", "Orange", "Yellow", "Green", "Blue", "Purple", "Pink", "Cancel" };

    //int choice = tinyfd_openFileDialog("Pick your favorite color", "Choose a color:", 8, options, NULL, 1);       /* Suspect line of code */

    //char *selection = tinyfd_openFileDialog("Pick your favorite color", "Choose a color:", 8, options, NULL, 1);  /* Likely format for using this dialog box */

    int choice;

    char *selection = "Purple";                 /* Simulate making a selection from the dialog box  */

    for (choice = 0; choice < 8; choice++)      /* Evaluating the selection to an integer value     */
    {
        if (strcmp(selection, options[choice]) == 0)
            break;
    }

    // User cancel
    if (choice == -1 || choice == 7)
    {
        printf("You cancelled the color selection\n");
    }

    // RGB values
    switch (choice)
    {
    case 0: // Red
        color->red = 204;
        color->green = 0;
        color->blue = 0;
        break;
    case 1: // Orange
        color->red = 255;
        color->green = 153;
        color->blue = 0;
        break;
    case 2: // Yellow
        color->red = 255;
        color->green = 255;
        color->blue = 0;
    case 3: // Green
        color->red = 0;
        color->green = 128;
        color->blue = 0;
        break;
    case 4: // Blue
        color->red = 0;
        color->green = 0;
        color->blue = 204;
        break;
    case 5: // Purple
        color->red = 102;
        color->green = 0;
        color->blue = 153;
        break;
    case 6: // Pink
        color->red = 255;
        color->green = 20;
        color->blue = 147;
        break;

    default:                /* Default - such as when cancel is selected */
        color->red = 128;
        color->green = 128;
        color->blue = 128;
        break;
    }
}

int main(int argc, char* argv[])
{
    if (argc < 3)           /* Noted correction in the comments */
    {
        return -1;
    }

    // Number of beeps
    int num_beeps = atoi(argv[2]);
    beeper(num_beeps);

    // Favorite color
    struct FavoriteColor person_color;

    strncpy(person_color.name, argv[1], sizeof(person_color.name) - 1);
    person_color.name[sizeof(person_color.name) - 1] = '\0';

    mood_ring(&person_color);

    //char message[256];        /* This string variable was not being populated and could cause undefined behaviour */
    //printf(message, sizeof(message), "Name: %s\nRed: %d\nGreen: %d\nBlue: %d", person_color.name, person_color.red, person_color.green, person_color.blue);

    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 */

    //tinyfd_messageBox("Favorite Color", message, "info", "ok", 1);

    return 0;
}

First off, my apologies for including the full code, but the various bits of refactoring were spread out through the entirety of the program. Following are the main points.

  • Not having the structure definition for "FavoriteColor" within your code sample, one was defined to at least allow for the capture of information as needed within the program.
  • As noted, any reference to "tinyfd" widgets (messages, dialog boxes, and so forth) were commented out and an equivalent method to print or simulate inputting data was substituted, keeping the core functionality intact.
  • Referencing the type of value the "tinyMessageBox" widget supposedly returns, a commented out line of code was included as a possible suggestion for refactoring the program to give it the proper functionality.
  • As noted in the original good comments from the other supporters, the argument count check was corrected to test for the proper number of command line parameters.
  • Also, there seemed to be a possibility of having undefined behavior in the "main" function with the definition of the "message" character array, so this bit was deactivated (do not know if this was a result of some missing code from your sample).

After reviewing your edited code and comments as to where things fall over, I did do some research on the use of the "tinyfd_messageBox" function and noticed that this function returns a character array (string) in lieu of an integer which is how you had your line of code defined. Returning a string value to an integer variable quite possibly is your point of contention as you probably are experiencing undefined behavior at this point.

With that, following was a sample run with terminal output (Purple was the chosen color for the test).

craig@Vera:~/C_Programs/Console/Beeper/bin/Release$ ./Beeper Craig 5
Beeping 5 times! 
Press OK to initiate.
Beep
Beep
Beep
Beep
Beep
Name: Craig
Red: 102
Green: 0
Blue: 153

The things to take away here are that possibly further research is needed into the proper usage of the "tinyfd" toolkit, along with possible further usage of some "C" tutorial literature as it pertains to variable definitions and usage.

Hopefully, you will be able to analyze this sample code along with the additional comments explaining the reasons for some of the substitutions.

Upvotes: 0

Related Questions