In: Computer Science
C++
Here is a program that to validate the ISBN number from user typed and output "Valid" if the number is valid, and "Invalid" otherwise.
I want to change this program to read the file, and to validate the ISBN number from the file.
Please add comments.
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::string;
// remove dashes convert letters to upper case
string normalize(const string &isbn) {
string ch;
for (char i : isbn) {
if (i == '-') {
continue; // if "-" then skip it
}
if (isalpha(i)) {
i = toupper(i); // Check uppercase
}
ch += i;
}
return ch;
}
// return the number of digits in to the string
size_t numDigits(string &str) {
size_t numDigits = 0;
for (char ch : str) {
if (isdigit(ch)) {
++numDigits;
}
}
return numDigits;
}
enum ValidationCode {
Ok, // validation passed
NumDigits, // wrong number of digits
ExtraChars // extra characters in isbn
};
enum ValidationCode validate(string &isbn) {
string normal = normalize(isbn);
size_t count = numDigits(normal);
if (count != 10) {
return NumDigits;
}
if (normal.size() == 10 || normal.size() == 11 && normal[10] == 'X') {
return Ok;
}
return ExtraChars;
}
int main() {
string str;
while (cin >> str) {
switch (validate(str)) {
case Ok:
cout << str << " is a valid isbn\n";
break;
case NumDigits:
cout << str << " doesn't have 10 digits\n";
break;
case ExtraChars:
cout << str << " has extra characters\n";
break;
default:
cout << "ERROR: validate(" << str << ") return an unknown status\n";
break;
}
}
}
isbn_data.text
1-214-02031-3
0-070-21604-5
2-14-241242-4
2-120-12311-x
0-534-95207-x
2-034-00312-2
1-013-10201-2
2-142-1223
3-001-0000a-4
done
Hello, here is the completed code you wanted. Every important statement is explained using comments. Please check and let me know if you have any doubts. Thanks.
CODE
#include <iostream>
#include <string>
#include<fstream>
using namespace std;
// function remove dashes convert letters to upper case
string normalize(const string &isbn)
{
   string ch;
  
   //loop to remove dashes convert letters to upper
case
   for (char i : isbn)
   {
       // if "-" then skip it
       if (i == '-')
       {
           continue;
       }
       // Check uppercase
       if (isalpha(i))
       {
           i =
toupper(i);
       }
       //appending to ch
       ch += i;
   }
   //returns isbn code with dashes removed and all
letters are in upper case
   return ch;
}
// function for return the number of digits in the string
size_t numDigits(string &str)
{
   size_t numDigits = 0;
  
   //loop to find number of digits in the string
   for (char ch : str)
   {
       //if digit
       if (isdigit(ch))
       {
           //incrementing
numDigits
          
++numDigits;
       }
   }
   //returns number of digits
   return numDigits;
}
enum ValidationCode
{
   Ok, // validation passed
   NumDigits, // wrong number of digits
   ExtraChars // extra characters in isbn
};
//function to validate isbn
enum ValidationCode validate(string &isbn)
{
   //calling normalize function
   string normal = normalize(isbn);
  
   //calling numDigits
   size_t count = numDigits(normal);
  
   //if count less not equal to 10
   if (count != 10)
   {
       //returns NumDigits
       return NumDigits;
   }
   //if size of the string is 10 or (size of the string
is 11 and last character is X)
   if (normal.size() == 10 || normal.size() == 11
&& normal[10] == 'X')
   {
       //returns Ok
       return Ok;
   }
   //returns ExtraChars
   return ExtraChars;
}
int main()
{
   ifstream file;
   string str;
  
   //opening file isbn_data.txt
   file.open("isbn_data.txt");
  
   //loop to read isbn from file and
validate
   //file >> str will read each isbn in the file
isbn_data
   //this loop will repeat until end of file
   while (file >> str)
   {
       //calling validate function through
switch
       switch (validate(str))
       {
           //if validate is
Ok
           case Ok:
          
    cout << str << " is a valid
isbn\n";
          
    break;
          
           //if validate is
NumDigits
           case
NumDigits:
          
    cout << str << " doesn't have 10
digits\n";
          
    break;
          
           //if validate os
ExtraChars
           case
ExtraChars:
          
    cout << str << " has extra
characters\n";
          
    break;
          
           //Error
condition
           default:
          
    cout << "ERROR: validate(" << str
<< ") return an unknown status\n";
          
    break;
       }
   }
}
OUTPUT

CODE SCREEN SHOT



