Thread: Problems testing my program?

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

    Question Problems testing my program?

    Hello;
    I keep getting a erroe saying assertion debug failed. Why doesn't this give me the max and min, etc..?
    When I enter name of input file: test1.dat
    with data such as
    11
    12
    13
    14
    15
    and when it says the name of the ouput file: test1.out. I press enter and the error poops (I mean pops) up. Strange! Can anyone please explain why?

    It should read something like 5 values ranging from 11 to 15 and its average.


    BTW here is my code:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cassert>
    #include <cfloat>
    using namespace std;
    
    int main()
    {
    	cout << "This program computes the number, maximum, minimum, and\n"
    			"average of an input list of numbers in one file,\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);
    
    	ifstream inStream;
    		inStream.open(inputFileName.data());
    	assert(inStream.is_open());
    	int count = 0;
    	double reading,
    		maximum = DBL_MIN,
    		minimum = DBL_MAX,
    		sum = 0.0;
    
    	for(;;)
    	{
    		inStream >> reading;
    
    		if(inStream.eof()) break;
    
    		count++;
    		sum += reading;
    		if (reading < minimum)
    			minimum = reading;
    		if (reading > minimum)
    			maximum = reading;
    	}
    	inStream.close();
    
    	// output section
    
    cout << "Enter the name of the oputput file: ";
    	string outputFileName;
    	getline(cin, outputFileName);
    
    	ofstream outStream(outputFileName.data());
    	assert(outStream.is_open());
    	outStream << "\n--> There were " << count << " values.";
    
    	if (count > 0)
    		outStream << "\n\tranging from " << minimum
    				  << " to " << maximum
    				  << "\n\tand their average is " << sum / count
    				  << endl;
    	outStream.close();
    	cout << "Processing complete.\n";
    	return 0;
    }

  2. #2
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    Problems testing my program?
    I got it! I forgot to add the orginal data file pressure.dat.
    Thanks anyways!

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    The problem was you still had junk left in your input stream

    Add this line:

    Code:
       // output section
    
       while(getchar() != '\n');
    
       cout << "Enter the name of the oputput file: ";
    	string outputFileName;
    	getline(cin, outputFileName);
    Also you had a logic problem
    Code:
       if (reading < minimum)
          minimum = reading;
       if (reading > maximum)
          maximum = reading;
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Blackjack program, having problems with the ace
    By yigster in forum C Programming
    Replies: 6
    Last Post: 05-07-2009, 06:41 AM
  2. Replies: 19
    Last Post: 05-30-2007, 05:47 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. aughhhhhhhhhhhhhhhhhhhh!!! (program problems)
    By JohnMayer in forum C Programming
    Replies: 15
    Last Post: 07-23-2002, 02:50 AM
  5. Replies: 6
    Last Post: 07-10-2002, 07:45 PM