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.