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

#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:

0-13-018661-9
0-13-096141-8
0-201-88336-8
0-534-91504-x
0-88185-020-4

Thanks for the help

Recommended Answers

All 3 Replies

The code you posted doesn't compile cleanly.

I went through the problem, And i think there is a problem with the conversion on the last number

for(i=0; i < a.length(); i++){
		//converts a char to an int
		value = a[i] - '0';
		store[i] = value;

observe the for loop.

try changing it to

for(i=0; i <= a.length(); i++){
		//converts a char to an int
		value = a[i] - '0';
		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.

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).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.