Output Problem

closed account (L074GNh0)
For some reason, my program can complies perfectly yet when I execute it, there is no data going to my output files and nothing is being displayed in the "New account section of code". Does anyone know what the problem is?
Here's my code:
I think it's a problem with the ProcessAccounts function but I don't know.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//


#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>


// Function prototypes
void ProcessAccounts(std::ifstream &file, std::ofstream &file1, double);
void PrintFileContents(std::ifstream &file);
std::ifstream GetInputFile(std::string);
std::ofstream GetOutputFile(std::string);
void SetInputStreamPos(std::ifstream &, int);
void SetOutputStreamPos(std::ofstream &, int);
double roundHundreths( const double x );

//[BEGIN MAIN]
int main()
  {
  std::string wholeFile;
  std::string inputFileName;
  std::string outputFileName;
  double interestRate;
  std::cout << "Enter an input filename: ";
  std::cin >> inputFileName;
  std::cout << "Enter an output filename: ";
  std::cin >> outputFileName;
  std::cout << "Enter an interest rate (%): ";
  std::cin >> interestRate;
  std::cout << std::endl << std::endl;
  std::ifstream ifile = GetInputFile(inputFileName);
  std::ofstream ofile = GetOutputFile(outputFileName);
 std::cout << "Current account status:" << std::endl;
 PrintFileContents(ifile);
 std::cout << std::endl;
 std::cout << "SetInputStreamPos(ifile, 0):" << std::endl << std::endl;
 SetInputStreamPos(ifile, 0);
 PrintFileContents(ifile);
 std::cout << std::endl << "Test 1 - Did it work?" << std::endl << std::endl;
 std::cout << "Processing accounts...";
 ProcessAccounts(ifile, ofile, interestRate);
 std::cout << "DONE!" << std::endl;
 std::cout << "Closing the files...";
 ifile.close();
 ofile.close();
 std::cout << "DONE!" << std::endl << std::endl;
 ifile = GetInputFile(outputFileName);
 std::cout << "New account status:" << std::endl;
PrintFileContents(ifile);
std::cout << std::endl << "Done printing new status!." << std::endl;
std::cout << "Test 2 - 5: Are the 4 lines correct?" << std::endl;
ifile.close();
    return 0;
    }

void ProcessAccounts(std::ifstream &ifile, std::ofstream &ofile, double rate)
{

	double save; //Saving's account variable
	std::string name; //String used to store name
	double credit; //Credit account variable

	while (ifile >> name >> save >> credit)  //While the data is being read in from ifile
    {

	     if (save >= 0) //If the savings variable is greater than or equal to zero
	     {
		     double diff = save; //Assign the value of the value of the savings account to the variable 'diff'
		     diff = diff*(1+(rate/100)); //Multiply diff by (100+the interest rate)/100
		     save = diff; //Assign save the value of diff
		     credit = credit *(1+(rate/100)); //Multiply the credit by (100+the interest rate)/100
			 credit = roundHundreths(credit); //Rounds the hundreths digit up or down (if it is >=.05 or less than .05, respectively)
		     save = roundHundreths(save); //Rounds the hundreths digit up or down (if it is >=.05 or less than .05, respectively)
	     } //End if

	     else if  ((credit > save)&&(save < 0)) //Otherwise, if the credit is greater than the savings and the savings are less than zero 
	     {
		    double diff2= credit-save; //Assign diff2 to positive difference between the credit and the save variables
		    diff2=diff2 * (1+(rate/100)); //Multiply diff2 by (100+the interest rate)/100
		    save = 0; //Set save to zero
		    credit=diff2; //Assign credit to the value of diff
			credit = roundHundreths(credit); //Rounds the hundreths digit up or down (if it is >=.05 or less than .05, respectively)
			save = roundHundreths(save); //Rounds the hundreths digit up or down (if it is >=.05 or less than .05, respectively)
	     } //End if

	ofile << name <<std::setprecision(2) << std::fixed << save << std::setprecision(2) << std::fixed  <<credit << std::endl; //Send output to ofile in this format
  } //End while
} //nd ProcessAccounts

void PrintFileContents(std::ifstream &str)
{
 
	std::string name; //Account name variable
	double saving; //Savings variable
	double credit; //Credit variable

	std::cout << std::left << std::setw(10) <<"Name";
	std::cout << std::setw(10) << "Savings" << "Credit" << std::endl << std::fixed << std::showpoint; //Output a "Name", "Savings" and "Credits" column


	while (str >> name >> saving >> credit) //While there is an input from the file in this form
	{
		if(saving < 0) //If the value in the savings variable is less than zero
		{
			double newsaving = - saving; //Assign newsaving to the negative value of the savings value
			std::cout << std::setw(10) << name << "-$" << std::setw(8) << std::fixed  << std::setprecision(2) << newsaving << "$" << std::fixed << std::setprecision(2) << std::setw(0) << credit << std::endl; //Output the values from the file
		} //End if
		    std::cout << std::setw(11) <<name <<"$" << std::setw(8) <<std::fixed << std::setprecision(2) << saving << "$" << std::fixed << std::setprecision(2) << std::setw(0) << credit << std::endl; //Output the values from the file
	} //End while
} //End PrintFileContents

std::ifstream GetInputFile(std::string filename)
{
  // Open the file named filename
  // Make sure it exists
  // Return it as an input file stream
  
  // Put your code here
	std::ifstream Name(filename); //ifstream constructor opens file 
	if (!Name) //If such a file doesn't exist
	{
		std::cerr << "File couldn't be opened" << std::endl; //Output error message
		exit(1); //Terminate program exectution
	} //End if
   return Name; //Return Name to main
}

std::ofstream GetOutputFile(std::string filename)
{
  
	std::ofstream Name(filename); //ofstream constructor opens file
	if (!Name) //If file doesn't exist
	{
		std::cerr << "File couldn't be opened" << std::endl; //Output error message
		exit(1); //Terminate program execution
	} //End if
	return Name; //Return Name to main
}

void SetInputStreamPos(std::ifstream &str, int pos)
{
 
	str.seekg(pos,std::ios::beg); //Set the cursor at 'pos' bytes from the beginning in the input stream
}

void SetOutputStreamPos(std::ofstream &str, int pos)
{
  
	str.seekp(pos, std::ios::beg); //Set the cursor at 'pos' bytes from the beginning in the output strea
}

double roundHundreths( const double x ) 
{
	double result = x * 100.0 + 0.5; //Moving number 2 places right and trying to round
	result = static_cast<int>( result ); //Flooring
	result /= 100.0; //Moving number 2 places left
    return result;
}
Last edited on
format your pasted codes inside code blocks to make it readable with line numbers as below for faster tracing:

[code ]
....put your codes here
[/code ]

You can insert this open and close "code" tags by clicking <> button of the Format: options at the right of the text box when you compose a message.
Topic archived. No new replies allowed.