Interactive Files and Streams Program Failure

This is a discussion on Interactive Files and Streams Program Failure within the C++ Programming forums, part of the General Programming Boards category; Brand new to the board - I am taking an intro course and have been sailing through the programs until ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164

    Interactive Files and Streams Program Failure

    Brand new to the board - I am taking an intro course and have been sailing through the programs until this one. It is a distance learning class with an ?instructor? There are 45 programs to write and I am stuck on the 37th one. Here goes:

    There is a text file I had to create that has information on computer terminals in a network. There are 100 terminals and the info on the nth terminal is on the nth line of the file. The computer info is terminal type (string), building located in (string), transmission rate (integer), access code (character), and date of last service (month, day, year).

    Write a program to read terminal numbers from the keyboard and directly access the line in the file for each terminal by moving the read position directly to that line. The program should retrieve and display the terminal info.

    This is Chapter 9 and covers ifstream and ofstream objects as well as additional stream features. So, it is not advanced - did I mention it was an intro course?

    I have reworked the code so much...I was getting the terminal info, but now it just hangs before getting to that point (have to control-C to get out). I know it is something simple and I am over thinking the problem. But I can't figure out what it is. A little detailed in my problem explanation I know but what can I say – programming! Any and all help is appreciated.
    Thanks!

    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>    // not used yet
    #include <fstream>
    #include <cassert> 
    using namespace std;
    
    int main()
    {
        ifstream readInFile("terminalInfo.txt");
        // ensures the input file is open
        assert(readInFile.is_open());
        // set line length for finding data
        const int LINE_LENGTH = 100;
        // variable declarations
        string terminalType,
               terminalBuilding,
               date;
        char accessCode,
             response;
        int lineNumber,
            transmissionRate,
            terminalNumber;
        do
        {
            cout << "\nEnter terminal number: ";
            cin >> terminalNumber;
            // 
            if (terminalNumber > 0 && terminalNumber <= 100)
            {
                // find the beginning of the line for that terminal number
                readInFile.seekg(terminalNumber * LINE_LENGTH, ios::beg);
                // read in terminal information from file
                readInFile >> terminalType >> terminalBuilding >> transmissionRate
                     >> accessCode >> date;
                // print terminal information to screen
                cout << "\nInformation for computer number: " << terminalNumber
                        << "\nType of terminal: " << terminalType 
                        << "\nis located in the " << terminalBuilding << " building"
                        << "\nhas a transmission rate of: " << transmissionRate 
                        << "\nan access code of: " << accessCode 
                        << "\nand the last service date was on: " << date;
            }
            cerr << "\nTerminal number is invalid."
                   << "\nValid numbers are from 1 through 100."
                   << "\nTry Again? (Y or N): ";
            cin >> response;
        }
        while (response != 'N' && response != 'n');
        return 0;
    }
    Last edited by clegs; 09-12-2007 at 01:31 AM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >            cin >> terminalType >> terminalBuilding >> transmissionRate
                     >> accessCode >> date;
    This reads from the console (cin stands for Console Input). You want to read from the file that was previously opened:
    Code:
                readInFile >> terminalType >> terminalBuilding >> transmissionRate
                     >> accessCode >> date;

  3. #3
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    Thanks!
    I changed the code above, that solves the hang problem until I added an else to the if. It was rolling right through to the cerr statements. Adding the else, solved that, but now it hangs again.

    I also need to work on the garbage (or nothing) I am getting, instead of the terminal info, from the file.
    I think that I need to define the size of each of the fields in the readinFile, the LINE_LENGTH is an arbitrary number I chose. That may have something to do with the garbage issue. I am going back to the text to see what I can find.

    I may just have to start all over.

    I don't understand why this program is giving me such fits.

    Thanks again.
    Last edited by clegs; 09-12-2007 at 01:30 AM.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    think that I need to define the size of each of the fields in the readinFile, the LINE_LENGTH is an arbitrary number I chose.
    If you're using a windows machine, each line will probably need to be exactly 98 characters (the <CR> and <LF> would add two to equal 100), so you'd pad the end of the line with spaces. Also each piece of data within the line should be separated by at least one space.

    When using seekg() to access a record, naturally each record must be a fixed number of characters long, or seekg() won't find the right record.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    YIPPIE!!!

    I fixed it all! And it was simple as I expected.

    First I used tabs instead of spaces between my terminal information which threw the character count and line length off. So, I fixed each line to 56 with blank spaces, data and the newline character, then set my LINE_LENGTH constant to 56. That and got the information I expected. PERFECT!!

    To fix the if loop hang, I added a question - want info on another terminal? That looped up to the request another terminal number or drop out and end. Well DUH!

    Thanks for your input swoopy!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. files & streams
    By gunghomiller in forum C++ Programming
    Replies: 17
    Last Post: 08-04-2007, 11:19 PM
  2. streams and files
    By chad101 in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2005, 12:54 PM
  3. Need help with input streams from multiple source files
    By orikon in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2005, 03:56 PM
  4. files
    By Raven Arkadon in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2005, 02:18 PM
  5. files and streams
    By correlcj in forum C++ Programming
    Replies: 6
    Last Post: 11-03-2002, 10:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21