weird cout error, overwrites current line
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: weird cout error, overwrites current line

  1. #1
    Join Date
    Jan 2009
    Posts
    24

    Cool weird cout error, overwrites current line

    I am creating a program for my dad, which will have a catalogue of patients, and will check them for dates and such, but that's off topic. Here's my dilemma, whenever I cout a series of messages, the program will not append the message to the one before it. Instead, it simply writes over the last one. Kinda like if it's just gluing the messages on top of each other.

    when I run my program:

    Code:
    #include <fstream>
    
    #include <iostream>
    
    #include <ctime>
    
    #include <string>
    
    #include <vector>
    
    #include <sstream>
    
    #include <exception>
    
    
    
    using namespace std;
    
    
    
    class patient     //this class will only be used by database and no direct contact with the user without database should occur
    
      {
    
    private:
    
        string firstname;
    
        string lastname;
    
        vector<string> middlenames;
    
        time_t date;
    
    public:
    
        patient(string fileline);//this constuctor takes a line from the database to create a patient
    
        string getdate();
    
        string getinfohuman();//I will customize this function
    
        string getinfocomp();
    
      };
    
    
    
    patient::patient(string fileline)//be wary of the throw at the ending
    
      {
    
        try
    
          {
    
            stringstream filelinestream;
    
            filelinestream<<fileline;
    
            string currentword;
    
            if(true)//this if exists so the objects are deleted once the check is over
    
              {
    
                stringstream checkstream;
    
                string checkword;
    
                checkstream<<fileline;
    
                checkstream>>checkword;
    
                checkstream>>checkword;
    
                for (int a=30;; a--)
    
                  {
    
                    checkstream>>checkword;
    
                    if (checkword=="^")
    
                        break;
    
                    if (a==0)
    
                        throw 1;
    
                  }
    
                int checkdate;
    
                if (!(checkstream>>checkdate))
    
                    throw 1;
    
              }
    
            filelinestream>>currentword;
    
            firstname=currentword;
    
            filelinestream>>currentword;
    
            lastname=currentword;
    
            filelinestream>>currentword;
    
            for(register int middlenum=0;currentword != "^";middlenum++)
    
              {
    
                middlenames.push_back(currentword);
    
                filelinestream>>currentword;
    
              }
    
            filelinestream>>date;
    
          }
    
        catch (int a)
    
          {
    
            cout<<"Error creating patient"<<endl;
    
            throw 1;
    
          }
    
      }
    
    
    
    string patient::getinfocomp()
    
      {
    
        string outstring;
    
        outstring = " " + firstname + " " + lastname;
    
        for (int middlenum=middlenames.size()-1; middlenum>=0; middlenum++)
    
          {
    
            outstring = outstring + " " + middlenames[middlenum];
    
          }
    
        outstring = outstring + " ^" + " " + getdate();
    
      }
    
    
    
    string patient::getinfohuman()
    
      {
    
        string outstring;
    
        outstring = firstname + "  " + lastname + "  " + "date:" + " " + getdate();
    
        return outstring;
    
      }
    
    
    
    string patient::getdate()
    
      {
    
        stringstream datestream;
    
        string outstring;
    
        datestream<<date;
    
        datestream>>outstring;
    
        return outstring;
    
      }
    
    
    
    class database
    
      {
    
    public:
    
        fstream &datafilestream;
    
        vector<patient> data;
    
        database(fstream &main);
    
        //string getinfocomp();
    
        //string getinfohuman();
    
        //void addpatient;
    
      };
    
    
    
    /*string database::getinfohuman()
    
      {
    
        string outstring;
    
        ou
    
      }*/
    
    
    
    database::database(fstream &main)//find a way to store broken patients
    
        :datafilestream(main)
    
      {
    
        string patientinput;  
    
        while(getline(main,patientinput))
    
          {
    
            cout<<patientinput<<endl;
    
            if(patientinput=="^eof^")
    
                break;
    
            try
    
              {
    
                patient *space=new patient(patientinput);
    
                data.push_back(patient(*space));
    
              }
    
            catch(int a)//this one handles the patient exeption thrown at the end of the constructor
    
              {
    
                string printme=patientinput;
    
                cout<<"patient "<<"\""<<printme<<"\""<<" has been discarded."<<endl;
    
              }
    
          }
    
      };
    
    
    
    int main()
    
      {
    
        fstream mainfile;
    
        mainfile.open ("database.txt");
    
        database maindatabase(mainfile);
    
        return 0;
    
      }
    ...with database.txt being...
    Code:
    I'm blue badabedad I if I was green I would die bada bedadadodo ^ yougio
    Dakota Camron Harrison-Smith ^ 1576800000
    Foo Bar Baz ^ 0
    I get

    Code:
    I'm blue badabedad I if I was green I would die bada bedadadodo ^ yougio
    Error creating patient
    " has been discarded.abedad I if I was green I would die bada bedadadodo ^ yougio
    Dakota Camron Harrison-Smith ^ 1576800000
    Foo Bar Baz ^ 0
    in my console

    I think the problem is somewhere in database::database(fstream &main) in the catch statement.

    Anything would be helpful, even program critiques.

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,011

    Re: weird cout error, overwrites current line

    Quote Originally Posted by E-man96 View Post
    Here's my dilemma, whenever I cout a series of messages, the program will not append the message to the one before it. Instead, it simply writes over the last one. Kinda like if it's just gluing the messages on top of each other.
    Maybe the file you are reading contains a carriage return before each line ending. When you use getline, the line ending is not copied, but the carriage return is.
    Easiest way to fix that is probably to remove a carriage return from the end of a line after you read it.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Apr 1999
    Posts
    27,422

    Re: weird cout error, overwrites current line

    Quote Originally Posted by E-man96 View Post
    I am creating a program for my dad, which will have a catalogue of patients,
    Code:
    patient *space=new patient(patientinput);
    data.push_back(patient(*space));
    There is no need for calling "new" here. Your vector takes objects, so just have the object created and copied. The result is that your program leaks memory.
    Code:
    data.push_back(patient(patientinput));
    That is all you need to do. Now you do not have any memory leaks with the above approach.

    Regards,

    Paul McKenzie

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center