Sorry to bother you guys again, but I need help with I/O files again.
I'm using
Code:
ifstream a_file ( "D:\\LotteryPrograms\\Fla_Mega_Money
\\Fla_Mega_Money_Hist.txt" );
to open a file to read, so "a_file" can update a series of variables (from n1 thru n44), using "int x = 0" to hold data temporarily so
"a_file" can update the variables (n1++...n44++). X is not required to keep anything for very long. It is not what's in x that I
want, it is what is in n1 thru n44 that's important to me.
I'm using
Code:
ofstream b_file ( "D:\\LotteryPrograms\\Fla_Mega_Money\\Loto_Temp_Program.txt" );
to write the result of :
Code:
if (x == 1) n1++; if (x == 2) n2++; if (x == 3) n3++; if (x == 4) n4++;
if (x == 5) n5++; if (x == 6) n6++; if (x == 7) n7++; if (x == 8) n8++;
if (x == 9) n9++; if (x == 10) n10++; if (x == 11) n11++; if (x == 12) n12++;
all the way to n44++
in file Loto_Temp_Program, where it gives me both, the original number and its frequency (n1 = [ n = a variable, and 1 = the # ] ).
However, while the b_file writes:
Code:
for ( int i = 0; i != EOF; i++ );
{
b_file << "#1 has a frequency of " << n1 << endl, ios::app;
b_file << "#2 has a frequency of " << n2 << endl, ios::app;
all the way to n44, what is in place of the variable (n1 thru n44) are "0"
I do not know if the program is actually updating but not writing, or just not updating.
What I get is:
Code:
#1 has a frequency of 0
#2 has a frequency of 0
#3 has a frequency of 0
#4 has a frequency of 0
#5 has a frequency of 0
all the way to #44
the screen does displays all the cout text, making me wonder if the program did the work, but just not
writing it in b_file. My code is:
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <ios>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main ()
{
int first = 0, second = 0, third = 0, fourth = 0, MB = 0;
cout << "You have chosen to play Florida's Mega_Money Lotto," << endl << endl;
// ================================================================
int x = 0;
char str [6], str2 [6];
// initialize 44 vairables to receive and count the numbers from 1 thru 44
int n1=0; int n2=0; int n3=0; int n4=0; int n5=0; int n6=0; int n7=0; int n8=0; int n9=0;
int n10=0; int n11=0; int n12=0; int n13=0; int n14=0; int n15=0; int n16=0; int n17=0; int n18=0;
int n19=0; int n20=0; int n21=0; int n22=0; int n23=0; int n24=0; int n25=0; int n26=0; int n27=0;
int n28=0; int n29=0; int n30=0; int n31=0; int n32=0; int n33=0; int n34=0; int n35=0; int n36=0;
int n37=0; int n38=0; int n39=0; int n40=0; int n41=0; int n42=0; int n43=0; int n44=0;
ifstream a_file ( "D:\\LotteryPrograms\\Fla_Mega_Money\\Fla_Mega_Money_Hist.txt" ); //Opens Temp_Program for reading the file
cout << "Please wait while I read the file... This may take a while" << endl << endl;
ofstream b_file ( "D:\\LotteryPrograms\\Fla_Mega_Money\\Loto_Temp_Program.txt" ); //Creates an instance of ofstream, and
for ( int i = 0; i != EOF; i++ );
{
//int x = 0;
a_file >> x; // reads a number from the file MegaMill
if (x == 1) n1++; if (x == 2) n2++; if (x == 3) n3++; if (x == 4) n4++;
if (x == 5) n5++; if (x == 6) n6++; if (x == 7) n7++; if (x == 8) n8++;
if (x == 9) n9++; if (x == 10) n10++; if (x == 11) n11++; if (x == 12) n12++;
if (x == 13) n13++; if (x == 14) n14++; if (x == 15) n15++; if (x == 16) n16++;
if (x == 17) n17++; if (x == 18) n18++; if (x == 19) n19++; if (x == 20) n20++;
if (x == 21) n21++; if (x == 22) n22++; if (x == 23) n23++; if (x == 24) n24++;
if (x == 25) n25++; if (x == 26) n26++; if (x == 27) n27++; if (x == 28) n28++;
if (x == 29) n29++; if (x == 30) n30++; if (x == 31) n31++; if (x == 32) n32++;
if (x == 33) n33++; if (x == 34) n34++; if (x == 35) n35++; if (x == 36) n36++;
if (x == 37) n37++; if (x == 38) n38++; if (x == 39) n39++; if (x == 40) n40++;
if (x == 41) n41++; if (x == 42) n42++; if (x == 43) n43++; if (x == 44) n44++;
cout << "I have read the file and updated all the number frequencies." << endl << endl;
cout << "I will now try to put them in file Loto_Temp_Program." << endl << endl;
// a_file >> n1; a_file >> n2; a_file >> n3; a_file >> n4;
// cin.get();
}
// =====================================================================================
for ( int i = 0; i != EOF; i++ );
{
b_file << "#1 has a freqienvy of " << n1 << endl, ios::app;
b_file << "#2 has a freqienvy of " << n2 << endl, ios::app;
b_file << "#3 has a freqienvy of " << n3 << endl, ios::app;
b_file << "#4 has a freqienvy of " << n4 << endl, ios::app;
b_file << "#5 has a freqienvy of " << n5 << endl, ios::app;
b_file is writing in the assigned file, but it seems that the updating is either not being done, or the updated values
are not being passed to b_file.
Please, what am I doing wrong?. . . . therry