Using a string to verify a part number

I'll start off by admitting this is a homework question- I am not asking anyone to do it for me, just to point me in the right direction because I've been reading my book for hours trying to figure out where to start, and I am absolutely, completely lost.
The assignment is to have the user enter a part number in which each digit has a different criteria- the first digit needs to be A, B, or C, the second and third digit need to sum to a number between 10 and 49, and so on. The professor's special instructions read "you MUST use an array for this question - the part number should be declared as a string - check the length of the string first - if that is correct than you access the parts of the string as array elements - that is you use subscripts - test each string part to see it it meets the conditions in the question"
I guess my real question is that I'm wondering if someone can rephrase those instructions for me, because I've read them over and over again and I just can't figure out what I'm supposed to do. Something about it just isn't registering for me. Again, please don't just do it for me or tell me exactly how to do it... I'm not looking to get into plagiarism, I just need another perspective on what it is my professor wants.
Thanks for any input.
Oh, and the point of the program would be to tell the user whether or not they have entered a valid part number that meets the specified criteria for each digit. I left that part out, not sure whether it was obvious or not.
closed account (48T7M4Gy)
I'd be asking the prof for a sample part number. The specification you have above doesn't make sense.

The first digit is A, B or C more or less makes sense. They aren't digits but there you go.

The second two digits sum to a number between 10 and 49 is gibberish. The total of two digits ranges from 0 (0+0) to 18 (9+9).

The specification says use a string then an array which is confused to say the least. The elements of a c-string or a string can be accessed by indexation, maybe. Or maybe the spec means the components are stored, not as a continuous string but as two separate elements in an array.

Assuming your rendition is an accurate summary of the original question, I'd be inclined not to waste any more time on it but approach the prof and ask for clarification and an example of a part number, at least. Hopefully that would be forthcoming. But if it's not then a formal complaint to the head of school wouldn't be unreasonable.
Last edited on
I was wrong in using the word "digit" I guess. Sorry, I'm tired. The correct word is "character", not "digit". I just don't understand the part in quotation marks, where the professor was telling me how to do the assignment. None of that part makes any sense to me, no matter how hard I try.
Last edited on
closed account (48T7M4Gy)
No problem I was blaming the prof for the character/digit. Take it easy - hopefully you aren't too close to the deadline that it can't wait til you ask. BTW, once you have clarity the validation of the part number should be fairly straight forward. Cheers

PS Maybe somebody else will chip in here with ideas. I'll be interested to know what the prof actually wants.
Last edited on
I emailed her, but it's due in like five hours so I'm not super optimistic. I was having issues with my computer and couldn't start the assignment when I wanted to, so this has been an all-nighter. I couldn't even find a computer on-campus to use, I definitely tried. At least this is the only question I need clarification on, and the only one left I haven't finished.
closed account (48T7M4Gy)
but it's due in like five hours so I'm not super optimistic.


When in doubt improvise and if you have a customer who is seeking leadership then make a guess at it and take the initiative.

Use a string:
- string is of the form X-XX-XX
- first element is A, B or C
- second element is an integer say X
- second element is an integer say Y

Validation:
1. must be capital A,B, or C not lowercase
2. X and Y are integers
3. 10 <= X+Y <= 49
4. must be of the right form and right length

Make an offer the pprof can't refuse.

Cheers

I'm not sure if this is asking too much, but can I see an example of a string used in a similar context? This is the first time I've ever used strings and I feel like that's a big part of my confusion. I've been looking up a lot of examples online but they're all so simple and I just can't figure out how to convert them into this context.
Also, thanks for the encouragement- I was kind of starting to give up a little but now I'm watching videos about strings trying to see if that might help.
closed account (48T7M4Gy)
OK, it will take 10 minutes but I'll give you a sample to show you what I mean but not for plagiaristic purposes. I can't guarantee of course it is what your prof is looking for.
Thanks- I really don't intend for it to be plagiaristic. I'm doing this with the idea that my professor might wander across it online if she checks our work for plagiarism. So please make it something different enough from the assignment that I literally have to write my own code, so that if she came across this thread it wouldn't be misconstrued as plagiarism.
closed account (48T7M4Gy)
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
#include <iostream>
#include <string>

int main()
{
   string partNo;
   bool valid = false;

   std::cout << "Please enter a part number: ";
   std::cin >> partNo;

   if (partNo.length() != 7 || ( partNo[0] != 'A' ||  partNo[0] != 'B' ||  partNo[0] != 'C' ))
      valid = false;
   else
      valid = true;

   if(valid == true)
      std::cout << partNo << " is a ";
   else
      std::cout << partNo <<  " is not a ";

   std::cout << "valid part number." << std::endl;

   return 0;
}


Last edited on
Thanks... I was starting to figure it out with Youtube videos, and this really helps me confirm that I'm on the right track and it helped me notice a couple mistakes I was making.
closed account (48T7M4Gy)
I know it's tricky but it would only be plagiarism if you copied it and didn't extend it beyond just the simple tests above which aren't anywhere near complete. Also there's nothing wrong in this.

Another added way of describing the part no. is that instead of one string it is made up of 3 substrings in an array which are separated by '-''s when they are printed out. That is fairly cumbersome to manage but it can be done.

Well, I don't want your endeavours to be misunderstood so I'll leave it at that.

Cheers and good luck with it.
If there are a lot of criteria for validating the string then you might find it easier to code that part as a function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
bool isValidPart(const string &partNo)
{
    if (partNo[0] != 'A' && partNo[0] != 'B') {
        return false;
    }
    if (next criteria fails) {
       return false;
    }
    ...
    // If you get here than all criteria succeed
    return true;
}

int main()
{
    string partNo;
    // get the partNo
    if (is ValidPart(PartNo)) {
        std::cout << partNo << " is valid\n";
    } else {
        std::cout << partNo << " is not valid\n";
    }
}

closed account (48T7M4Gy)
True but I think we should be mindful of the potential for plagiarism
Topic archived. No new replies allowed.