I'm working on an assignment for my programming I class. I'm brand new to C++. What we're trying to do is take year, month, and day arguments provided by the user at the time of execution and calculate the difference between them and the current date.
In my main function, I have the following code to grab those arguments from the user:
int main(int argc, char** argv)
My understanding is that this creates a character array and stores the user's arguments in it. Next, I am assigning the arguments stored in this array to new char variables so that I can pass them to a function, like so:
int main(int argc, char** argv)
{
int year, month, day;
char arg1 = argv[1];
string arg2 = argv[2];
char arg3 = argv[3];
argumentChange(argc, arg1, arg2, arg3, year, month, day);
blablabla other code
}
Where argumentChange
is the function I'm sending them to. The issue is when I try to compile this, I receive the following errors:
Error: invalid conversion from 'char*' to 'char' [-fpermissive]
charArg1 = argv[1];
Error: invalid conversion from 'char*' to 'char' [-fpermissive]
charArg3 = argv[3];
I've tried searching this issue, and I can't really make heads or tails of the explanations given elsewhere. I've seen a lot of people mention "pointers", which we haven't yet covered in class and which I know nothing about. What have I done wrong? How do I make this work?
Here is the complete code for my entire program:
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
void argumentChange(int numArg, char chArg1, string sArg2, char chArg3, int year, int month, int day)
{
cout << "Starting birth date: " << chArg1 << " " << sArg2 << " " << chArg3
<< endl;
if (numArg < 4 || numArg > 4)
{
cout << "Correct Usage: ./cl <birth_year><birth_month><birth_day>" << endl;
cout << "Try again, dude." << endl;
}
else if (numArg == 4)
{
year = chArg1 - '0';
day = chArg3 - '0';
if ((sArg2 == "january") || (sArg2 == "January"))
{
month = 0;
}
else if ((sArg2 == "february") || (sArg2 == "February"))
{
month = 1;
}
else if ((sArg2 == "march") || (sArg2 == "March"))
{
month = 2;
}
else if ((sArg2 == "april") || (sArg2 == "April"))
{
month = 3;
}
else if ((sArg2 == "may") || (sArg2 == "May"))
{
month = 4;
}
else if ((sArg2 == "june") || (sArg2 == "June"))
{
month = 5;
}
else if ((sArg2 == "july") || (sArg2 == "July"))
{
month = 6;
}
else if ((sArg2 == "august") || (sArg2 == "August"))
{
month = 7;
}
else if ((sArg2 == "september") || (sArg2 == "September"))
{
month = 8;
}
else if ((sArg2 == "october") || (sArg2 == "October"))
{
month = 9;
}
else if ((sArg2 == "november") || (sArg2 == "November"))
{
month = 10;
}
else if ((sArg2 == "december") || (sArg2 == "December"))
{
month = 11;
}
else
{
cout << "Error: You have entered an invalid term for month. Please type ";
cout << "the complete name of a valid month." << endl;
}
}
}
struct tm bday(int year, int month, int day)
{
struct tm r {0};
r.tm_year = year - 1900;
r.tm_mon = month;
r.tm_mday = day;
return r;
}
int main(int argc, char** argv)
{
int year, month, day;
char arg1 = argv[1];
string arg2 = argv[2];
char arg3 = argv[3];
argumentChange(argc, arg1, arg2, arg3, year, month, day);
struct tm a = bday(year, month, day);
time_t x = mktime(&a);
time_t y = time(0);
if ( x != (time_t)(-1) && y != (time_t)(-1) )
{
double difference = difftime(y, x) / (60 * 60 * 24);
cout << ctime(&x);
cout << ctime(&y);
cout << "difference = " << difference << " days" << endl;
}
return 0;
}