C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 30th, 2005, 02:45 PM
devshaikh11 devshaikh11 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 7 devshaikh11 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 39 sec
Reputation Power: 0
Send a message via ICQ to devshaikh11
Unhappy Can anyone please point out to me what exactly I am doing wrong?

Hi guys, I'm new to the forums here. I am having some trouble with the logic of this code. The program is not doing what I want it to do. My input.txt file contains 10 12 16 14 18. My output.txt file is supposed to contain 0.83 0.75 1.14 0.78 after the program is run. But instead it outputs 0 0 -14 in the output.txt file. I am pretty sure that its something very minor... but I can't spot it. Can anyone please troubleshoot it for me, or give me some tips? I would greatly appreciate it. Thanks.

Code:
//A program that opens two files, one for reading and one for writing.
 //It will then stream in a group of integers and calculate the ratio
 //of one integer that follows it.  The calculated ratio is then 
 //written to a file as a double.
 
 #include<iostream>
 #include<fstream>
 #include<cstdlib>
 using namespace std;
 
 bool open_input(ifstream& fin);
 //Requests the user to enter a filname for an input file.  (The
 //filename is assigned to a character array that is defined as follows
 //char_infile[16].)  Assigns the filename to the variable using a cin
 //statement.Uses the open and fail function to open the input file and
 //tests for failure to open.  Returns true if opening was successful 
 //and false if opening failed.
 
 bool open_output(ofstream& fout);
 //Requests the user to enter a filname for an output file.  (The
 //filename is assigned to a character array that is defined as follows
 //char_outfile[16].)  Assigns the filename to the variable using a cin
 //statement.Uses the open and fail function to open the output file 
 //and tests for failure to open.  Returns true if opening was 
 //successful and false if opening failed.
 
 int read_input(ifstream& fin);
 //Reads in ONE integer from the input file and returns that integer.
 //If the file has reached the end of the file the function should
 //return -1.
 
 void calc_write_ratio(ofstream& fout, int Value1, int Value2);
 //Calculates the ratio of the two integers to this function.  The
 //ratio is then written to the output file as a double followed by a
 //blank space.
 
 /**************************************************  ******************/
 
 bool open_input(ifstream& fin)
 {
 	char infile[16];
 
 	cout << "Enter the name of the input file that you wish to open"
 		<< endl;
 
 	cin >> infile;
 
 	fin.open(infile);
 	
 	if(fin.fail())
 	{
 		cout << "** Can't open input file **" << endl;
 		exit(1);
 	}
 	else
 		return 1;
 }
 
 /**************************************************  ******************/
 
 bool open_output(ofstream& fout)
 {
 	char outfile[16];
 
 	cout << "Enter the name of the output file that you wish to open"
 		<< endl;
 
 	cin >> outfile;
 
 	fout.open(outfile);
 	
 	if(fout.fail())
 	{
 		cout << "** Can't open output file **" << endl;
 		exit(1);
 	}
 	else
 		return 1;
 }
 
 /**************************************************  ******************/
 
 int read_input(ifstream& fin)
 {
 	int next; 
 	fin >> next;
 
 	if(fin.eof())
 		return -1;
 	else	
 		return next;
 }
 
 /**************************************************  ******************/
 
 void calc_write_ratio(ofstream& fout, int Value1, int Value2)
 {
 	double ratio;
 
 	ratio = (Value1 / Value2);
 
 	fout << ratio << " ";
 }
 
 /**************************************************  ******************/
 
 void main()
 {
 	ifstream file_in;
 	ofstream file_out;
 
 	int integer1, integer2;
 
 	open_input(file_in);
 
 	open_output(file_out);
 
 	integer1 = read_input(file_in);
 	
 	do
 	{
 		integer2 = read_input(file_in);
 		
 		calc_write_ratio(file_out, integer1, integer2);
 
 		integer1 = integer2;
 	} while (read_input(file_in) != -1);
 }

Reply With Quote
  #2  
Old April 30th, 2005, 03:35 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 8 m 6 sec
Reputation Power: 4
I see a problem, look very closely at your do-while loop, in the body of the loop you have the "integer2 = read_input(file_in);" line where you read in the next integer, however, look at the condition of your while(), it is "read_input(file_in) != -1", this is also reading in an integer!

so, every pass through your loop, you are reading an integer and storing it, calculating the ration, and reading another integer, NOT storeing it, and then repeating....

there may be other errors, but fix that one first.... I'd recommend re-structuring your loop to look like this (switching to a normal while from a do-while will properly process a file that has no content, and will automatically skip over this loop and exit):

Code:
while ((integer2 = read_input(file_in)) != -1)
 	{
 		calc_write_ratio(file_out, integer1, integer2);
 
 		integer1 = integer2;
 	} 
 }
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips.



Reply With Quote
  #3  
Old May 2nd, 2005, 10:45 PM
devshaikh11 devshaikh11 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 7 devshaikh11 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 39 sec
Reputation Power: 0
Send a message via ICQ to devshaikh11
Thanks a million B-con, I understand now.

P.S. Testing with a stub helped a great deal also.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Can anyone please point out to me what exactly I am doing wrong?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT