Hey, I am having trouble with the file I/O part of this program. It seems to start at the end of the files contents and not go into the while loop.

#include "queue.cpp"
#include <iostream>
#include <fstream>
#include <iomanip>

/*******************************************
* class Priority                           *
* Takes input from file "queue1.dat"       *
* If that input is '*' calls service()     *
* If that input is '=' exits program       *
* Otherwise, Sends that input to queue.cpp *
* Author: Kimberlie Davis                  *
* Version: 2/5/09                          *
********************************************/
//using std::ifstream;

class Priority:public Queue
{
     protected:
              int priority;
              char character; 
     public:
            void service(void);
                 
};

/*
* method service
* While queue is not empty,
* Output contents of queue
*/
void
Priority::service()
{
    char output;
    
    while(!empty())
    {
       output = takeAway();
       cout << "Service:" << output << endl; 
    }                 
}

/*
* main method
* Takes input from file
* Tests if file exists
* Tests if file is open
* While not end of file, tests files contents
* Calls correct method based on file contents
*/
int main()
{
    Priority priority;
    Queue queue;
    ifstream partin;
    char charInput;
    int numInput;
    
    partin.open("queue1.dat");
    
    if(!partin)
       cout << "Error: Unable to find file" << endl;
    if(partin.fail())  
        cout << "Error: Unable to open file!" << endl; 
    //file >> charInput;
    //file >> numInput;
    
    while(partin >> charInput >> numInput)
    {
        cout << charInput << " " << numInput << endl;
        if(charInput == '*')
           priority.service();
        if(charInput == '=')
           system("exit");
        else
           queue.addToQueue(numInput, charInput);  
             
        //file >> charInput;
        //file >> numInput;
    }
    
    file.close(); 
    system("pause");
}

The code looks ok, can you post a few lines of that file?

Here are the contents of the file:
R 3
T 5
W 1
A 4
* 3
M 5
B 1
E 1
F 2
C 4
H 2
J 1
* 4
* 1
D 3
L 1
G 5
* 9
=

You have a problem at the last line of your input file.

You read while(partin >> charInput >> numInput) and you have just = on the last line so it reads into charInput and then waits and waits.....

You need to read the char and then the number, and check the status of the file in between.

oh wow that works! Why does that keep it from printing before? Like I thought since I have cout << in the while loop, it would print each time it goes through. Is it going through the whole file first?

I commented out the queue.cpp include and associated objects, leaving only the file reading. Your problem is NOT in file reading because that works ok. That means the problem is in the code you did not post.

You have a problem at the last line of your input file.

You read while(partin >> charInput >> numInput) and you have just = on the list line so it reads into charInput and then waits and waits.....

You need to read the char and then the number, and check the status of the file in between.

That's not the problem. The problem seems to be in the last line of that file where the number is missing.

I added an integer after the = (the teacher said we could) and it worked.

errh... sorry Ancient Dragon, but the last line of the file is just "="?
Which was what I was trying to allude to.

The probelm seems to me to be that you read that with a statement while(partin >> charInput >> numInput) Now you get charInput set to "=", which is correct BUT then the program has to get an input into numInput. If the stream is empty then you just wait. So the program just sits. The while loop condition does not get tested because stream::operator<<(int&)
simply never returns.

The better way to do it is to read whole line of input and then divide the line up.

It worked in my test because I deleted that last line not because of the other stuff I commented out. If that last line is supposed to have only one character = then it is better to read the file one character at a time until eof, something line StuXYZ previously posted.

hey guyz...how about this... * If that input is '=' exits program * that's the problem i guess..u can not delete/add something here..

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.