I'm kind of new to C++ and this forum so forgive me if i look dumb. :icon_cheesygrin:

So my program is basically knowing what category is the book inside the "input.txt" using the Dewey Decimal System and displays it in "output.txt

It's working fine at the moment. But now it is only functioning right if there is only 1 number in "input.txt", i would like to ask how can i edit my program that it will function properly if there are, for example, 90 numbers in "input.txt".

Here is my code btw.:

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

int categories(string& deweyDecimal, ifstream& inStream, ofstream& outStream);


int main()
{
	ifstream inStream;
	ofstream outStream;

	inStream.open("input.txt");
	outStream.open("output.txt");
	string deweyDecimal;
	bool numOfInput=true;
	while(numOfInput)
	{	
		inStream>>deweyDecimal;
	
		try
		{
			if (deweyDecimal.length()!=3)
				throw deweyDecimal;

			for (int i=0; i<deweyDecimal.length();i++)
			{
				if (!isdigit(deweyDecimal.at(i)))
					throw deweyDecimal;
			}
	
		}
		catch (string a)
		{

			outStream<<a<<" is an invalid input. Input should be three numbers."<<endl;
			exit(1);
		}
		numOfInput=false;
	}

	categories(deweyDecimal, inStream, outStream);

	inStream.close();
	outStream.close();
	
	return 0;
}

int categories(string& deweyDecimal, ifstream& inStream, ofstream& outStream)
{
	outStream<<"Category: ";
	switch (deweyDecimal.at(0))
	{
		case '0':
			outStream<<"Computer Science, Information, and General Works"<<endl;

		break;
		case '1':

			outStream<<"Philosophy and Psychology "<<endl;

		break;
		case '2':
			outStream<<"Religion"<<endl;
		break;
		case '3':
			outStream<<"Social Sciences"<<endl;
		break;
		case '4':
			outStream<<"Language "<<endl;
		break;
		case '5':

			outStream<<"Science"<<endl;

		break;
		case '6':

			outStream<<"Technology"<<endl;

		break;
		case '7':

			outStream<<"Arts and Recreation"<<endl;

		break;
		case '8':

			outStream<<"Literature "<<endl;

		break;
		case '9':
			outStream<<"History and Geography "<<endl;
		break;

	}
	return 0;
}

I know the problem is that there is only one string variable used. Can you suggest anything so that i don't have to create a lot of variables? thanks in advance.

Recommended Answers

All 4 Replies

The while statement on line 19 is incorrect -- you don't need numOfInput. To read the entire file the while statement should be constructed like this:

while( inStream>>deweyDecimal )
{
   // blabla

}

But that will only read one word. If each line in the file contains more than one word then you will want to use getline()

while( getline(inStream, deweyDecimal) )
{
   // blabla

}

the while statement in line 19 is for the exception handling and i don't know how to remove it and make the try-and-catch work.

numOfInput is not being used in your try or catch block. Therefore it's not necessary...the only thing it's accomplishing is breaking the loop right away. Also in your categories function, you have one of your parameters "ifstream". You don't need the ifstream parameter (since you can see you don't even use it). It's not hurting anything though.

try something like this:

ifstream fin;
ofstream fout;
fin.open(input.txt);
fout.open(output.txt);

string deweydecimal;

while (fin >> deweydecimal){
  {
  //put all your try/catch stuff here
  
  categories(deweydecimal, ofstream& fout);
  }

EDIT: just noticed I copied Ancient Dragon... sorry.. 6 am here.

Thanks for the reply. I tried what both of you said and it compiled and ran ok. But it is not showing the correct output. I entered 123 in input.txt and the output was the error message on the try and catch. Did i misinterpret what both of you said? Here is the changes i made.

while(inStream>>deweyDecimal)
	{	
		inStream>>deweyDecimal;
/* the whole try and catch above */
		/*

I think i misplaced the inStream>>deweyDecimal; line. But when i removed it inside the while statement it didn't ran. It said abnormal program termination. Again thanks for the reply guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.