Thread: inStream practice problem not working?

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

    Question inStream practice problem not working?

    Greeting,
    Here is my code...
    The question is that inStream has already been opened as a connection to an input file (data.dat) that contains the following data:
    1 -2 3
    4 -5 6
    7 -8 9
    and the the following declarations are in effect
    int n1, n2, n3;
    double r1, r2, r3;
    char
    c1, c2, c3,c4;


    Code:
    #include <iostream>	//cin,cout,<<,>>
    #include <fstream>	//ifstream,ofstream	
    #include <string>	//string, getline()
    #include <cassert>	//assert()
    using namespace std;
    
    int n1, n2, n3;
    double r1, r2, r3;
    char c1, c2, c3, c4;
    
    int main()
    {
    	cout << "This program computes values assigned to each of the variables \n"
                           << "in the input list 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
    	
    	
    		inStream >> c1 >> n1 >> r1 >> r2
    		               >> n2 >> c2 >> c3 >> r3;	//read a value
    
    		
    	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  << c1 << n1 << r1 << r2
    	                   << n2 << c2 << c3 << r3;	//write a value	
    	
    	outStream.close();	//close stream
    
    	cout << "Processing complete.\n";
    
    	return 0;
    }
    I should get the following answers according to the book:
    c1 = '1' n1 = 23, r1 = 45.6 r2 = error--real numbers cant contain letters(ie 'X')

    My is 1-234-567-8, I am
    Can anyone explain why the book cam up with these answers to the practice problems?
    "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
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    can you please post your entire text file (.dat) so i can look at your logical approach...

    b/c looking at your source code it doesn't seem to be wrong....maybe there's a logical problem along the way somewhere, b/c you are alternating your input from a file int, char, double and so on.....

    ...
    matheo917

  3. #3
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    plus it seems like your code is either missing something all it's written in an odd way....

    what's with this line in your "output section"


    while(getchar() != '\n');



    ???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 01-18-2008, 04:06 AM
  2. Problem working PlaySound()...
    By yaya in forum C++ Programming
    Replies: 1
    Last Post: 12-30-2007, 03:17 AM
  3. small reference problem
    By DavidP in forum C++ Programming
    Replies: 6
    Last Post: 06-21-2004, 07:29 PM
  4. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12:30 PM
  5. Replies: 5
    Last Post: 12-03-2003, 05:47 PM