user1311854
user1311854

Reputation: 41

Using atoi and error :unresolved external symbol

I have been working on this code for my class and I am just not sure what the errors mean or how to fix them. Also, I am not sure what the next step is or how I can finish the program. I have only been using C++ for a month and I am not very familiar with any of it. Thank you in advance!

error LNK2019: unresolved external symbol "int __cdecl parseDate(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?parseDate@@YAHV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main
: fatal error LNK1120: 1 unresolved externals

My assignment is:
A parking garage charges a $2.00 minimum fee to park for up to three hours. The garage charges an additional $0.50 per hour for each hour or part thereof in excess of three hours. The maximum charge for any given 24-hour period is $10.00. People who park their cars for longer than 24 hours will pay $8.00 per day.
Write a program that calculates and prints the parking charges. The inputs to your program are the date and time when a car enters the parking garage, and the date and time when the same car leaves the parking garage. Both inputs are in the format of: YY/MM/DD hh:mm

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
#include <algorithm>
#include <sstream>
using namespace std;

string startDateString;
string endDateString;
string dateStr;
int parseDate( string dateStr );

int main ()

{

string enter_date;
string enter_time;
string exit_date;
string exit_time;
cout << "Please enter the date and time the car is entering "<< endl
    << "the parking garage in the following format: YY/MM/DD hh:mm"<< endl;
getline (cin,dateStr);//cin >> enter_date >> enter_time;

cout<< "Please enter the date and time the car is exiting "<< endl
    << "the parking garage in the following format: YY/MM/DD hh:mm"<< endl;
getline (cin,dateStr);//cin >> exit_date >> exit_time;


{
    // Format: YY/MM/DD hh:mm
    int year  = atoi( dateStr.substr( 0, 2 ).c_str() );
    int month = atoi( dateStr.substr( 3, 2 ).c_str() );
    int day   = atoi( dateStr.substr( 6, 2 ).c_str() );
    int hour  = atoi( dateStr.substr( 9, 2 ).c_str() );
    int min   = atoi( dateStr.substr( 12, 2 ).c_str() );

    // Now calculate no. of mins and return this
    int totalMins = 0;
    totalMins += ( year * 365 * 24 * 60 ); 
    totalMins += ( month * 30 * 24 * 60 ); 
    totalMins += ( day * 24 * 60 );        
    totalMins += ( hour * 60 );
    totalMins += ( min );

    return totalMins;
}
int startTime = parseDate( startDateString );
int endTime   = parseDate( endDateString );
int elapsedTime = endTime - startTime; // elapsedTime is no. of minutes parked

return 0;
}

Upvotes: 1

Views: 872

Answers (1)

sparc_spread
sparc_spread

Reputation: 10833

It looks like you never defined parseDate() separately, but instead mooshed it inside your main(). I think you need to take out:

{
    // Format: YY/MM/DD hh:mm
    int year  = atoi( dateStr.substr( 0, 2 ).c_str() );
    int month = atoi( dateStr.substr( 3, 2 ).c_str() );
    int day   = atoi( dateStr.substr( 6, 2 ).c_str() );
    int hour  = atoi( dateStr.substr( 9, 2 ).c_str() );
    int min   = atoi( dateStr.substr( 12, 2 ).c_str() );

    // Now calculate no. of mins and return this
    int totalMins = 0;
    totalMins += ( year * 365 * 24 * 60 ); 
    totalMins += ( month * 30 * 24 * 60 ); 
    totalMins += ( day * 24 * 60 );        
    totalMins += ( hour * 60 );
    totalMins += ( min );

    return totalMins;
}

And put it in a separate function at the end of your code:

int parseDate (string dateStr) 
{
    // Format: YY/MM/DD hh:mm
    int year  = atoi( dateStr.substr( 0, 2 ).c_str() );
    int month = atoi( dateStr.substr( 3, 2 ).c_str() );
    int day   = atoi( dateStr.substr( 6, 2 ).c_str() );
    int hour  = atoi( dateStr.substr( 9, 2 ).c_str() );
    int min   = atoi( dateStr.substr( 12, 2 ).c_str() );

    // Now calculate no. of mins and return this
    int totalMins = 0;
    totalMins += ( year * 365 * 24 * 60 ); 
    totalMins += ( month * 30 * 24 * 60 ); 
    totalMins += ( day * 24 * 60 );        
    totalMins += ( hour * 60 );
    totalMins += ( min );

    return totalMins;
}

Upvotes: 4

Related Questions