I/O trouble again

This is a discussion on I/O trouble again within the C++ Programming forums, part of the General Programming Boards category; Sorry to bother you guys again, but I need help with I/O files again. I'm using Code: ifstream a_file ( ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40

    I/O trouble again

    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

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Well first of all, whenever you start calling variables n1, n2, n3 etc, then you should be thinking array!

    Given the apparent relationship, you can just do
    array[x]++;


    > for ( int i = 0; i != EOF; i++ );
    Then there's all sorts of wrong on this loop.
    First, you've written for ( i = 0 ; i != -1 ; i++ ) which is going to run for a long time anyway.

    And the trailing ; means it does nothing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Try
    Code:
    while(a_file >> x)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40
    Quote Originally Posted by Salem View Post
    Well first of all, whenever you start calling variables n1, n2, n3 etc, then you should be thinking array!

    Given the apparent relationship, you can just do
    array[x]++;
    Thanks for your reply. I am far too new to C++ to understand how to use arrays... but I am learning. In the meantime, I'm doing it the hard way (Think I'll appreciate arrays better that way)


    > for ( int i = 0; i != EOF; i++ );
    Then there's all sorts of wrong on this loop.
    First, you've written for ( i = 0 ; i != -1 ; i++ ) which is going to run for a long time anyway.

    And the trailing ; means it does nothing.
    I'm not sure that I fully understand your answer...? Are you saying that > for ( int i - 0; i != EOF; i++ ) < is wrong?

    and that the trailing " ; " is what is stopping the variables from being updated? . . . therry

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    EOF is the status result from certain file operations, so using it to limit a for loop makes no sense.

    As for the ;, try this simple test
    Code:
    #include <stdio.h>
    int main (  ) {
      int i;
      for ( i = 0 ; i < 10 ; i++ ) ;  /* yes, there is a ; here */
      printf("i=%d\n", i );
      return 0;
    }
    Think what values of i will get printed, then run the code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with IFs
    By Jingleheimer in forum C Programming
    Replies: 6
    Last Post: 03-31-2008, 05:26 AM
  2. Could I get into trouble?
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 05-31-2007, 07:52 PM
  3. More trouble
    By Metalix in forum C Programming
    Replies: 12
    Last Post: 01-21-2005, 01:18 PM
  4. having a bit of trouble
    By principii in forum C Programming
    Replies: 3
    Last Post: 09-15-2004, 02:19 PM
  5. big trouble in c++!!!
    By newbie_grg in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2002, 04:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21