| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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);
}
|
|
#2
|
||||
|
||||
|
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. ![]() |
|
#3
|
|||
|
|||
|
Thanks a million B-con, I understand now.
![]() P.S. Testing with a stub helped a great deal also. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Can anyone please point out to me what exactly I am doing wrong? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|