| | |
trouble with bools...I think
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2008
Posts: 1
Reputation:
Solved Threads: 0
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
Here is code for the input file:
Thanks for the help
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> using namespace std; //Declares functions int calc(string a); void valid(int a, string b, ofstream & c); //main function int main () { char isbn; int total; //declares the output and input file ofstream outfile ("program1.out"); ifstream myfile ("isbn.dat"); //stops the program if the file's not found if(!myfile){ cout<<"File not found.\n"; exit(1); } //Declares string string line; //Reads contents of file till it ends while(myfile>>line){ cout<<line<<'\t'; //calculates total and stores it total = calc(line); //checks if ISBN is valid and stores in output file cout<<"\t invaild \n"; //Writes to output file outfile<<line<<"\t invalid \n"; } else valid(total, line, outfile); } return 0; } int calc (string a) { int i; int j; int total; int value; int store[10]; //runs until the string ends for(i=0; i < a.length(); i++){ //erases the "-" from the string if (a[i] == '-') { a.erase(i, 1); i--; } } for(i=0; i < a.length(); i++){ //converts a char to an int value = a[i] - '0'; store[i] = value; } 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; //calculates total return total; } //checks the valididty of said ISBN number void valid(int a, string b, ofstream & c){ //if int total is = to the last number in the ISBN then print valid if (a == b[b.length() -1]) { cout<<"\t valid \n"; //Writes to output file c<<b<<"\t valid \n"; } //if total if a is = 10 and the last number is "x" then print valid else if (a == 10 && b[b.length() -1] == 'x') { cout<<"\t valid \n"; //Writes to output file c<<b<<"\t valid \n"; } //anything else print invalid else { cout<<"\t invaild \n"; //Writes to output file c<<b<<"\t invalid \n"; } }
Here is code for the input file:
C++ Syntax (Toggle Plain Text)
0-13-018661-9 0-13-096141-8 0-201-88336-8 0-534-91504-x 0-88185-020-4
Thanks for the help
I went through the problem, And i think there is a problem with the conversion on the last number
observe the for loop.
try changing it to
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.
C++ Syntax (Toggle Plain Text)
for(i=0; i < a.length(); i++){ //converts a char to an int value = a[i] - '0'; store[i] = value;
try changing it to
C++ Syntax (Toggle Plain Text)
for(i=0; i <= a.length(); i++){ //converts a char to an int value = a[i] - '0'; store[i] = value;
Hope this helps.
•
•
Join Date: Nov 2007
Posts: 982
Reputation:
Solved Threads: 210
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).
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
![]() |
Other Threads in the C++ Forum
- Previous Thread: Problem in C++ Permutation(recursive) program
- Next Thread: How to Enable/Disable Users thro' C++ or any code
Views: 438 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






