0

so I'm having the user input a destination, flight time, and a layover time. The input will always look like "destination, 0:00, 0:00" and I'm trying to copy the input and reprint it. I have some function to ensure its inputted in the proper format, but is there a way to print say the flight time after the first comma? Maybe I'm over complicating it, any help would be great! Thanks!

Current Objective: Once I have validated the that the user inputted their string with the correct format, I want to copy it, then reprint. I cannot use sscanf, strtok, or any other function that does tokenization of string data.

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

/* == FUNCTION PROTOTYPES == */
int checkForComma(char* pUserString, char comma);
int checkForColon(char* pUserString, char colon);


/* == CONSTANTS == */
const char kComma = ',';
const char kColon = ':';

int main()
{
    int numbers = 0;
    int loopEnd = 0;
    int flightTime = 0;
    int inputAccepted = 0;
    int commaFound = 0;
    int colonFound = 0;

    while (loopEnd != 1)
    {
        char programOutput[81] = "";
        char userInput[81] = "";

        fgets(userInput, 81, stdin);

        strcpy_s(programOutput, 81, userInput);

        if ((userInput == NULL) || (userInput == '\0'))
        {
            printf("Please enter valid input!\n");
            return 0;
        }

        if (userInput == '.')
        {
            loopEnd = 1;
        }

        while (inputAccepted == 0)
        {
            commaFound = checkForComma(programOutput, kComma);
            colonFound = checkForColon(programOutput, kColon);

            if (commaFound && colonFound == 2)
            {
                printf("Input Accepted!");

                while (commaFound && colonFound == 2)
                {
                    int numberCounter = 0;
                    char* numCheck = programOutput;


                }
                break;
            }
            else
            {
                printf("Input not accepted!");
                return 0;
            }
        }

        printf("Press ENTER key to Continue\n");
        getchar();
    }

    return 0;
}


int checkForComma(char* pUserString, char comma)
{
    int counter = 0;

    char* pCheck = pUserString;

    while ((pCheck = strchr(pCheck, comma)) !=NULL)
    {
        counter++;
        pCheck++;
    }

    return counter;
}

int checkForColon(char* pUserString, char colon)
{
    int counter = 0;

    char* pCheck = pUserString;

    while ((pCheck = strchr(pCheck, colon)) != NULL)
    {
        counter++;
        pCheck++;
    }

    return counter;
}
RocktheFries
  • 221
  • 2
  • 10
  • Not clear what's being asked here. If the input format is "dest, flight time, layover time", and you want to reprint the input with flight time after the first comma... if the input is correctly formatted, isn't the flight time already after the first comma by definition? Also, checkForComma() and checkForColon() are the same function, no? Since you're already passing the comma and colon char as an argument, you should have one function called countCharOccurences(). – flu Mar 29 '19 at 18:48
  • @RocktheFries "cannot use sscanf" is unclear. `sscanf()` is a very nice tool to use here. Why are you prohibiting it? – chux - Reinstate Monica Mar 29 '19 at 19:39
  • Can you use `strcspn()`, `strspn()`? – chux - Reinstate Monica Mar 29 '19 at 19:40
  • something like for loop going through the string. Find a comma, print characters until done? – Michael Dorgan Mar 29 '19 at 20:46

0 Answers0