mardi 3 mai 2016

Identify how many words are formatted as dates of the form MMDDYY in a string and count them. (C++)

The objective of this program is to read a line of characters to count and identify the number of words in the line. It must identify how many of those words are of the format as dates of the form MMDDYY.

For example, if the user enters: "Four score and die captain ago"

The output should be the following.

Total number of words: 6

Number of valid dates: 0

If the user enters "Four 032356 team ironman seven 231290 ago"

The output should be

Total number of words: 7

Number of valid dates: 1

I'm having a problem implementing the function validateDate. Also I don't know whether or not my approach of the date validiation is correct. My sincerest apology if the code is a mess, I'm kinda learning the basics. Thanks in advance. Here's the code:

// #include "stdafx.h"

// This program demonstrates the use of dynamic variables

// Dean DeFino

#include <iostream>
#include <cctype>
#include <cstring>

using namespace std;

const int MAXNAME = 10;

//Function prototype
void processCstring(char *);
bool validateDate(char[], int);

int main()
{
    int pos;
    char *name = nullptr;

    int result;

    const int SIZE = 81;    // The maximum size of the C-string.
    char *cstring;          // To hold a C-string
    char goAgain;           // To hold Y or N

    cstring = new char[SIZE]; // Fill in code to allocate the character array pointed to by cstring

    name = new char[SIZE];    // Fill in code to allocate the character array pointed to by name

    cout << "Enter your last name with exactly 10 characters." << endl;
    cout << "If your name has < 10 characters, repeat last letter. " << endl;
    cout << "Blanks at the end do not count." << endl;

    for (pos = 0; pos < MAXNAME; pos++)
        cin >> *(name + pos);   // Fill in code to read a character into the name array
                                // WITHOUT USING a bracketed subscript
    cout << "Hi ";

    for (pos = 0; pos < MAXNAME; pos++)
        cout << *(name + pos);

    do
    {
        cin.ignore();
        // Get a C-string.
        cout << "\nEnter a C-string containing 80 or fewer characters:\n";
        cin.getline(cstring, SIZE);

        // Display the number of words in the C-string.
        cout << "\nThe number of words in the C-string:  ";
        processCstring(cstring);


        // Does the user want to do this again?
        cout << "Process another string? (Y or N) ";
        cin.get(goAgain);

        // Validate the input. If the user enter something diferent of y,Y or n,N
        //then ask again for an answer.
        while ((toupper(goAgain) != 'Y') && (toupper(goAgain) != 'N'))
        {
            cout << "Please enter Y or N: ";
            cin.get(goAgain);
        }

    } while (toupper(goAgain) == 'Y');


    // Fill in code to deallocate cstring and name (two steps each pointer)
    delete cstring;
    cstring = nullptr;
    delete name;
    name = nullptr;

    return 0;
}

//***************************************************
// Function processString                               *
// This function counts the number of words and the  *
// number of valid datres in a string passed into an str.   *
//***************************************************

void processCstring(char *cStrPtr)
{
    char *str = cStrPtr;
    int numWords = 0;
    int numDates = 0;
    int count = 0; // Variable para que el loop no sea infinito.

    //****WARNING
    //Do NOT RUN this while loop until you fixed.
    //This is  infinite loop
    while (*(str + count) != '\0')
    {
        if (isspace(*(str + count)))    //Complete the code to count the number of words.
            count++;                    //Skip any leading spaces
        else if (isalnum(*(str + count)) && isspace(*(str + count + 1)))
            numWords++;
        count++;

        (validateDate(str, strlen(str));
            cout << "h";

        //Now count the number of valid dates in the form MMDDAA
        //for example 040390 is a valid date, 230490 is not valid
        // 23jun90 is not valid.
        //Use the function validateDate (pass the word you found as
        //parameter) to validate if the word is a valid date

    }

    cout << "\nTotal number of words: " << numWords + 1 << endl;
    cout << "Number of valid dates: " << numDates << endl;
}


//***************************************************
// Function validateDate                               *
// This function validate if a word received as a parameter  *
// is a valid date.                                     *
//***************************************************bool 

validateDate(char myPass[], int size)
{
    bool answer = false;
    int i = 0; // Arreglar
​
    while (i < size && !answer)
    {
        /*Validar que sea un string de 6 chars
            Que el primer char1 < 2
            char1 + char2 <= 9
            char3 < 4
            char3 + char4 <= 11*/
​
        if (static_cast<int>(myPass[0]) < 2)
        {
            if (static_cast<int>(myPass[0]) + static_cast<int>(myPass[1]) <= 9)
            {
                if (static_cast<int>(myPass[2]) < 4)
                {
                    if (static_cast<int>(myPass[2]) + static_cast<int>(myPass[3]) <= 11)
                    {
                        answer = true;
                        i++;
                    }
                }

            }
        }
    }
    return answer;
}

Aucun commentaire:

Enregistrer un commentaire