Thread: files/arrays/streams!!!

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    5

    files/arrays/streams!!!

    A bit overwhelmed..very new at this and not much is explained and even with all the reading I've done...I still don't get it
    I am writing a program that checks an ISBN number and it has to be input either from the user or from file input. I think I'm on the right track with copying a file, but can't figure out much more until I understand more...
    -when the file is copied in...6 lines, is it string, or do I make it string? How do I reference it.

    I know if I copied it as an array I could reference it, but I need first to qualify it and take out the dashes...don't I?

    I can qualify the check digit once I get rid of the dashes and put it in an array....right..?

    Just direct me...I've been working on this all week and haven't been able to discover very much.
    (ISBN is 10 digits with 3 dashes, not to be at the beginning or end, and check digit is pretty easy...)

    Just basic direction...I'll work it out...in very plain ENGLISH too...this Mom isn't too use to all the fancy language and this is my first programming language (4 th week!)

    Thanx


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

    #define inFile "isbntest.txt"
    #define outfile "isbn.txt"

    int copyLine (ifstream&, ofstream&);
    int main()
    {

    int lineCount; // output: number of lines processed
    instream ins; // ins is an input stream
    ofstream outs; // outs is and output stream

    // Open input and output file, exit on any error.
    ins.open(inFile); // connects ins to file inFile
    if (ins.fail())
    {
    cerr<< "***ERROR: CANNOT OPEN ****"<<inFile
    << " for input." <<endl;
    return EXIT_FAILURE; // failure return
    }
    outs.open(outFile): // connects outs to file outFile
    if (outs.fail())
    {
    cerr << "***ERROR: CANNOT OPEN***" << outFile
    << " FOR OUTPUT."<< ENDL;
    return EXIT_FAILURE; // failure return
    }

    string line;
    linecount = 0;

    getline (ins, line);
    while (line.length() != 6)
    {
    lineCount++;
    outs << line << endl;
    getline(ins, line);
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    5
    Ok, I believe it should first be put into a string and and checked for validity, ie "-" in wrong place, correct number of digits.. and then the "-" taken out and put in an array of 10. Then could I implement another array matching the contents of the first 9 cells, doing the math thing and derive my own check digit. Then check to see if both arrays match...and if so...

    Am I way off....how to start????

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    5

    Unhappy

    Have to send this off by midnight...thanx for the help

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    5

    Unhappy

    Have to send this off by midnight...thanx for the help

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    There is no data type called string that is inherent in C++, like int, char, double and other primitives are. Therefore, you will always need to provide a class to use that data type. I suspect the string data type you are trying to use is the string class from STL, so include the string header file. If you are trying to use a "naked" null terminated char array (aka c_style string), then you need to change line into a different datatype and use different syntax in the call to getline().

    All string classes I have seen are based on c_style strings. Most of them overload the [] operator so you can reference each char in the underlying char[] just like you would if you used a "naked" char array. Most string classes I have seen also have a method that allows you to erase/remove a given char once you find it with another method like find_first_of. Otherwise you can use the underlying c_style string and use loops to do the finding, erasing, and shifting of characters. Unfortunately the length() fucntion is used to return the number of char in a given string similar to strlen() used on c_style strings. I presume from your initial post that the file has 6 ISBN that you are supposed to manipulate in some fashion, so I don't think the while conditional is appropriate.

    Unfortuntately I can't tell from your post what sort of manipulation you are supposed to do on each ISBN read in. Please repost with more explicit explanation on what manipulation you are trying to do, what type of string you are trying to use, and what the nature/structure of the file you are trying to read is, and THEN maybe I can be of more assistance, although I don't routinely use STL type strings and iterators so I may not be able to of much more help.

Popular pages Recent additions subscribe to a feed