Assure correct string input in C++

610 views Asked by At

I'm trying to assure that my input string is correctly formated (correct format XXX-X-XXX ),but I don't wan't to use char temp[255] and cin.getline combination.

So far I've managed to "catch" all exceptions besides this one:

Enter registration plate: shjkf 22h 23jfh3kfh jkfhsdj h2j h2k 123-A-456

As assumed regPlate will get all the strings from the input including the correct formated one at the end and it will print the string.That's not correct. After reading the first string it should print Bad input and everything after it needs to be deleted.

I've tried to use cin.clear(),cin.ignore(),etc. in my if function but with no results.

void main()
{
    std::string regPlate;
    do
    {
        cout << "Enter registration plate: ";
        cin >> regPlate;
        if (regPlate.size() < 9 || regPlate.at(3) != '-' || regPlate.at(5) != '-' || regPlate.size() > 9)
        {
            cout << "Bad entry" << endl;

        }
    } while (regPlate.size() < 9 || regPlate.at(3) != '-' || regPlate.at(5) != '-' || regPlate.size() > 9);
    cout << endl << regPlate << endl;

    system("pause");
}
3

There are 3 answers

0
Phantom Photon On

Would a while loop work, maybe something like:

int main(){
    string regPlate;
    while(regPlate.size() != 9 || regPlate.at(3) != '-' || regPlate.at(5) != '-'){
        cout << "Enter registration plate: " << endl;
        cin >> regPlate;
    }
    cout << regPlate;
    return 0;
}
1
Trevor On

Using the example you provide (with a few assumptions) I ran your code an it seems to work as expected. Here's what I ran, with the headers included.

#include <string>
#include <iostream>

using namespace std;

void main()
{
   string regPlate;
   do
   {
       cout << "Enter registration plate: ";
       cin >> regPlate;
       if (regPlate.size() != 9 || regPlate.at(3) != '-' || regPlate.at(5) != '-')
       {
          cout << "Bad entry" << endl;

       }
   } while (regPlate.size() != 9 || regPlate.at(3) != '-' || regPlate.at(5) != '-');
   cout << endl << regPlate << endl;

   system("pause");
}

The output I receive is:

Enter registration plate: shjkf 22h 23jfh3kfh jkfhsdj h2j h2k 123-A-456
Bad Entry
Enter registration plate: Bad Entry
Enter registration plate: Bad Entry
Enter registration plate: Bad Entry
Enter registration plate: Bad Entry
Enter registration plate: Bad Entry
Enter registration plate: Bad Entry
Enter registration plate: 
123-A-456

I also manually typed out all of the values your listed, and it appeared to work that way also.

0
Dragan Zrilić On

Thanks to @Someprogrammerdude I've managed to fix the problem.

Only thing that I've had to do is instead of std::cin>>regPlate to use std::getline(cin,regPlate)

std::string regPlate;
    do
    {
        cout << "Enter registration plate: ";
        std::getline(cin, regPlate);
        if (regPlate.size() != 9 || regPlate.at(3) != '-' || regPlate.at(5) != '-')
        {
            cout << "Bad entry" << endl;

        }
    } while (regPlate.size() != 9 || regPlate.at(3) != '-' || regPlate.at(5) != '-');
    cout << regPlate << endl;