title: Need help on reading a character from a string
body: <p>My program is supposed to ask the user to input their credit card number and then display whether the numbers are valid or invalid and if valid ,display the card type. I'm having trouble trying to read the first 2 characters in the cardNumber string in order to find out whether the card type is visa,mastercard,american express or discover (4 for visa, 5 for mastercard, 37 for american express, 6 for discover)</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  int main()
  {  
      string cardNumber; //this will carry the number given by the user.
      int sum(0), sumOfDoubleEven(0), sumOfOdd(0), evenDigit(0), oddDigit(0);
  
      cout &lt;&lt; "This program is the Credit Card Number Validator." &lt;&lt; endl;
      cout &lt;&lt; "This program will check the validity status of your card number" &lt;&lt; endl;
  
      //Main loop
      do
      {
          cout &lt;&lt; endl &lt;&lt; "Please provide a number to validate or type 'n' to quit ";
          cin &gt;&gt; cardNumber; //gets cardNumber from user
          if(cardNumber.length()&gt;=13 &amp;&amp; cardNumber.length()&gt;=16)
          {
              if (cardNumber == "n") break; //exits loop on user request
  
              //this for loop repeats once for each digit of the cardNumber.
              //digitPosition decrements by one on each pass from last position to first position
              for (int digitPosition=cardNumber.length(); digitPosition&gt;0; digitPosition--)
              {
                  if (digitPosition%2 == 0)
                  {   //executes the following code if digitPosition is even
                      oddDigit=(int)cardNumber[digitPosition-1]-'0';
                      sumOfOdd=sumOfOdd+oddDigit;
                  } else {   //executes the following code if digitPosition is odd.
                      evenDigit=((int)cardNumber[digitPosition-1]-'0')*2;
                      evenDigit=evenDigit%10+evenDigit/10;
                      sumOfDoubleEven=sumOfDoubleEven + evenDigit;
                  }
              }
  
              sum=sumOfOdd+sumOfDoubleEven; //sums the result
              cout &lt;&lt; endl &lt;&lt;"The number "&lt;&lt;cardNumber&lt;&lt;" you provided is ";
              if (sum%10==0) //executes if sum is divisible by 10
                  cout &lt;&lt; "valid" &lt;&lt; endl;
              else //executes if sum is not divisible by 10
                  cout &lt;&lt; "invalid" &lt;&lt; endl;
              if(cardNumber.at(0)==4) {
                  cout&lt;&lt;"Your Card type is VISA"&lt;&lt;endl;
              } else {
                  cout&lt;&lt;"Sorry, you've entered a card number not in the range of 13-16 digits" &lt;&lt;endl;
              } 
  
          }
      }while (cardNumber != "n");
  
      return 0;
  }
  </code></pre>