The output of k is 540 when it should be 12 with the input 12:45:54PM could you explain on the mistake of the code below

Viewed 14

This problem is about time conversion of a 12hr clock to 24hr clock.

int main()
{ string s;
int i, k;
getline(cin,s);//got the input
vector <char> a(s.begin(),s.end());//copying them character wise in a vector
 k =(a[0]*10)+(a[1]*1);//to check with the value less than or greater than 12
 cout<<k<<" ";
if((a[8]=='P') && ( k != 12))
{ a.at(0) =1;
  a[1] = a[1]+2;    
} 
else if (k==12 && a[8]=='A')
{ a[0]=0;
 a[1]=0;    
}
}
0 Answers
Related