Pointing to first character in a string using an if statment

I'm attempting to accept a users input for their date of birth in the format of MMDDYYYY as a string, then using an if statement to evaluate the string for a leading zero and remove it so I can convert the string to an int. Once it's an int I can perform math on it to calculate a persons age.

I've come up with this code after researching, and it is erasing the first character of the string, but it's always removing the first character even if the first character isn't a zero. Can anyone give me an idea of what I'm doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <string>
#include <cctype>



using namespace std;



int main( ) 
{
    int patientDateOfBirth;
    string patientDOB;

cout << "Enter patients DOB: ";
cin  >> patientDOB;
    if(patientDOB[0] = '0')
    {
    patientDOB.erase(0,1);
    cout << patientDOB << endl;
    }
    else
    {
    cout << patientDOB;
    }
    return 0;
}
closed account (SECMoG1T)
Line 20

1
2
3
4
5

if(patientDOB[0] = '0')//you assigned a '0' to the first character

if(patientDOB[0] == '0')/// this is what you wanted
Last edited on
Topic archived. No new replies allowed.