[Solved] looping through a character array c++ [closed]


you read only amount of characters that size variable specify,
since then , Why custNum function would not return true for anything longer than size variable ? , Because it’s not checking anything more than what size variable specify.

Below is the code you need

      #include <iostream>
#include <string>

using namespace std;
bool custNum(string,unsigned int);


int main()
{

    const unsigned int size = 8;
    //char custmor[size];

    string mystring;

    cout << "Enter a customer number in the form ";
    cout << "LLLNNNN\n";
    cout << "(LLL = letters and NNNN = numbers): ";
    cin >> mystring;

    cout << mystring <<endl << " " <<   mystring.length() << endl;
    // cin.getline(custmor, size);


     if(custNum(mystring , size))
        cout<<"That's a valid id number"<<endl;
    else
        cout<<"That's not a valid id number"<<endl;




        return 0;
}
bool custNum(string s, unsigned int size)
{
    unsigned int count;

    if (s.length() != (size + 1))
        return false;

    for(count = 0; count<3; count++)
    {

        if(!isalpha(s[count]))
            return false;
    }
    for(count = 3; count <size - 1; count++) //3<7 , 4
    {
        cout <<  s[count] <<endl;
        if(!isdigit(s[count]))
            return false;
    }
    return true;

}

17

solved looping through a character array c++ [closed]