CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2008

    Formatting Error

    I'm having a bit of a formatting error. I've written a program that reads in from file or keyboard and prints out to a new file. Unfortunately I'm getting a blank entry before the proper stuff prints, with normal strings like "," or endls being printed but not the actual interesting stuff. Not sure what's going on here so I'd appreciate any help. Here's a copy of my read method, not sure how helpful it'll be. Personally I don't see anything wrong (of course not, otherwise I wouldn't be asking for help ).

    void Student::write(ostream &file)
    	file << "WORK??";
    	file << name.getFirst() << " ";
    	file << name.getMiddle() << " ";
    	file << name.getLast() << endl;
    	file << address.getStreet() << " ";
    	file << address.getCity() << " ";
    	file << address.getState() << " ";
    	file << zip.GetString() << endl;
    	file << id.GetString() << endl;
    	file << area.GetString() << "-";
    	file << phone.GetString() << endl;
    So, my output file would look like this, whether I enter from keyboard or from a file.



    WORK??my name is
    streetname thiscity mystate 11111

    As for my read from keyboard method, here's a tiny bit (I just copied the first few lines as it's a pretty ugly method):
    int Student::keyRead()
    	int endOfFile = 0;
    	string s;
    	cout << "Please enter first name or type -1 to quit: ";
    	cin >> s;
    	if (s == "-1")
    		endOfFile = -1;
    		cout << "Please enter middle name: ";
    		cin >> s;
    		cout << "Please enter last name: ";
    		cin >> s;
    etc. & etc.
    Basically my read from file is the same, just modified.

    (My Student constructor is declared but at the moment empty.)

    Any suggestions?? I'm probably missing something really obvious, aren't I?
    Thanks in advance, smart folks!

  2. #2
    Join Date
    Jan 2006

    Re: Formatting Error

    As unlikely as it seems, maybe your getter/setter functions have bugs?

    Incidentally, both your write() and keyRead() member functions look like they are better off implemented as non-member non-friend functions. In particular, write() looks like it should be an overloaded operator<< for std::ostream instead.

    If you have concerns about using non-member functions when writing object oriented code, read:
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Dec 2008

    Re: Formatting Error

    I finally found the problem was actually in my for loop as I read things in. I had it reading a blank entry. >_< Oh well, it's fixed.
    Thanks so much for the links, I really appreciate it. I've read through one of them and I'll read through the other two in a little bit. I still have a lot to learn about design and making good design choices! :]

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)