trouble with bools...I think

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 1
Reputation: jacobdet is an unknown quantity at this point 
Solved Threads: 0
jacobdet jacobdet is offline Offline
Newbie Poster

trouble with bools...I think

 
0
  #1
Sep 13th, 2008
so I'm writing a program that takes the input from the file, an ISBN number, removes the "-" from it, puts it through a formula (1*first number 2*second number ... 8*eight number) /11 and what ever that equals should be the same as the last number in the ISBN if it is valid. All of the formula works and everything, but my valid() always returns invalid. This was for school and I got a 95 on the project, but I still don't know why it doesn't work. Here is my code


  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. //Declares functions
  8. int calc(string a);
  9. void valid(int a, string b, ofstream & c);
  10.  
  11. //main function
  12. int main () {
  13.  
  14. char isbn;
  15. int total;
  16.  
  17. //declares the output and input file
  18. ofstream outfile ("program1.out");
  19. ifstream myfile ("isbn.dat");
  20. //stops the program if the file's not found
  21. if(!myfile){
  22. cout<<"File not found.\n";
  23. exit(1);
  24. }
  25.  
  26. //Declares string
  27. string line;
  28.  
  29. //Reads contents of file till it ends
  30. while(myfile>>line){
  31. cout<<line<<'\t';
  32. //calculates total and stores it
  33. total = calc(line);
  34. //checks if ISBN is valid and stores in output file
  35.  
  36.  
  37. cout<<"\t invaild \n";
  38.  
  39. //Writes to output file
  40. outfile<<line<<"\t invalid \n";
  41. }
  42. else
  43. valid(total, line, outfile);
  44.  
  45. }
  46.  
  47. return 0;
  48. }
  49.  
  50. int calc (string a) {
  51.  
  52. int i;
  53. int j;
  54. int total;
  55. int value;
  56. int store[10];
  57.  
  58. //runs until the string ends
  59. for(i=0; i < a.length(); i++){
  60. //erases the "-" from the string
  61.  
  62. if (a[i] == '-') {
  63. a.erase(i, 1);
  64. i--;
  65. }
  66. }
  67.  
  68. for(i=0; i < a.length(); i++){
  69. //converts a char to an int
  70. value = a[i] - '0';
  71. store[i] = value;
  72.  
  73. }
  74.  
  75. total = (1*store[0] + 2*store[1] + 3*store[2] + 4*store[3] + 5*store[4] + 6*store[5] + 7*store[6] + 8*store[7] + 9*store[8]) % 11;
  76. //calculates total
  77. return total;
  78. }
  79.  
  80. //checks the valididty of said ISBN number
  81. void valid(int a, string b, ofstream & c){
  82.  
  83. //if int total is = to the last number in the ISBN then print valid
  84. if (a == b[b.length() -1]) {
  85. cout<<"\t valid \n";
  86. //Writes to output file
  87. c<<b<<"\t valid \n";
  88. }
  89. //if total if a is = 10 and the last number is "x" then print valid
  90. else if (a == 10 && b[b.length() -1] == 'x') {
  91. cout<<"\t valid \n";
  92. //Writes to output file
  93. c<<b<<"\t valid \n";
  94. }
  95. //anything else print invalid
  96. else {
  97. cout<<"\t invaild \n";
  98. //Writes to output file
  99. c<<b<<"\t invalid \n";
  100. }
  101. }


Here is code for the input file:
  1. 0-13-018661-9
  2. 0-13-096141-8
  3. 0-201-88336-8
  4. 0-534-91504-x
  5. 0-88185-020-4

Thanks for the help
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,679
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1504
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: trouble with bools...I think

 
0
  #2
Sep 13th, 2008
The code you posted doesn't compile cleanly.
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 678
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: trouble with bools...I think

 
0
  #3
Sep 13th, 2008
I went through the problem, And i think there is a problem with the conversion on the last number

  1. for(i=0; i < a.length(); i++){
  2. //converts a char to an int
  3. value = a[i] - '0';
  4. store[i] = value;
observe the for loop.

try changing it to
  1. for(i=0; i <= a.length(); i++){
  2. //converts a char to an int
  3. value = a[i] - '0';
  4. store[i] = value;
You are not converting the last char to int as a.length is the total number of letters containing in the string and the for loop ends one short .
Hope this helps.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 982
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 210
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: trouble with bools...I think

 
0
  #4
Sep 13th, 2008
You are not converting from a char to int in the valid() function when comparing with 'a'.

Then a couple of notes:

- try choosing meaningful names, e.g. the valid() function takes three arguments named: a, b, c. That's a poor practice.
- when you ask why a program fails to do something, post code that compiles cleanly (so that there is a program in the first place).
Last edited by mitrmkar; Sep 13th, 2008 at 5:55 am. Reason: notes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 438 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC