Question

In: Computer Science

C++ Here is a program that to validate the ISBN number from user typed and output...

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

Solutions

Expert Solution

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


Related Solutions

This C++ program is to validate the ISBN number and output "Valid" if the number is...
This C++ program is to validate the ISBN number and output "Valid" if the number is valid, and "Invalid" otherwise. The ISBN number for a book is a 10-digit number made up of 4 sections separated by '-'. In the ISBN number 1-214-02031-3: 1 represents a country (must be 1 digit) 214 represents the publisher (must be 3 digits) 02031 represents the book number (must be 5 digits) 3 is the checksum (must be 1 digit or the letter 'x')....
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream>, switch and if-else statements only and do not use string storing for the conversion in words. Thank you.** **Our class is still discussing on the basics of programming. Please focus only on the basics. Thank you.** Example outputs: Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four...
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream> and switch and if-else statements only. Thank you. Ex. Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four Enter a number: 100000 One Hundred Thousand Enter a number: -2 Number should be from 0-1000000 only
Write an ISBN Validator in C language What are ISBN? ISBN, or International Standard Book Number,...
Write an ISBN Validator in C language What are ISBN? ISBN, or International Standard Book Number, is a numeric identifier for commercially printed books. Prior to 2007, they had 10 digits, but all newly issued ISBN use 13 digits. They are used to uniquely identify a book and consist of several portions. These include the registrant group, publisher, title, and a check digit. The registrant group indicates where the publisher is located. For example a registrant group of 0 or...
c# language Write a program that takes in a number from the user. Then it prints...
c# language Write a program that takes in a number from the user. Then it prints a statement telling the user if the number is even or odd. If the number is odd, it counts down from the number to 0 and prints the countdown on the screen, each number on a new line. If the number is even, it counts down from the number to 0, only even numbers. For example, if the user enters 5, the output will...
Write a C program for a library automation which gets the ISBN number, name, author and...
Write a C program for a library automation which gets the ISBN number, name, author and publication year of the books in the library. The status will be filled by the program as follows: if publication year before 1985 the status is reference else status is available. The information about the books should be stored inside a linked list. The program should have a menu and the user inserts, displays, and deletes the elements from the menu by selecting options....
2. Write a c++ program that takes from the user the ​number of courses​ and constructs...
2. Write a c++ program that takes from the user the ​number of courses​ and constructs 3 ​dynamic 1D arrays​ with size courses+1. Each array represents a student. Each cell in the array represents a student’s mark in a course. In the last cell of each 1D array you should calculate the average mark of that student. Then output the average mark of all students in each course. Delete any allocated memory. Example Number of courses : 4 50 60...
C programming Get a number from the user and write a program that checks to see...
C programming Get a number from the user and write a program that checks to see if the inputted number is equal to x*x+x and x must be greater than 0. For example, if the user inputs 12. The program should check to see if 12 = x*x+x. In this case it is true because 3*3+3 = 12.
Write a C++ program that finds the minimum number entered by the user .The user is...
Write a C++ program that finds the minimum number entered by the user .The user is allowed to enter negative numbers 0, or positive numbers. The program should be controlled with a loop statement. Break statements is not allowed to break from loop. After each number inputted by the user, The program will prompt user if they need to enter another number. User should enter either Y for yes or N for no. Make Sure the user enters either Y...
Modify the following C++ program to count the number of correct and incorrect responses typed by...
Modify the following C++ program to count the number of correct and incorrect responses typed by the student. After the student types 5 answers, your program should calculate the percentage of correct responses. If the percentage is lower than 75 percent, your program should print “Please ask for extra help” and then terminate. If the percentage is 75 percent or higher, your program should print “Good work!” and then terminate. The program is as follows: #include<iostream> #include<iomanip> #include<cstdlib> #include<time.h> using...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT