Thread: Teachers code aint right

  1. #1
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

    Teachers code aint right

    The code is simple, and its not an assignment. She says theres nothing wrong with it, and i totally disagree. It outputs a second copy of the last digit entered, and i think it lies in the first while loop. Example, say i enter

    1
    2
    3
    -999

    According to theory this places 1, 2, and 3 into the file lined as shown above, and -999 quits that while loop. This is where the problem is. Now if i remeber there is a marker of some sort added to the end of a file to mark the end, could this extra 3 be that marker? Or is the loop screwey? She says its supposed to do that, that she made it that way. I say the code is errorous but im not sure why. I've never used ifstream and ofstream this way before, so i really dont know.

    My beef with this is shes teaching the other students to this code saying its right and i say its errorous, and it ........es me off that she cant explain to me how her design calls for this. She said, and i quote "it doesnt matter how u alter it, its c++ doing it".

    Bull$$$$....please look at this and help me out, i cant prove her wrong alone lol. Also note the way we have been doing it is to NOT have a pre-created file when its run, just let it do it. The file also cannot be specified to .txt/.rtf and so on, no extensions.

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    const int SENTINEL = -999;
    
    int main(int argc, char* argv[])
    {
    	int data;
    	ofstream outfile;
    	ifstream infile;
    
    	outfile.open("c:\myfile");
    
    	cout << "Enter an integer or -999 to quit: ";
    	cin >> data;
    
    	while (data != SENTINEL)
    	{
    		outfile << data << endl;
    		cout << "Enter an integer or -999 to quit: ";
    		cin >> data;
    	}
    
    	outfile.close();
    
    	infile.open("c:\myfile");
    
    	while (! infile.fail())
    	{
    		infile >> data;
    
    		cout << data;
    	}
    
    	return 0;
    }

  2. #2
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    It's fine.
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I don't see anything wrong with either of the while loops. What part of the code is bothering you?

    gg

  4. #4
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Its not here, or at school. Two different versions of VC++, heres the output i get:

    Code:
    Enter an integer or -999 to quit: 1
    Enter an integer or -999 to quit: 2
    Enter an integer or -999 to quit: 3
    Enter an integer or -999 to quit: -999
    1233Press any key to continue
    Notice that extra three?? it should be 123 not 1233....

  5. #5
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    Code:
    	while(infile >> data)
    	{
    		cout << data;
    	}
    it wasn't the writing loop that was causing problems -
    I don't remember exactly why it does this but it's something to do with how infile.fail() will not return true untill *after* EOF is reached - meaning after you do that last infile >> data;
    which means you could also write
    Code:
    while(!infile.fail())
    {
      if(infile >> data) cout << data;
    }
    since when(actually the next operation *after*) infile reaches eof, either a >> operation or the infile itself will evaluate to false(>> returns a reference to the ifstream object if i remember correctly);

    also it's actually quite silly to write
    Code:
    .open("c:\myfile");
    
    instead of
    
    .open("myfile");
    because not only does this limit the simple program to DOS/Windows (which it doesn't need to be limited to, for such a simple program,) but also it means if you don't have a C: drive (rare but yes I have done that - windows sometimes acts real weird without a C: drive ) it won't work.

    Anyway to sum up, the problem was not the write operation but the read operation.
    Last edited by Cobras2; 03-12-2003 at 06:31 PM.
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

  6. #6
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Thanks, makes sense now : ). In school she has us writing it to the a:\ drive i just substituted it out for c:\.

    Thanks again.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    It's another variation of this.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Yep, that one still burns me now and then.
    Its better like this:
    Code:
    while (!(infile >> data).fail())
    {
       cout << data;
    }
    gg

  9. #9
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    she told me the if way was "wrong" and did it this way:

    Code:
    infile.open("c:\myfile");
    
    infile >> data; //prime it
    
    	while (! infile.fail())
    	{
    	    cout << data << endl;
                        infile >> data;
    	}
    
    	return 0;
    what gives?

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    why not just do it this way:
    Code:
    while(infile >> data) {
        cout << data << endl;
    }

  11. #11
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    i showed her that too, she says her way is the standard...wtf is wrong with this woman.

    Today i got kicked out for asking her to open a window. Its warm out, hot in there, and they were doing CONSTRUCTION in the area in back of the room, making it loud and dusty as $$$$. The dusts a health hazard and she kicked me out for my "cocky attitude". I SWEAR this woman CANT be WRONG....

    God im so ........ed off....

  12. #12
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    a couple or so months, depending when you graduate. but, wow, she should have opened a window.

  13. #13
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    A guy who wants to "prove her wrong" kicked out for having a "cocky attitude"? How could THAT happen????

    It sounds like maybe your teacher doesn't really like programming. Maybe she got stuck teaching it. She doesn't seem to have a "curious" personality. (Wouldn't make a good hacker.)

    The best thing you can do is try and be mature about it. Try to do the best programming you can... to please yourself! If you don't respect her knowledge, you don't have to care about her opinion of your work. However, you do have to respect her as a person and as a teacher. And, you should try to get a good grade!

    Learn what you can from the class and NEVER take her classes again! (If you have a choice.)

    [EDIT]
    This is the kind of class that turns good kids into hackers... "This class is boring, let's try to crash the network"
    Last edited by DougDbug; 03-13-2003 at 07:47 PM.

  14. #14
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    She teaches it like its a class and nothing more to her, yet she seems to enjoy the error-fixing end when its not her error. I honestly think she feels a little compromised by having someone whos really into c++ in there. You see shes used to epople just using it to fill a class slot, and not knowing anything. Then here i am someone whos right with her on most things, and usually quicker to realize a problem when debugging something.

    I wasnt trying to prove her wrong in the sense that it first comes across as, i was trying to prove her wrong so that i could understand why it was(n't) right. See if she had just even tried to offer some sort of explanation as too why it was "c++'s fault" i probably would have let it go, but im there to learn, not to take some junk screwey answer as knowledge.

    By working with you guys i have no learned a little bit more than the scope of her lesson offered. I'll never know why she wouldnt work with me here, but she wouldnt. I can live with that knowing that i got my answer as to why.

    The only question i've left to ask is this: What reason, if any, places her fix on a higher scale then the one you guys showed me? And one u cant answer: If its c++'s fault, why did she go home and make a fix?

  15. #15
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    She's a doofus, that's why.
    Once I was subbing at a local school. On my break, I was reading in the library. About 6 or 7 junior high kids were in there working on computers for some assignment. The kids hit the print key, which started printing a 30 or 40 page document.
    Thecrabby old librarian got cranked over that, started yelling at the kids to not hit print, they don't need to print it, etc, really being nasty about it. She shut the printer off to stop it.
    Well, of course when she turned it on it started printing again because all the print jobs were still in the print queue. She yelled at the kids again, and shut the printer off. This happened about 3 times.
    Finally I went up to her and quietly and politely told her she'd have to go into printer setup and delete the print jobs. She gave me a rabid skanky look and hissed "I know that!" Then why the f**k didn't she do it?? I'm so glad my kids don't go to that school system.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM