Hey everyone. I want to call dateConversion and send along the date string which I do with this "dateConversion(dateString);" I want that to print the month day and year. I will have to do it at this line "cout << "on " << date << endl << endl;".

When I do this "cout << "on " << dateConversion(dateString); << endl << endl;". But it doesn't work. I know these are dumb questions but Im lagging a little behind in class, I have a hard time understanding some material because the teacher goes so fast.

//This program processes data from a comma delimited file and
//displays donation information for PBS
//Written By: Jordan Hus

#include<iostream>
#include<cmath>
#include<iomanip>
#include<fstream>
#include<string>

using namespace std;

const int MAX_ARRAY_ZIP = 42200;                  // Max number of zip code entries

typedef char StateType[3];                      // Data type for state char. strings (max 2 char)
typedef char CityType[31];                      // Data type for city char. strings (max 30 char)

// Function prototypes
void ReadZipCodeFile(int zip[], double lat[], double lon[],
                         StateType state[], CityType city[], int& numElemsZip);
int strIndex(char inStr[], int start, char searchTarget);
void substring(char inStr[], int start, int end, char outStr[]);
char dateConversion(char dateString[]);
int binarySearch(int array[], int numElemsZip, int value);


int main()
{
        //variables for position and creating substrings
        int pos, oldPosition, index;
        int zipCode;
        double donatedAmt, taxDeduct;
        char inputLine[60];
        char date[20];
        char zipString[6];
        char dateString[50];
        char donAmtString[50], tempdonAmtString[50];
        char lastName[36];

        // Declare pointers for array references
        int* zip;  
        double* latitude;
        double* longitude;  
        StateType* state;
        CityType* city;   
        
        // Declare parallel arrays for zip code data
        zip = new int[MAX_ARRAY_ZIP];             // Dynamic array for zip codes
        latitude  = new double[MAX_ARRAY_ZIP]; // Dynamic array for latitudes
        longitude = new double[MAX_ARRAY_ZIP]; // Dynamic array for longitudes
        state = new StateType[MAX_ARRAY_ZIP];  // Dynamic array for states
        city = new CityType[MAX_ARRAY_ZIP];     // Dynamic array for cities

        int numElemsZip;                                           // Actual number of values in Zip Code array 
                                                                                   // (1 more than largest index)

        // Read file and count values in zip code array
        ReadZipCodeFile(zip,latitude,longitude,state,city,numElemsZip);

        // Donation File initialization
        ifstream DonationFile ("donations.txt");        
        if (DonationFile.fail() )                // Test for file existence
        {
          cout <<  "Problem opening file";
          exit(-1);
        }

        DonationFile.getline(inputLine, 61);
        while( ! DonationFile.eof())
        {
                //Locate date string and convert to long format
                pos = strIndex(inputLine, 0, ',');                          //mark first comma location
                substring(inputLine, 0, pos, dateString);                   //get date as string
                dateConversion(dateString);								    //convert the date

                //Locate zip string and convert to integer
                oldPosition = pos;
                pos = strIndex(inputLine, oldPosition + 1, ',');            // Mark comma location
                substring(inputLine, oldPosition + 1, pos - 1, zipString);  // Get zip code as string
                zipCode = atoi(zipString);

                // Locate donation amount string, extract "$", and convert to double
                oldPosition = pos;                                          // Keep current comma location
                pos = strIndex(inputLine, oldPosition+1, ',');              // Mark 2nd comma location
                substring(inputLine, oldPosition+1, pos-1, donAmtString);   // Get weight info as string
                oldPosition = pos;                                          // Keep current comma location
                pos = strIndex(donAmtString, 0, '$');                       // Mark position of $
                substring(donAmtString, pos + 1, oldPosition - 1,tempdonAmtString);                             // Remove "$" prefix
                strcpy(donAmtString,tempdonAmtString);
                donatedAmt = atof(donAmtString);
                taxDeduct = donatedAmt * 0.8;

                // Locate last name (as last data token and extract)
                pos = strlen(inputLine) - 1;                                // Mark position: end of lastname
                substring(inputLine, oldPosition+1, pos, lastName);         // Get name as string

                //search for city and state of zip code
                index = binarySearch(zip, numElemsZip, zipCode);

                
                //Output message
                //name and city and state
                cout << "The " << lastName << " household from " << city[index] << ", "
                         << state[index] << endl;
                //donation amount with tax deductible information
                cout << "donated $" << setprecision(2) << fixed << donatedAmt  
                         << "($" << setprecision(2) << fixed<< taxDeduct << " tax-deductible)"
                         << endl;
                //date information
                cout << "on " << date << endl << endl;
                
                DonationFile.getline(inputLine, 61);                         //continuation read
        }
}

//Read Zip code file and count values in array
void ReadZipCodeFile(int zip[], double lat[], double lon[],
                         StateType state[], CityType city[], int& numElemsZip)
{
        //OPEN ZIP CODE FILE
        ifstream ZipCodeFile;                                                // Input file identifier       
        
        ZipCodeFile.open("zipUS.txt");                                       // Open file
        
        if (ZipCodeFile.fail() )                                             // Test for file existence
        {
          cout <<  "Problem opening file";
          exit(-1);
        }

        int i = 0;
        
        ZipCodeFile >> zip[i] >> lat[i] >> lon[i] >> state[i] >> city[i];                 
        while (!ZipCodeFile.eof() && i < MAX_ARRAY_ZIP)         
        {
           i++;         
           ZipCodeFile >> zip[i] >> lat[i] >> lon[i] >> state[i] >> city[i];              
        }                       
        numElemsZip = i;
}

//This function searches a string for a given character
//(in line, start, target) if not found, returns -1
int strIndex(char inStr[], int start, char searchTarget)
{
   int pos = start;                                                           // Set position marker to start index
   int returnPos = -1;													      // Character position marker, assume not found
   bool found = false;													      // Used to exit loop when found, assume not found at start
   
   while (inStr[pos] != '\0' && !found)
   {
          if (inStr[pos] == searchTarget)								      // If character found,
          { 
                 returnPos = pos;											  //   mark position
                 found = true;												  //    set switch allowing exit from loop.
          }
          else
                 pos++;														  // Otherwise, advance to next character
   
   }
   return returnPos;														  // Return value
}

//This function performs a substrin operation on c-strings
//it creates a SUB string from longer line of text.
//(input, start, end, outname)
void substring(char inStr[], int start, int end, char outStr[])
{
   strcpy(outStr, "");														  // Empty output string

   int pos = 0;																  // To mark curr position of evolving substring
   for (int i = start; i <= end; i++)
   {
          outStr[pos] = inStr[i];									          // Move character to output string
          pos++;                                                              // Manually advance position marker
   }
        
   outStr[pos] = '\0';                                                        // Add null character to complete string
}

//This function converts a cstring date to its long format
void dateConversion(char dateString[])
{

	int monthInt, day, year;
	char month[20];
	
	//use the ASCII table to covert characters in the string into integers
	monthInt = (10 * int(dateString[0] - 48) + int(dateString[1] -48));
    day = (10 * int(dateString[3] - 48) + int(dateString[4] -48));
    year = (10 * int(dateString[6] - 48) + int(dateString[7] -48) + 2000);

	switch(monthInt)
        {
                case 1: strcpy(month,"January");
                        break;
                case 2: strcpy(month,"Feburay");
                        break;
                case 3: strcpy(month,"March");
                        break;
                case 4: strcpy(month,"April");
                        break;
                case 5: strcpy(month,"May");
                        break;
                case 6: strcpy(month,"June");
                        break;
                case 7: strcpy(month,"July");
                        break;
                case 8: strcpy(month,"August");
                        break;
                case 9: strcpy(month,"September");
                        break;
                case 10:strcpy(month,"October");
                        break;
                case 11:strcpy(month,"November");
                        break;
                case 12:strcpy(month,"December");
                        break;
                default: strcpy(month,"There has been a problem");
        }

}
        

// The binarySearch function performs a binary search on an   
// integer array. array, which has a maximum of numelems          
// elements, is searched for the number stored in value. If the
// number is found, its array subscript is returned. Otherwise,
// -1 is returned indicating the value was not in the array.  

int binarySearch(int array[], int numElemsZip, int value)
{
        int first = 0,                           // First array element
                last = numElemsZip - 1,    // Last array element
                middle,                                 // Mid point of search
                position = -1;                   // Position of search value
        bool found = false;                     // Flag

        while (!found && first <= last)
        {
                middle = (first + last) / 2;     // Calculate mid point
                if (array[middle] == value)       // If value is found at mid
                {
                        found = true;
                        position = middle;
                }
                else if (array[middle] > value)  // If value is in lower half
                        last = middle - 1;
                else
                        first = middle + 1;               // If value is in upper half
        }
        return position;
}

Edited 6 Years Ago by JHus00: n/a

Your return-type for "dateConversion" in the declaration is "char" (line 23). This means it can only return ONE character. That's problem numero uno.
The next problem is that the definition of "dateConversion" returns "void" (line 182), which means it returns nothing.

This code should not compile. What compiler are you using?

Your return-type for "dateConversion" in the declaration is "char" (line 23). This means it can only return ONE character. That's problem numero uno.
The next problem is that the definition of "dateConversion" returns "void" (line 182), which means it returns nothing.

This code should not compile. What compiler are you using?

Grrr, ok this is for a project im having some real trouble with. Im not sure if you have enough information on the programs purpose but I think the prof wants us to use a void function here and maybe change the actual contents of the array dateString thats being passed?

The program goes through a comma delimited file and the dateString needs to be converted from 10/30/2000 format to display October 30, 2000 to the user.

This article has been dead for over six months. Start a new discussion instead.