C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 5th, 2008, 09:22 PM
!makes_sense !makes_sense is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 7 !makes_sense User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 29 m 19 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old May 7th, 2008, 04:01 AM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Dec 2007
Posts: 1,177 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 21 h 27 m 36 sec
Reputation Power: 12
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
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.

Reply With Quote
  #3  
Old May 7th, 2008, 09:08 PM
!makes_sense !makes_sense is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 7 !makes_sense User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 29 m 19 sec
Reputation Power: 0
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?

Reply With Quote
  #4  
Old May 14th, 2008, 04:15 AM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Dec 2007
Posts: 1,177 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 21 h 27 m 36 sec
Reputation Power: 12
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
Basically, isdigit is declared along the lines of
bool isdigit(char);
Pretty obvious, it returns true when the char parameter is a digit. You've learned function calls, right?
I'm not so sure what includes you may have to use.

Each string character is of the type char. When you write myString[2] the result of that expression is of type char. A char is basically an 8-bit integer as far as comparing it goes.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > General - ISBN Validation program


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap