我正在为我的课程编写课程。我是C ++的新手。我们尝试做的是在执行时提供用户提供的年,月和日参数,并计算它们与当前日期之间的差异。
在我的main函数中,我有以下代码从用户那里获取这些参数:
int main(int argc, char** argv)
我的理解是,这会创建一个字符数组并将用户的参数存储在其中。接下来,我将存储在此数组中的参数分配给新的char变量,以便我可以将它们传递给函数,如下所示:
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
}
argumentChange
是我发送给他们的功能。问题是当我尝试编译时,我收到以下错误:
错误:来自' char *'的转换无效去#char;' [-fpermissive]
charArg1 = argv [1];
错误:来自' char *'的转换无效去#char;' [-fpermissive]
charArg3 = argv [3];
我已经尝试过搜索这个问题,但我无法对其他地方的解释做出正面或反面的讨论。我已经看到很多人提到"指针",我们还没有在课堂上报道,而我对此一无所知。我做错了什么?我如何使这项工作?
以下是我整个计划的完整代码:
#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;
}
答案 0 :(得分:1)
正如所指出的,argv [x]的类型是char*
,它是指向字符数组的指针。所以char**
是指向数组数组的指针。 (真的*并不自动意味着它是指向数组的指针,在这种情况下就是这样)
// argv is an array of char*'s
int main(int argc, char** argv)
// firstParam is an array of char's
char* firstParam = argv[1];
// firstLetter is the first char of the array
char firstLetter = firstParam[0]
实际上,您应该跳过所有char
数组,然后使用std::string
&#39}。例如
int main(int argc, char** argv) {
std::string param = argv[1];
if (param == "january") {
// Something ...
}
}
您还应该检查传入的参数数量,因此您无法访问数组范围之外的内容。这是argc
的用途。
答案 1 :(得分:0)
在T
类型中存储一堆变量时,我们可能会有dynamic array
来存储它。通常我们使用指向数组的指针,其类型为T*
以int
为例:
int* ary1 = new int[3];
ary1[0] = 10; // ary1 + 0 store 10, a int
ary1[1] = 11; // ary1 + 1 store 11, a int
ary1[2] = 12; // ary1 + 2 store i2, a int
与char*
字符串
char** argv= new char*[4];
argv[0] = "cl.exe" // ary2 + 0 stores "cl.exe", a `char*` string
argv[1] = "2018"" // ary2 + 1 stores "2018" , a `char*` string
argv[2] = "3" // ary2 + 2 stores "3" , a `char*` string
argv[3] = "16" // ary2 + 3 stores "16" , a `char*` string
因此,您无法将argv[1]
,(char*
字符串)分配给arg1
,(char
字符)。