+ 2
The following code to solve Convert US date to EU date with c++, but I've logical error that give me some results wrong .
Can you help me to find the problem and solve it
4 ответов
+ 2
#include <iostream>
#include <cstring>
using namespace std;
//convert date US to EU
int main() {
string stri[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
char str[200];
char day[3],mou[20],yer[5],si;
int da=0,mo=0,ye=0,s=0;
cin.getline(str,99);
for (int i=0;i<strlen(str);i++){
if(str[i]=='/'||str[i]==' '||str[i]==','||str[i]=='.')
{ si=str[i];s++;continue;}
if (!s)
{mou[mo++]=str[i];}
if (s==1)
{day[da++]=str[i];}
if (s>1)
{yer[ye++]=str[i];}
}
char b='1';mo=0;
if (si!='/')
for (int i=0;i<12;i++,b++)
{if(mou==stri[i])
{mou[0]=mou[1]=mou[2]=mou[3]=mou[4]=mou[5]=mou[6]=mou[7]='\0';
if(i>8)
{mou[mo++]='1';
mou[mo]=(i>9? (i>10 ? '2':'1'):'0');}
else
mou[0]=b; break;}}
si='/';
cout<<day<<si<<mou<<si<<yer;
return 0;
}
+ 2
Thanks Ms Brian you are solve the problem ☺️
- 1
Shafiq Mohammed Almatri the most important problem is that the day, mou, and yer strings are not null terminated. An easy way to fix that is to initialize the char arrays to all zeros.
Change from:
char day[3],mou[20],yer[5],si;
To:
char day[3]{},mou[20]{},yer[5]{},si;
Beyond that, there are warnings that stem from poor formatting. The warnings go away if you make the code prettier with consistent indentation.