I need to read a file in using a queue in between curly braces, this is what i h
ID: 3663348 • Letter: I
Question
I need to read a file in using a queue in between curly braces, this is what i have. I have tried about 20 different ways of doing this and it keeps only printing out a "." or the first "{" . Here is my code:
#include <iostream>
#include <fstream>
using namespace std;
#include "Queue.h"
int main()
{
Queue q1;
char c;
char fname;
ifstream file;
//File.open("sample.txt");
cout<<"Enter filename:";
cin>>file;
//ifstream in(fname);
file.open("sample.txt");
if(!file)
{
cout<<"File Does Not Exist";
}
bool in = false;
while(!file.eof()){
file.get(c);
if( c == '{'){
if(in == false)
in = true;
if(c == '}')
in = false;
if(in == true)
q1.enqueue(c);
cout<<c;
}
}
//cout<<c;
/*while(!file.eof())
{
file.get(c);
if (c == '{' )//|| c== '}')
{
in = true;
q1.enqueue(c);
if (c== '}' && in!=true)
{
in= false;
}
if ( in )
{
in = true;
}*/
/*if ( in == true)
{
q1.enqueue(c);
}*/
file.close();
Here is my Queue.h
/* Queue.h contains the declaration of class Queue.
Basic operations:
Constructor: Constructs an empty queue
empty: Checks if a queue is empty
enqueue: Modifies a queue by adding a value at the back
front: Accesses the front queue value; leaves queue unchanged
dequeue: Modifies a queue by removing the value at the front
display: Displays the queue elements from front to back
Class Invariant:
1. The queue elements (if any) are stored in consecutive positions
in myArray, beginning at position myFront.
2. 0 <= myFront, myBack < QUEUE_CAPACITY
3. Queue's size < QUEUE_CAPACITY
-----------------------------------------------------------------------*/
#include <iostream>
#include <string>
#ifndef QUEUE
#define QUEUE
const int QUEUE_CAPACITY = 128;
typedef string QueueElement;
class Queue
{
public:
/***** Function Members *****/
/***** Constructor *****/
Queue();
/*-----------------------------------------------------------------------
Construct a Queue object.
Precondition: None.
Postcondition: An empty Queue object has been constructed; myFront
and myBack are initialized to -1 and myArray is an array with
QUEUE_CAPACITY elements of type QueueElement.
----------------------------------------------------------------------*/
bool empty() const;
/*-----------------------------------------------------------------------
Check if queue is empty.
Precondition: None.
Postcondition: True is returned if the queue is empty and false is
returned otherwise.
----------------------------------------------------------------------*/
void enqueue(const QueueElement & value);
/*-----------------------------------------------------------------------
Add a value to a queue.
Precondition: value is to be added to this queue.
Postcondition: value is added to back of queue provided there is space;
otherwise, a queue-full message is displayed and execution is
terminated.
-----------------------------------------------------------------------*/
void display(ostream & out) const;
/*-----------------------------------------------------------------------
Output the values stored in the queue.
Precondition: ostream out is open.
Postcondition: Queue's contents, from front to back, have been output
to out.
-----------------------------------------------------------------------*/
QueueElement front() const;
/*-----------------------------------------------------------------------
Retrieve value at front of queue (if any).
Precondition: Queue is nonempty.
Postcondition: Value at front of queue is returned, unless queue is
empty; in that case, an error message is displayed and a "garbage
value" is returned.
----------------------------------------------------------------------*/
void dequeue();
/*-----------------------------------------------------------------------
Remove value at front of queue (if any).
Precondition: Queue is nonempty.
Postcondition: Value at front of queue has been removed, unless queue
is empty; in that case, an error message is displayed and
execution is terminated.
----------------------------------------------------------------------*/
private:
/***** Data Members *****/
int myFront,
myBack;
QueueElement myArray[QUEUE_CAPACITY];
}; // end of class declaration
const int MAX = 128;
typedef string StackElement;
class Stack
{
public:
Stack();
bool empty() const;
void push(const StackElement & value);
void display(ostream & out) const;
StackElement top() const;
void pop();
private:
StackElement myArray[MAX];
int myTop;
};
#endif
my output:
Enter filename:{
or just a period.. any help would be appreciated.
Explanation / Answer
//
// File: main.cc
// Author: HARE KRISHNA
//
// Created on 26 January, 2016, 4:30 PM
//
#include <stdlib.h>
int main(int argc, char** argv) {
Queue q1;
char c;
char fname;
ifstream file;
cout<<"Enter filename:";
cin>>file;
file.open("sample.txt");
if(!file)
{
cout<<"File Does Not Exist";
}
bool in = false;
while(!file.eof()){
file.get(c);
if( c == '{'){
if(in == false)
in = true;
if(c == '}')
in = false;
if(in == true)
q1.enqueue(c);
cout<<c;
}
}
if (!file.good())
{
cout <<" opened for reading failed" << endl;
cout << "End of task" << endl;
exit (1);
}
else
cout << " successfully opened for reading" << endl;
while (!file.eof())
{
for (int i=0;i<MAX;i++)
{
file << name[i];
q.enqueue (name[i]);
}
}
file.close();
return (0);
}
#ifndef QUEUE
#define QUEUE
const int QUEUE_CAPACITY = 128;
typedef string QueueElement;
struct Node;
typedef Node* NodePtr;
struct Node
{
Person p;
NodePtr next;
};
class Queue
{
friend ostream& operator << (ostream&, Person&);
public:
/***** Function Members *****/
/***** Constructor *****/
Queue();
Queue(char [],char);
Queue(const Queue&);
Queue(char[],char)
bool empty() const;
void enqueue(const QueueElement & value);
void display(ostream & out) const;
QueueElement front() const;
void dequeue();
private:
/***** Data Members *****/
int myFront,
myBack;
QueueElement myArray[QUEUE_CAPACITY];
}; // end of class declaration
void Queue::enqueue(const QueueElement & value){
NodePtr temp = new Node;
temp -> p = name;
temp -> next = NULL;
if (tail == NULL)
head = temp;
else
tail -> next = temp;
tail = temp;
size++;
}