Trouble with instream.

This is a discussion on Trouble with instream. within the C++ Programming forums, part of the General Programming Boards category; Greetings, I have written this code which compiles and runs great BUT... I am not coming up with the same ...

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Post Trouble with instream.

    Greetings,

    I have written this code which compiles and runs great BUT...
    I am not coming up with the same answers in the book, can someone help to find out why, plz?

    Here is my code
    Code:
    #include <iostream>	//cin,cout,<<,>>
    #include <fstream>	//ifstream,ofstream	
    #include <string>	//string, getline()
    #include <cassert>	//assert()
    using namespace std;
    
    int num1, num2, num3, num4;
    
    int main()
    {
    	cout << "This program computes num1 thru num4\n"
    	        << "and places its results in another file.\n\n";
    
    	// ------------------input section-------------------------------------------------
    
    	cout << "Enter the name of the input file: ";
    	string inputFileName;
    	getline(cin, inputFileName);	//get name of input file
    	
    	ifstream inStream;	//open an input stream to the input file
    		inStream.open(inputFileName.data());	//establish a connection
    	assert(inStream.is_open());	//check for success
    	
    	for(;;)	//forever loop
    	{
    		inStream >> num1 >> num2 >> num3 >> num4;	//read a value
    
    		if(inStream.eof()) break;	//if eof, quit
    
    	}
    	inStream.close();	//close the connection
    
    	// ------------------------output section-----------------------------------------
    
    while(getchar() != '\n');
    
      cout << "Enter the name of the output file: ";
    	string outputFileName;
    	getline(cin, outputFileName);
    
    	ofstream outStream(outputFileName.data());	//open an output stream to the output file
    	// and establish a connection
    	assert(outStream.is_open());	//check for success
    
    	outStream << num1 << num2 << num3 << num4;	//write a value	
    	
    	outStream.close();	//close stream
    
    	cout << "Processing complete.\n";
    
    	return 0;
    }
    And my data for text file data.dat:
    1 -2 3
    4 -5 6
    7 -8 9
    also my output file is labeled answer.dat.

    The answers I get are the following:
    967-8
    BUT...
    the book says:
    num1= 1, num2= -2 , num3= 3, and num4= 4
    Help me become unconfused to why my numbers don't match?


    next I need to change inStream like so and see its output but its always the same as before, why?

    like so:

    inStream >> num1 >> num2;
    inStream >> num3;
    inStream >> num4;
    Last edited by correlcj; 10-27-2002 at 09:31 AM.
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    This program computes the number, maximum, minimum, and average of an input list of numbers in one file, and places its results in another file.
    No, it doesn't There is no code in there doing any checking for max/min etc. All it is doing is reading in values from a file, overwriting the values previously read. When the reading is done, the variables num1-4 contain the values of the last 4 numbers in the file, which is what is written to the output file.

    The order they are written is 9, 6, 7 -8 because your read fails after reading the 9, and as you are reading into 4 variables at a time, the 9 gets the num1, and num2-4 are left untouched.


    >num1= 1, num2= -2 , num3= 3, and num4= 4
    The numbers would be this if you did only one read from the file, rather than looping to EOF like you currently are.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    [QUOTE >num1= 1, num2= -2 , num3= 3, and num4= 4
    The numbers would be this if you did only one read from the file, rather than looping to EOF like you currently are.[/QUOTE]
    Thanks HAMMER!
    Sorry for the confusion I was in a hurry and posted the min , max cout by mistake. great job on catching that for me also. I did what you said, i killed the loop and read it as you suggested and it works great!
    Thanks again Hammer!:
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  4. #4
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Question Quick question Hammer or anyone really??

    Code:
    ifstream inStream;	//open an input stream to the input file
    		inStream.open(inputFileName.data());	//establish a connection
    	assert(inStream.is_open());	//check for success
    	
    	
    		inStream >> num1 >> num2;
    		inStream >> num3;
    		inStream >> num4;	//read a value
    
    		
    	inStream.close();	//close the connection
    If I changed the inStream like so it would still print it out to the screen as num1 = 1, num2= -2, num3 = 3, and num4 = 4, right?

    I tried it out in my program and it printed 1 -2 34 like so in both now and before in my program. Just so I want to understand it doesn't matter how it gets read, right? Now if I change the osStream and has its printed onto the screen is what will change its location onto the screen, correct?
    Pls let me know if I am babling on or there is some correct wisdom floating somewhere up inside my head. HE! HE! HE!
    Thanks!
    cj
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 11:44 AM
  2. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  3. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 12:58 PM
  4. An InStream has been opened but not delared...
    By correlcj in forum C++ Programming
    Replies: 0
    Last Post: 10-28-2002, 04:57 PM
  5. Where do inStream variables get assigned?
    By correlcj in forum C++ Programming
    Replies: 0
    Last Post: 10-18-2002, 02:13 PM

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