My assignment was to write a program that will take in an ISBN number with dashes and check to see if it is a valid ISBN. The program is meant to stop when the user inputs all zeroes. The issue I am having is that regardless if the ISBN is valid or not it will say the ISBN is Valid.
#include <iostream>
usingnamespace std;
int main()
{
char isbn;//Holds user's ISBN number
string repeat; //Used to enter the first loop
cout << "Would you like to check if an ISBN number is valid or not?"<< endl;
cout <<"Enter Y for yes or 0000000000 for no: ";
cin >> repeat;
while (repeat != "0000000000")
{
int counter;
int limit = 13; //10 isbn numbers and 3 dashes
int x = 10; //Initilized incase user inputs x
int weightedSum = 0;
int code = 0;
int weightedValue = 0;
int weight = 10;
int verification; //Used to hold value of weighted sum modulus 11 for isbn verification
cout<< "Please enter the ISBN number: ";
for (counter = 0; counter < limit; counter++)
{
cin>> isbn;
if(isbn >= '0' && isbn <= '9' || isbn == 'x' || isbn== 'X')
switch(isbn)
{
case'0':
code = 0;
break;
case'1':
code = 1;
break;
case'2':
code = 2;
break;
case'3':
code = 3;
break;
case'4':
code = 4;
break;
case'5':
code = 5;
break;
case'6':
code = 6;
break;
case'7':
code = 7;
break;
case'8':
code = 8;
break;
case'9':
code = 9;
break;
case'x':
code = 10;
break;
case'X':
code = 10;
break;
default:
cout<<"Done validating"<<endl;
weightedValue = code * weight; //Caluclates weighted value
weightedSum = weightedSum + weightedValue; //Calculates weighted sum
weight--;
}
}
verification = weightedSum % 11;
if(verification != 0)
cout<<"Inalid."<<endl;
else
cout<<"valid"<<endl;
cout<<"If you would like to verify another ISBN number input Y, if not enter"<<"\n"<< "0000000000 to stop: ";//User input in order to verify another ISBN number
cin>>repeat;
}
system ("pause");
return 0;
} // End of main function