C++ isbn validation

it's for homework

Page 1 of 1

2 Replies - 6592 Views - Last Post: 05 November 2008 - 08:34 AM Rate Topic: -----

#1 spylace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-November 08

C++ isbn validation

Posted 05 November 2008 - 01:14 AM

I'm sure you've seen plenty of these to last you a lifetime
Last minute homework fix after a week of procrastination =P
An ISBN number, as you already know, has either 10 or 13 letters with 3 or no dashes stuck in between.
The short version the the story is that this program is to validate ISBN numbers either by user input or calling up a text file in your directory.



#include <cctype>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

#define inFile "isbntest.txt"

			//////////	FUNCTIONS USED	//////////

istream& FLUSH(istream& stream);
void menu();						//	MENU
void manual();						//	ISBN BY USER
void done();						//	QUIT
int byfile();						//	ISBN BY FILE
bool checkISBN();				//	VALIDATING ISBN

#define ENDFILE "CTRL-Z"

int main()
{
	ifstream fin;
	ofstream fout;

	char option;
	string isbn;
	fin.open("inFile");
	fout.open("inFile");

	do
	{
		menu();
		
		cout << ": " << endl;
		cin >> option;
		
		switch(option)
		{
			case 1:
			manual();
			break;
			
			case 2:
			byfile();
			break;
			
			case 3:
			done();
			break;
			
			default:
			cerr << "Incorrect selection! Please try again: " << endl;		
		}
	}	
	while (option != '3');
	fin.close();
	fout.close();
	system("pause");
	return 0;

}										// END MAIN



			//////////	FUNCTION MENU	//////////



void menu()
{
	cout << "Hello!" << endl << endl;
	cout << "This program verifies the ISBN(s) that you insert " << endl << endl;
	cout << "\t To manually insert an ISBN, enter '1' " << endl;
	cout << "\t To call up ISBN from an internal file ISBN.txt, enter '2' " << endl;
	cout << "\t If you are finished and would like to exit, enter '3' " << endl << endl;
	cout << "Have fun =D " << endl; 
}



			//////////	FUNCTION MANUAL	//////////



void manual()
{
	string isbn;																			
	do
	{
		cout << "Enter an isbn or press " 
			<< ENDFILE << ": ";
		cin.get(isbn);					// EXTRACTING ISBN
		checkISBN();

		if (checkISBN() == true)
			fout << "This is a valid ISBN." << endl;
		else
			fout << "This is an invalid ISBN. " << endl;
			fout << "If you would like to quit, enter 'q' " << endl;
			fout << "If you would like to try again, enter an ISBN " << endl;
			fin << isbn;
	}
	while(isbn != ENDFILE);
}										// END MANUAL



			//////////	FUNCTION FILE	//////////



int byfile()
{
	string filename;
	string isbn;
	int count;
	

	fout << "Enter the input file name: ";
	fin >> filename;					//	USER INPUT FOR FILE DIRECTORY
	fin.open(filename.c_str());


	if (cin.fail())
		{
			fin >> count;
			fin.ignore(80, '\n');
			while(!fin.eof())
			{
				getline(fin, isbn);
				if(isbn.(isbn.length() -1) == '\o')
				{
					isbn.erase(isbn.length()-1,1)
				}
			}

			cerr << " ***ERROR: Cannot open " << filename
				<< " for input. " << endl;
			return EXIT_FAILURE;
	}
	else 
	checkISBN(isbn);
	fin.close();
}



			///////////	FUNCTION DONE	///////////



void done()
{
	cout << "Thank you for using this program. " << endl << endl;
	cout << "Have a nice day :) " << endl;
}



			//////////	FUNCTION CHECKISBN	//////////



bool checkISBN(isbn)
{
	int charCount;
	int dashCount;
	int length;
	int i;
	string isbn;

	for (int i = 0; i < 80; i++)			// DEFINING 'i'
		(!isdigit(isbn.at(i));
		length = isbn.length();
		if(length != 10 && length !=13)		// DEFINING LENGTH
		{
			cerr << "Wrong length! Please try again! " << endl;
			FLUSH(fin);
			return(false);
		}
		else
			isbn.at();
			dashCount = 0;
			{
				if(isbn.at(i) == '-')		// DEFINING DASHCOUNT
				{
					++dashCount;
				}
			}
		if(dashCount != 0 && dashCount !=3)
		{
			cerr << "Invalid number of dashes! Please try again! " << endl;
			return(false);
		}
		else 
			cout << "***Valid ISBN*** " << endl;
}



			//////////	FUNCTION FLUSH	//////////

istream& FLUSH(istream& stream)
{
	stream.clear();
	int ch;
	while ((ch = stream.get()) != '\n' && ch != EOF) 
	{
	  ;	// Do nothing inside loop - it's all in the condition!
	}
	stream.clear();
	return stream;
}






And obviously... it does not run
I'm sorta new on the whole programing scene and I know I can't do all this without some kind of help...

On debugging specifically and why it does not run
Some of the errors I'm getting are
-isbn is an undeclared identifier
-fout is an undeclared identifier
-and various semicolon errors....


umm help?

<edit>: gee I'm dumb, I declared it as an int and wrote it as void ><;;
but I've still got a lot of debugging left....

This post has been edited by spylace: 05 November 2008 - 01:50 AM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ isbn validation

#2 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5191
  • View blog
  • Posts: 26,901
  • Joined: 10-May 07

Re: C++ isbn validation

Posted 05 November 2008 - 01:55 AM

When you declare the function checkISBN, you are only giving it the name of the variable. You need to also provide the data type.

Quote

bool checkISBN(isbn);				//	VALIDATING ISBN


error: `bool checkISBN' redeclared as different kind of symbol

You declare checkISBN as a return type of bool, however when you call the function, it doesn't return anything.

Quote

int main()
{
...
void menu()
{



You can also only have one function named main.
Was This Post Helpful? 0
  • +
  • -

#3 spylace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-November 08

Re: C++ isbn validation

Posted 05 November 2008 - 08:34 AM

View Postno2pencil, on 5 Nov, 2008 - 12:55 AM, said:

Quote

int main()
{
...
void menu()
{



You can also only have one function named main.


I changed the first part but I don't necessarily understand the second....
should void not be named menu?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1