2016-04-28 51 views
-1

我正在编写一个程序,该程序输入日期格式并以不同格式输出相同日期。到目前为止,我只在程序的前半部分工作过,但我被卡住了。我不断收到这2个错误。错误C4703和错误4716

*错误C4703:使用可能未初始化的本地指针变量'month1'。

*错误C4716:'numDate'必须返回一个值。

#include<stdio.h> 

int numDate(); 
void wrdDate(); 

int main() 
{ 

int a; 

printf("Write a date to have it converted to an alternate format. You can write your date\n"); 
printf("in one of two ways. Either in purely numeric form, ex(09/18/2016), or a complete written\n"); 
printf("out form, ex(September 18, 2016). Enter one of those formats and receive the other one in return."); 
printf("If you wish enter a 09/18/2016 format, enter 1\n"); 
printf("If you wish to enter a September 18, 2016 format, enter 2.\n"); 
scanf_s("%d", &a); 

if (a == 1) 
{ 
    numDate(); 
} 

if (a == 2) 
{ 
    wrdDate(); 
} 
} 
int numDate() 
{ 

int day, month, year; 
int day1; 
char* month1; 
int year1; 
printf("Enter date (dd/mm/yy): "); 
scanf_s("%d/%d/%d", &month, &day, &year); 

day1 = day; 
year1 = year; 

if (day < 0) 
{ 
    if (month == 1) 
     month1 = "January"; 
    else if (month == 2) 
     month1 = "February"; 
    else if (month == 3) 
     month1 = "March"; 
    else if (month == 4) 
     month1 = "April"; 
    else if (month == 5) 
     month1 = "May"; 
    else if (month == 6) 
     month1 = "June"; 
    else if (month == 7) 
     month1 = "July"; 
    else if (month == 8) 
     month1 = "August"; 
    else if (month == 9) 
     month1 = "September"; 
    else if (month == 10) 
     month1 = "October"; 
    else if (month == 11) 
     month1 = "November"; 
    else if (month == 12) 
     month1 = "December"; 
} 


day1 = day; 
year1 = year; 

printf("%s %d, %d,", month1, day1, year1); //It says Error 4703 is happening here 

} 

int wrdDate() 
{ 

} 
+0

注意:'month == 01'和'month == 1'在编译后具有完全相同的含义。所以'month == 02'和'month == 2'等等 – MikeCAT

+1

''numDate'必须返回一个值'应该是非常明显的。你告诉编译器:'numDate'将返回一个'int',但不会返回任何东西。 – Michael

+0

为什么不使用数组(在这种情况下更好)或'switch'语句而不是许多'if'语句? – MikeCAT

回答

3

错误C4703:用于未初始化的潜在局部指针变量 'MONTH1'。

正如此消息所述,当month < 1 || 12 < month时,month1未初始化。通过改变

char* month1; 

喜欢的东西

const char* month1 = "(unknown month)"; 

注意,指针从字符串字面量转换被分配到month1初始化它,你不能修改字符串常量,因此使用const char*char*更好。

错误C4716:'numDate'必须返回一个值。

numDate返回类型是int,所以该函数必须返回的int的值。如果您不想返回任何值,请将返回类型更改为void。请注意,您将不得不更改声明和定义。