Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,222 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,209 people online right now. Registration is fast and FREE... Join Now!




C++ ISBN Validator problem

 
Reply to this topicStart new topic

C++ ISBN Validator problem, I am learning about the use of streams (fstream) and string

cwc123188
5 Nov, 2007 - 09:31 PM
Post #1

New D.I.C Head
*

Joined: 5 Nov, 2007
Posts: 5

My first time here. It still seems impossible that people would help each other out like this online. But I am giving it a try as I can't speak English well enough to ask questions in class... Any help is truly appreciated!! I hope the code doesn't look like a mess as it is my best, for now.

An ISBN (International Standard Book Number) identifies a unique publication. An ISBN is ten digits. The first nine digits must be decimal digits (0...9). The tenth may a decimal digit or the letter X, according to the checksum, seen below. Three single dashes may be between any of the characters, but an ISBN must not begin or end with a dash.

The last character of an ISBN number is a checksum. The checksum is the determined by the first 9 digits; it is computed by taking modulo 11 (the remainder after dividing by 11) of the sum of each digit multiplied by its position in the ISBN. The letter X corresponds to a value of 10.

Here are two ISBNs and the calculations that show how the check sum is determined:

0-201-88337-6 -> (0*1 + 2*2 + 0*3 + 1*4 + 8*5 + 8*6 + 3*7 + 3*8 + 7*9) mod 11 = 6
1-57231-866-X -> (1*1 + 5*2 + 7*3 + 2*4 + 3*5 + 1*6 + 8*7 + 6*8 + 6*9) mod 11 = 10 (X)

I have encountered a few problems. I know I need to use the for statements in the isbnCheckForUser and isbnCheckForFile functions. But I don't know how to or what to filter out first to eventually get to the "checksum" validation. I am also unclear about the use of fstream. I do not know how to use the .txt file shown below for the program.

here's the content of the isbntest.txt file:
6
0-201-88337-6
0-13-117334-0
157231--866-X
-013-117334-0
0821211315
1-57231-866-X


Here's my code so far:

CODE
#include <iostream>
#include <fstream>                                                                            // REQUIRED FOR FILE STREAMS
#include <cstdlib>                                                                            // FOR DEFINITION OF EXIT_FAILURE
#include <cctype>
#include <string>
using namespace std;

// ASSOCIATE STREAMS WITH EXTERNAL FILE NAMES
#define inFile "isbntest.txt"                                                                // LINKED ISBN FILE (LIST) SOURCE

// FUNCTIONS USED
void menuPrompt ();                                                                            // MAIN MENU USER INSTRUCTION
void userVal ();                                                                            // MANUAL INPUT VALIDATION
int readFile ();                                                                            // LINKED FILE VALIDATION
bool isbnCheckForUser (string isbn);                                                        // ISBN VALIDATION FUNCTION FOR USER INPUT
bool isbnCheckForFile (string isbn);                                                        // ISBN VALIDATION FUNCTION FOR FILE INPUT

int main ()
{
    char choice;                                                                            // INPUT-MAIN MENU CHOICE

        do
        {
            menuPrompt ();
            cin >> choice;
            switch (choice)
            {
            case '1':
                    userVal ();
                    break;
            case '2':
                    readFile ();
                    break;
            case '3':
                    cout << "Thank you for using ISBN-Check. Have a nice day!" << endl;
                    break;
            default:
                    cerr << "Oops! Incorrect selection. Please try again." << endl;
            }            
        }
        while (choice != '3');
    
    system ("pause");

    return 0;
}

void menuPrompt ()
{
    cout << "Welcome to ISBN-Check." << endl;
    cout << "This program will verify the format of your ISBN(s)." << endl;
    cout << "To manually input the ISBN(s), enter 1." << endl;
    cout << "To verify the ISBN(s) from the linked .txt file, etner 2." << endl;
    cout << "When you're done, enter 3 to exit the program." << endl;
}

void userVal ()
{
    char subchoice;                                                                            // INPUT- SUB MENU CHOICE
    string isbn;                                                                            // INPUT- ISBN STRING
    do
    {
    cout << "Enter an ISBN:";
    cin >> isbn;
    isbnCheckForUser ();
    if (isbnCheckForUser () = true)
        cout << "This is a valid ISBN." << endl;
    else
        cout << "This is NOT a valid ISBN." << endl;
    cout << endl;
    cout << "To go back to the main menu, enter 1." << endl;
    cout << "To continue manually inputting ISBN(s), enter any digit besides 1." << endl;
    cin >> subchoice;
    }
    while (subchoice != '1');
    
}

int readFile ()
{
    ifstream fin;                
    fin.open (inFile);
    if (fin.fail ())
    {
        cerr << " ERROR: Cannot open " << inFile << " for input. " << endl;
        return EXIT_FAILURE;                                                                // FAILURE RETURN
    }
    fin >> isbn;
    isbnCheckForFile ();
    if (isbnCheckForFile () = true)
        cout << "This is a valid ISBN." << endl;
    else
        cout << "This is NOT a valid ISBN." << endl;
        
}

bool isbnCheckForUser (string isbn)
{

}

bool isbnCheckForFile (string isbn)
{
    
}

User is offlineProfile CardPM
+Quote Post

Alek86
RE: C++ ISBN Validator Problem
5 Nov, 2007 - 10:30 PM
Post #2

New D.I.C Head
*

Joined: 5 Nov, 2007
Posts: 6

how to use fstream:

CODE

#include <fstream>
using std::fstream;
using std::ios_base;

int main() {
    int iVar1 = 12, iVar2 = 0;
    fstream vStream("D:\\1", ios_base::out);
    vStream << iVar1;
    vStream.close();
    vStream.open("D:\\1", ios_base::in);
    vStream >> iVar2;
    vStream.close();
}


about functions you need
maybe you'll try to write some code and you'll ask more exact questions?
User is offlineProfile CardPM
+Quote Post

cwc123188
RE: C++ ISBN Validator Problem
6 Nov, 2007 - 01:46 PM
Post #3

New D.I.C Head
*

Joined: 5 Nov, 2007
Posts: 5

Hi. I tried writing my functions-they are shown below. ( bool isbnCheckForFile (string isbn); and bool isbnCheckForUser (string isbn); included in the complete code) The compiler tells me that it has problem with the getline () at the 170th line of code. I don't know what to do. If there's anyone who can help....Thanks a lot!



CODE

// CS 210 EDDIE CHEN
// THIS IS PROGRAM 4 (ISBN-Check), a program that verifies ISBN.

#include <iostream>
#include <fstream>                                                                            // REQUIRED FOR FILE STREAMS
#include <cstdlib>                                                                            // FOR DEFINITION OF EXIT_FAILURE
#include <cctype>
#include <string>
using namespace std;

// ASSOCIATE STREAMS WITH EXTERNAL FILE NAMES
#define inFile "isbntest.txt"                                                                // LINKED ISBN FILE (LIST) SOURCE

// FUNCTIONS USED
void menuPrompt ();                                                                            // MAIN MENU USER INSTRUCTION
void userVal ();                                                                            // MANUAL INPUT VALIDATION
int readFile ();                                                                            // LINKED FILE VALIDATION
bool isbnCheckForUser (string isbn);                                                        // ISBN VALIDATION FUNCTION FOR USER INPUT
bool isbnCheckForFile (string isbn);                                                        // ISBN VALIDATION FUNCTION FOR FILE INPUT

int main ()
{
    char choice;                                                                        // INPUT-MAIN MENU CHOICE
    string isbn;

        do
        {
            menuPrompt ();
            cin >> choice;
            switch (choice)
            {
            case '1':
                    userVal ();
                    break;
            case '2':
                    readFile ();
                    break;
            case '3':
                    cout << "Thank you for using ISBN-Check. Have a nice day!" << endl;
                    break;
            default:
                    cerr << "Oops! Incorrect selection. Please try again." << endl;
            }            
        }
        while (choice != '3');
    
    system ("pause");

    return 0;
}

void menuPrompt ()
{
    cout << "Welcome to ISBN-Check." << endl;
    cout << "This program will verify the format of your ISBN(s)." << endl;
    cout << "To manually input the ISBN(s), enter 1." << endl;
    cout << "To verify the ISBN(s) from the linked .txt file, etner 2." << endl;
    cout << "When you're done, enter 3 to exit the program." << endl;
}

void userVal ()
{
    char subchoice;                                                                            // INPUT- SUB MENU CHOICE
    string isbn;                                                                            // INPUT- ISBN STRING
    do
    {
    cout << "Enter an ISBN:";
    cin >> isbn;
    isbnCheckForUser (isbn);
    if (isbnCheckForUser (isbn) == true)
        cout << "This is a valid ISBN." << endl;
    else
        cout << "This is NOT a valid ISBN." << endl;
    cout << endl;
    cout << "To go back to the main menu, enter 1." << endl;
    cout << "To continue manually inputting ISBN(s), enter any digit besides 1." << endl;
    cin >> subchoice;
    }
    while (subchoice != '1');
    
}

int readFile ()
{
    ifstream fin;
    string isbn;
    fin.open (inFile);
    if (fin.fail ())
    {
        cerr << " ERROR: Cannot open " << inFile << " for input. " << endl;
        return EXIT_FAILURE;                                                                // FAILURE RETURN
    }
    fin >> isbn;
    isbnCheckForFile (isbn);
    if (isbnCheckForFile (isbn) == true)
        cout << "This is a valid ISBN." << endl;
    else
        cout << "This is NOT a valid ISBN." << endl;
        
}

bool isbnCheckForUser (string isbn)
{
    int isbn0;
    int isbn1;
    int isbn2;
    int isbn3;
    int isbn4;
    int isbn5;
    int isbn6;
    int isbn7;
    int isbn8;
    int isbn9;
    int sum;
    int modulo;
    
    if (isbn.at(0) == '-' || isbn.at(isbn.length()-1) == '-')
        cout << "This is NOT a valid ISBN." << endl;
        
        isbn.find('-');                                
        isbn.erase('-');
        isbn.at(0) = isbn0;
        isbn.at(1) = isbn1;
        isbn.at(2) = isbn2;
        isbn.at(3) = isbn3;
        isbn.at(4) = isbn4;
        isbn.at(5) = isbn5;
        isbn.at(6) = isbn6;
        isbn.at(7) = isbn7;
        isbn.at(8) = isbn8;
        isbn.at(9) = isbn9;
            
    if (isbn.at(isbn.length()-1) == 'x' || isbn.at(isbn.length()-1) == 'X')
        isbn9 = 10;
        
    sum= isbn0 * 1 + isbn1 * 2 + isbn2 * 3 + isbn3 * 4 + isbn4 * 5 + isbn5 * 6 + isbn6 * 7 + isbn7 * 8 + isbn8 * 9;
    modulo = sum / 11;
    
    if (modulo = 10 && isbn.at(isbn.length()-1) == 'x' || isbn.at(isbn.length()-1) == 'X')
        cout << "This is a valid ISBN." << endl;
    
    else if (modulo = 10 && isbn.at(isbn.length()-1) != 'x' || isbn.at(isbn.length()-1) != 'X')
        cout << "This is NOT a valid ISBN." << endl;
            
    else if (modulo != isbn9)
        cout << "This is Not a valid ISBN." << endl;
            
    else
        cout << "This is a valid ISBN." << endl;
}

bool isbnCheckForFile (string isbn)
{
    ifstream fin;
    int num_of_isbn;
    int count;
    int isbn0;
    int isbn1;
    int isbn2;
    int isbn3;
    int isbn4;
    int isbn5;
    int isbn6;
    int isbn7;
    int isbn8;
    int isbn9;
    int sum;
    int modulo;

    getline(istream& fin, string& num_of_isbn, char '\n');
    fin.ignore( 80, '\n');
    while (!fin.eof())
        {
            for (count=0; count < num_of_isbn; count++)
                {
                    getline (fin, isbn);
                    if (isbn.at(0) == '-' || isbn.at(isbn.length()-1) == '-')
                    cout << "This is NOT a valid ISBN." << endl;
                    isbn.find('-');                                //HOW DO I COUNT THE NUMBER OF DASHES?
                    isbn.erase('-');
                    isbn.at(0) = isbn0;
                    isbn.at(1) = isbn1;
                    isbn.at(2) = isbn2;
                    isbn.at(3) = isbn3;
                    isbn.at(4) = isbn4;
                    isbn.at(5) = isbn5;
                    isbn.at(6) = isbn6;
                    isbn.at(7) = isbn7;
                    isbn.at(8) = isbn8;
                    isbn.at(9) = isbn9;
                    if (isbn.at(isbn.length()-1) == 'x' || isbn.at(isbn.length()-1) == 'X')
                    isbn9 = 10;
                    sum= isbn0 * 1 + isbn1 * 2 + isbn2 * 3 + isbn3 * 4 + isbn4 * 5 + isbn5 * 6 + isbn6 * 7 + isbn7 * 8 + isbn8 * 9;
                    modulo = sum / 11;
                    if (modulo = 10 && isbn.at(isbn.length()-1) == 'x' || isbn.at(isbn.length()-1) == 'X')
                    cout << "This is a valid ISBN." << endl;
                    else if (modulo = 10 && isbn.at(isbn.length()-1) != 'x' || isbn.at(isbn.length()-1) != 'X')
                    cout << "This is NOT a valid ISBN." << endl;
                    else if (modulo != isbn9)
                    cout << "This is Not a valid ISBN." << endl;
                    else
                    cout << "This is a valid ISBN." << endl;
                }
        }
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/9/09 05:46AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month