Strange characters in output Need Help ASAP

This is a discussion on Strange characters in output Need Help ASAP within the C++ Programming forums, part of the General Programming Boards category; I am trying to figure out why I am reading in strange characters from the input file. My input is ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    15

    Strange characters in output Need Help ASAP

    I am trying to figure out why I am reading in strange characters from the input file. My input is just "MerryChristmas". I have tried putting spaces in between the letters and without spaces. I am inserting my code. I really need this back by Monday. Can anyone help me.. I am tearing my hair out..........................

    Code:
    #include <iostream>
    #include <cstdlib>
    //#include "QueueTypeImp.cpp"
    #include "QueSpec.h"
    #include<fstream>
    using namespace std;
    
    //function prototypes
    
    void Fill_Queue(queue& Q);
    
    el_t Remove_First(queue& Q);
    //void Recursive_Reverse(queue& Q);
    void Print_Queue(queue& Q);
    //void Sucessor_Q(queue& Q);
    //void Replace_Q(queue Q, el_t old_val, el_t newval);
    
    // declaring file stream variables
    
    ifstream inQData;
    ofstream outQData;
    
    int main()
    {
    //declaration of variables
    
    el_t element, first_q_element;
    el_t old_val = 'a', newval = 'z';
    queue original_q;
    
    //Opening of files
    inQData.open("a:/queueinfo.txt");
    outQData.open("a:/queueoutput.txt"); 
    
    //fills stack with at least 25 elements from an input file
    
        //inQData>>element;
        
        while ((!original_q.QueueIsFull()) && (inQData ))
        {
        inQData>>element;
            if ( element != '\0')
              original_q.Enqueue(element);
        //inQData>>element;
        }
    
    // Function Calls & Function Operations
    
    outQData.flush();
    first_q_element = Remove_First(original_q);
    outQData<<"The first element in the Queue is :"<<first_q_element<<endl;
    
    Print_Queue(original_q);/*
    outQData<<endl;
    outQData<<"The queue in reverse order is: ";
    Recursive_Reverse(original_q);
    outQData<<endl;
    outQData<<"The queue printed again is: ";
    Print_Queue(original_q);
    outQData<<endl;
    outQData<<"The queue printed with its successor element is : ";
    Sucessor_Q(original_q);
    outQData<<endl;
    outQData<<"The queue printed when replacing a with z is: ";
    Replace_Q(original_q, old_val, newval); 
    outQData<<endl;
    outQData<<"old val"<<old_val<<newval;
    */
    //Close files
    inQData.close();
    outQData.close();
    
    
      system("PAUSE");	
      return 0;
    }
    //Function to remove the first element of queue and print it. The first 
    //element in queue should now be the last element.
    el_t Remove_First(queue& Q)
    {
        el_t e;
        
        if(!Q.QueueIsEmpty())
            {
               Q.Dequeue(e);
               //outQData<<e1;
               Q.Enqueue(e);
            }
        return e;
    }//End of Remove First Function
    
    //Function will print the elements in reverse order.
    void Recursive_Reverse(queue& Q)
    {
        el_t e;
        if (!Q.QueueIsEmpty())
        {
            Q.Dequeue(e);
            Recursive_Reverse(Q);
            Q.Enqueue(e);
            outQData<<e;
        }
    }//End of Recursive Reverse Function
    //Function will print the queue and leave it unchanged
    void Print_Queue(queue& Q)
    {
        el_t e;
        queue temp;
        while(!Q.QueueIsEmpty())
        {
            Q.Dequeue(e);
            outQData<<e;
            temp.Enqueue(e);
        }
          while(!temp.QueueIsEmpty())
          {
             temp.Dequeue(e);
             Q.Enqueue(e);
          }
    }//End of print Function
    //Function will print out the successor of each element in the Queue 
    //and leave the Queue unchanged
    void Sucessor_Q(queue& Q)
    {
        el_t e, suc_val;
        queue temp;
        while(!Q.QueueIsEmpty())
        {
            Q.Dequeue(e);
            temp.Enqueue(e);
        }
        while(!temp.QueueIsEmpty())
        {
            temp.Dequeue(e);
            suc_val = e + 1;
            Q.Enqueue(suc_val);
            outQData<<suc_val;
            
        }
    }//End Sucessor Function
    //Function will repalce every value of a with z, leaving the queue
    //otherwise unchanged.
    void Replace_Q(queue Q, el_t old_val, el_t newval)
    {
        queue temp;
        el_t  e1;
        
        while(!Q.QueueIsEmpty())
        {
            Q.Dequeue(e1);
               if(e1 == old_val)
                  temp.Enqueue(newval);
                      else
                          temp.Enqueue(e1);
        }
           while(!temp.QueueIsEmpty())
           {
               temp.Dequeue(e1);
               outQData<<e1;
               Q.Enqueue(e1);
           }
    }//End of Replace Function

  2. #2
    Registered User
    Join Date
    Oct 2003
    Posts
    15
    Okay since I am not a true novice at this, let me explain what I have done. I did not just write a bunch of code and then compile. I put each function in one at a time and tested each section. It was working up until the last function I wrote. The compiler stopped responding while I was entering new code. I ran the code adn came up with the error.

    The code has commented out because I was trying to isolate the problem and only work with 2 functions.


    I would really like some help not a bunch of bashing. I am not perfect and I am still learning this. I would appreciate if you did not have anything helpful to say then just don't post anything concerning my post.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Have you overloaded the << operator as a friend of ostream?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array output strange
    By swgh in forum C++ Programming
    Replies: 1
    Last Post: 12-09-2006, 06:58 AM
  2. Strange output for class
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 11-05-2006, 04:22 PM
  3. sorting characters and output to text file
    By odb1 in forum C++ Programming
    Replies: 1
    Last Post: 10-10-2003, 05:46 PM
  4. output of characters
    By deleeuw in forum C++ Programming
    Replies: 1
    Last Post: 08-31-2001, 12:17 AM

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