[Solved] Why is this wrong? C++ Analysing Date and Time


This is probably what you want:

if(pos != string::npos) 
{
    mystring = mystring.erase(0, 13); 
    int z = 0;
    for(int i = 0; i < mystring.Length(); i++)
    {
        if(isalpha(mystring[i])) z++;
    }
    if((int)mystring[0]-'0' > 3) z++;   //(int)mystring[0]-'0' converts it to int.
    if((int)mystring[3]-'0' > 1) z++;
    if(mystring[6] != '2') z++;        //No conversion needed for !=
    if(mystring[7] != '0') z++;
    if(mystring[8] != '1') z++;
    if(mystring[9] != '4') z++;
    if((int)mystring[11]-'0' > 2) z++;
    if((int)mystring[14]-'0' > 6) z++;

    if(z != 0) 
    {
     cout << "Please enter a valid Date & Time" << endl;
    }
}

(for-loop is better than one million ifs and changed to z++ instead of z+1)

solved Why is this wrong? C++ Analysing Date and Time