Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9226867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:55:54+00:00 2026-06-18T04:55:54+00:00

I am trying to parse input from a file to represent a standard deck

  • 0

I am trying to parse input from a file to represent a standard deck of cards (i.e. 2C represents the two of clubs). However, my solution is not working as expected, and is declaring all inputs to be invalid. I can’t see any logical errors in my code, so I wanted to get a second opinion. The code is below:

/*
 * Determines if the input string is valid.
 * 
 * A string is considered valid if it begins with either a number (2-10) 
 * or a letter (J/j, Q/q, K/k) to deetermine rank, followed by a letter to
 * determine suit (C/c, D/d, H/h, S/s).
 */
bool inputValidator(string cardData)
{
    if (cardData.length() == 2) //Input string is two characters long
    {
        if (cardData[0] < '2' || cardData[0] > '9'
            || cardData[0] != 'J' || cardData[0] != 'j'
            || cardData[0] != 'Q' || cardData[0] != 'q'
            || cardData[0] != 'K' || cardData[0] != 'k'
            || cardData[0] != 'A' || cardData[0] != 'a')
        {
            cout << "Card with data " << cardData << " has an invalid rank." << endl;
            return false;
        }
        if (cardData[1] != 'C' || cardData[1] != 'c' //Parse suit
            || cardData[1] != 'D' || cardData[1] != 'd'
            || cardData[1] != 'H' || cardData[1] != 'h'
            || cardData[1] != 'S' || cardData[1] != 's')
        {
            cout << "Card with data " << cardData << " has an invalid suit." << endl;
            return false;
        }
        return true;
    }
    else if (cardData.length() == 3) //Input string is three characters long
        //This occurs only if the number is 10.
    {
        if (cardData[0] != '1' || cardData[1] != '0') //Parse rank
        {
            cout << "Card with data " << cardData << " has an invalid rank." << endl;
            return false;
        }
        if (cardData[2] != 'C' || cardData[2] != 'c' //Parse suit
            || cardData[2] != 'D' || cardData[2] != 'd'
            || cardData[2] != 'H' || cardData[2] != 'h'
            || cardData[2] != 'S' || cardData[2] != 's')
        {
            cout << "Card with data " << cardData << " has an invalid suit." << endl;
            return false;
        }
        return true;
    }
    return false;
}

If there are any logical flaws (or an inherently better way to do this), I would appreciate being told. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:55:55+00:00Added an answer on June 18, 2026 at 4:55 am

    You’re writing clauses like this:

    cardData[2] != 'D' || cardData[2] != 'd'
    

    Which will always be true, as the variable being tested can’t be both values at the same time. You probably meant to use && rather than ||.

    You could certainly simplify the logic, for example by converting the input to lower or upper case before comparing it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to extract all input elements from a form. When I parse the
Note, I am not trying to generate the schema for an XML file from
I'm trying to get some data from a file, then parse it and pass
I'm trying to parse an xml file and extract data from it to display
I am trying to create a json file from an input xml file using
So I'm trying to parse a config.json file from the REPL. The file is:
I am trying to parse the input from a midi device connected to my
I am trying to parse a CSV file generated from an Excel spreadsheet. Here
I am trying to read from a text file and tokenize the input. I
I'm trying to extract two blocks of code from a text file using Java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.