| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
General - ISBN Validation program
Greetings to all members,
I have to write a C++ program which requires that user enters ISBN and the program validates it. The ISBN can be 13 characters long or 10 characters long to be valid in the first place. I have implemented a check for the length of these isbn by declaring the isbn as string data type and checking their lengths. Another major part of the validation program is to determine the checksum. The tenth digit in the isbn is the checksum which is calculated by first multiplying all the 9 digits by their respective positions in the string and then adding them altogether. Finally, taking the modulus of the sum by 11. What I do not know is how to implement this checksum. I think that I have to convert the each string character to integer and then do the arithmetic. But how do I do that. What are the commands for converting string characters to respective integers. Finally, how do I check that the user has not enter a bad digit such as 0821A11315 when it should be 0821211315 and how to check for sequential dashes such as 157231--866-x. Any thoughts on how to solve these problems. Thank you. |
|
#2
|
|||
|
|||
|
First of all, you need to figure out how to address each char.
Given string isbn; cin >> isbn; then you can access the first character of the user input by isbn[0] the second by isbn[1] and so on, up to isbn[isbn.size()-1]. Once you do that you have the char value, which can be fed to functions like isdigit(), compared to '-', or be "converted" to its (ascii, usually) numeral value (Warning! '1' != 1) by storing it to an int. If you need the numeral value, you can either use lexical_cast from the boost libraries, use atoi() in some fashion, or perhaps make your own function. Note that a switch(c) { case '1': return 1; ...} is probably the only way that is truly portable, without assuming that the ascii charset is used. These tools should be enough to do what you need to. |
|
#3
|
|||
|
|||
|
Thanks for the input.
I know how to access the string characters but I don't know how to do isdigit (), compare to '-'; Can you be more specific how to do these? |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > General - ISBN Validation program |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|