943,753 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 842
  • C++ RSS
Oct 27th, 2008
0

Array and data type help

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. An ISBN (International Standard Book Number) identifies a unique publication. An ISBN is ten digits. The first nine digits must be decimal digits (0...9). The tenth may a decimal digit or the letter X, according to the checksum, discussed below. Three single dashes may be between any of the characters, but an ISBN must not begin or end with a dash.
  2.  
  3. Some example ISBNs:
  4.  
  5. 0-201-88337-6
  6. 0-13-117334-0
  7. 0821211315 (no dashes ok)
  8. 1-57231-866-X
  9.  
  10. The last character of an ISBN number is a checksum. The checksum is the determined by the first 9 digits; it is computed by taking modulo 11 (the remainder after dividing by 11) of the sum of each digit multiplied by its position in the ISBN. The letter X corresponds to a value of 10.
  11.  
  12. Here are two ISBNs and the calculations that show how the check sum is determined:
  13.  
  14. 0-201-88337-6 -> (0*1 + 2*2 + 0*3 + 1*4 + 8*5 + 8*6 + 3*7 + 3*8 + 7*9) mod 11 = 6
  15. 1-57231-866-X -> (1*1 + 5*2 + 7*3 + 2*4 + 3*5 + 1*6 + 8*7 + 6*8 + 6*9) mod 11 = 10 (X)
  16.  
  17. For more info, check out:
  18. www.isbn.org
  19. www.amazon.com Try a book search by ISBN
  20.  
  21. Some invalid ISBNs:
  22.  
  23. 0-201-8A337-6 (bad digit)
  24. 0-201-88337-63 (too many digits)
  25. 0-201-88-337-6 (too many dashes)
  26. 0-201883376 (not enough dashes)
  27. 0-201-88337-3 (wrong check sum)
  28. -013-117334-0 (beginning or ending dash)
  29. 157231--866-X (sequential dashes)
  30. 013-1134-0 (too few digits)
  31.  
  32. Write a menu driven program that will verify ISBNs either from user input or from file input (use default filename = isbntest.txt). The file format should be:
  33.  
  34. integer value that specifies the number of ISBN's to follow (max of 20),
  35.  
  36. ISBN, (one ISBN per line)
  37.  
  38. ISBN, etc.
  39.  
  40. Example data file:
  41.  
  42. 6
  43.  
  44. 0-201-88337-6
  45.  
  46. 0-13-117334-0
  47.  
  48. 157231--866-X
  49.  
  50. -013-117334-0
  51.  
  52. 0821211315
  53.  
  54. 1-57231-866-X
  55.  


can somebody simplify for me what the problem is and some advise to solve it because of my English, I don't really know what itreally means.thank you
Similar Threads
Reputation Points: 3
Solved Threads: 0
Junior Poster
Se7Olutionyg is offline Offline
108 posts
since Oct 2008
Oct 27th, 2008
0

Re: Array and data type help

Write a parsing program (something that splits a bigger data type ie. a ISBN number to a more manageable datatype ie. an array of char[]). Load in the ISBN number from the file, and make sure it doesn't contain more than 3 dashes (-), that it only contains numbers (other than dashes and possible X checksum value) and that it doesn't start or end with a dash. Then, if all those checks pass, make sure the final digit is the proper checksum (using the method you described). I'm not sure how much clearer this can be made!
Last edited by skatamatic; Oct 27th, 2008 at 1:13 am.
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
775 posts
since Nov 2007
Oct 27th, 2008
0

Re: Array and data type help

Ok, The programm has to read a string to an array, check if there is no more then 3 deshis, check if first 9 characters excluding the deshis are numbers, make shure there is 9 numbers and the 10th digit is eather a number or "x", after then multiplay firs 9 mumerical didgit by its position, and add the result, now the result devide by 11 and get reminder, it should be equal to the last 10th digit of the number.. if reminder is 10 the 10th digit will suppost to be 'X', if all of thous conditions are true, the number is correct, the other way it is incorrect

hope this helps, if not ask further
Reputation Points: 46
Solved Threads: 0
Light Poster
uim_1977 is offline Offline
25 posts
since Oct 2008
Oct 27th, 2008
0

Re: Array and data type help

Click to Expand / Collapse  Quote originally posted by uim_1977 ...
Ok, The programm has to read a string to an array, check if there is no more then 3 deshis, check if first 9 characters excluding the deshis are numbers, make shure there is 9 numbers and the 10th digit is eather a number or "x", after then multiplay firs 9 mumerical didgit by its position, and add the result, now the result devide by 11 and get reminder, it should be equal to the last 10th digit of the number.. if reminder is 10 the 10th digit will suppost to be 'X', if all of thous conditions are true, the number is correct, the other way it is incorrect

hope this helps, if not ask further
I don't think your spelling is going to help his understanding of English much better >.<
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
775 posts
since Nov 2007
Oct 27th, 2008
0

Re: Array and data type help

yes i do have problems with the spelling in English, but didn't you understand what i was trying to say ?
Reputation Points: 46
Solved Threads: 0
Light Poster
uim_1977 is offline Offline
25 posts
since Oct 2008
Oct 27th, 2008
0

Re: Array and data type help

Click to Expand / Collapse  Quote originally posted by uim_1977 ...
yes i do have problems with the spelling in English, but didn't you understand what i was trying to say ?
Yes, but he may need to translate words, and incorrect spelling will give him nothing but grief.
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
775 posts
since Nov 2007
Oct 28th, 2008
0

Re: Array and data type help

can you show me the chart how to do it please, or some ideas or some hints
Reputation Points: 3
Solved Threads: 0
Junior Poster
Se7Olutionyg is offline Offline
108 posts
since Oct 2008
Oct 28th, 2008
0

Re: Array and data type help

Write a parsing program (something that splits a bigger data type ie. a ISBN number to a more manageable datatype ie. an array of char[]). Load in the ISBN number from the file, and make sure it doesn't contain more than 3 dashes (-), that it only contains numbers (other than dashes and possible X checksum value) and that it doesn't start or end with a dash. Then, if all those checks pass, make sure the final digit is the proper checksum (using the method you described). I'm not sure how much clearer this can be made!

hmmm well have a function similar to this (im not going to right main or any declarations)

C++ Syntax (Toggle Plain Text)
  1. bool Parse(char const * szString)
  2. {
  3. bool bFail(false), bWasDash(false);
  4. int checksum(0), realchksum(0), dashes(0);
  5. for (int i(0); i < strlen(szString); i++)
  6. {
  7. if (szString[i] != '-')
  8. bWasDash = false;
  9. if (isdigit(szString[i]))
  10. checksum += szString[i] - 30; //numbers start at 30 on ascii chart
  11. else if (i == strlen(szString) && tolower(szString[i]) == 'x')
  12. realchksum = 10;
  13. else if (i == strlen(szString) && isdigit(szString[i]))
  14. realchksum = szString[i] - 30;
  15. else if (((i == 0 || i == strlen(szString) || bWasDash) && szString[i] == '-') || isalpha(szString[i]))
  16. {
  17. bFail = true;
  18. break;
  19. }
  20. else if (szString[i] = ='-')
  21. {
  22. bWasDash = true;
  23. dashes++;
  24. }
  25. //add more checks here i dont know/care if i got them all
  26. }
  27.  
  28. //now check the checksum
  29. if (checksum != realchksum)
  30. bFail = true;
  31. return bFail; //returns true if its a bad number
  32. }

I don't know if this will actually run, i didnt compile it. And it probably doesn't cover all of the checks. And you'll have to figuire out the file i/o part - since that's pretty elementary. I really don't like writing whole functions for people, but since you don't understand english very well, maybe you'll understand code better.
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
775 posts
since Nov 2007
Oct 28th, 2008
0

Re: Array and data type help

Here is a pretty crappy version of a more dynamic solution. It would make more sense to make the array internal to the class, but it still demonstrates my point

C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. using namespace std;
  3.  
  4.  
  5. class Numbers
  6. {
  7. int iValue;
  8. int iCount;
  9. public:
  10. inline bool bExists (Numbers * numarray, int iSize, int iVal)
  11. {
  12. for (int i(0); i < iSize; i++)
  13. if (numarray[i].iValue == iVal)
  14. return true;
  15. return false;
  16. }
  17. inline int GetCount (Numbers * numarray, int iSize, int iVal)
  18. {
  19. for (int i(0); i < iSize; i++)
  20. if (numarray[i].iValue == iVal)
  21. iCount++;
  22. return iCount;
  23. }
  24. inline int GetIndex (Numbers * numarray, int iSize, int iVal)
  25. {
  26. for (int i(0); i < iSize; i++)
  27. if (numarray[i].iValue == iVal)
  28. return i;
  29. return -1;
  30. }
  31. inline void AddNum (Numbers * numarray, int iSize, int iVal)
  32. {
  33. if (!bExists(numarray, iSize, iVal))
  34. numarray[GetFreeSpot(numarray, iSize)].iValue = iVal;
  35. else
  36. numarray[GetIndex(numarray, iSize, iVal)].iCount++;
  37. }
  38. inline int GetFreeSpot(Numbers * numarray, int iSize)
  39. {
  40. return GetIndex(numarray, iSize, 0);
  41. }
  42. inline int GetNum ()
  43. {
  44. return iValue;
  45. }
  46. Numbers(void): iValue(0), iCount(0) {}
  47. ~Numbers(void) {};
  48. };
  49. int main()
  50. {
  51. Numbers * pNums;
  52. int iSize(0);
  53. int iNum(0);
  54. cout << "Enter array size: ";
  55. cin >> iSize;
  56. pNums = new Numbers [iSize];
  57. //load in the values
  58. for (int i(0); i < iSize; i++)
  59. {
  60. cout << "\nValue " << i + 1 << ": ";
  61. cin >> iNum;
  62. pNums[i].AddNum(pNums, iSize, iNum);
  63. }
  64. for (int i(0); i < iSize; i++)
  65. {
  66. if (pNums[i].GetNum())
  67. cout << "Number: " << pNums[i].GetNum() << " " << "Count: " << pNums[i].GetCount(pNums, iSize, pNums[i].GetNum()) << endl;
  68. }
  69. cin.ignore(cin.rdbuf()->in_avail());
  70. cin.get();
  71. delete [] pNums;
  72. return 0;
  73. }

Note that this won't count zeros, considering them null values
Last edited by skatamatic; Oct 28th, 2008 at 9:44 pm.
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
775 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: how to parse lines from a text file?
Next Thread in C++ Forum Timeline: How to display numbers from an input file and count each one's occurence





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC