Thread: First try with ARRAYS

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

    First try with ARRAYS

    I'm in deep trouble again. Sorry to be such a pest... I'm trying to learn to use Arrays. I understand that it's supposed to be the easy
    way to program. Thus far, I have:



    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <ios>
    #include <stdio.h>
    #include <stdlib.h>
    using namespace std;
    
    
    
    int main ()
    {
        
        cout << "You have chosen to play Florida's Mega_Money Lotto," << endl << endl;
        cout << " Please wait while I read the file " << endl << endl;
        
        int x = 0;
    
        // initialize 44 vairables from 0 thru 43
    
        
        int freq [44];
    
        for( int u = 0; u < 44; u++)
        {
            freq [u] = 0;
    
        }
    
        
        
        //Opens Temp_Program for reading the file
              
        ifstream a_file ( "D:\\LotteryPrograms\\Fla_Mega_Money\\Hist_temp.txt" );   
    
        for( int u = 0; u != EOF; u++)
        {
        
        a_file >> x;  // reads a number from the file MegaMil
        
        freq [ x - 1 ]++;
    
        }
    
    
        
        
        ofstream b_file ( "D:\\LotteryPrograms\\Fla_Mega_Money\\Loto_Temp_Program.txt" ); //Creates an instance of ofstream, and
    
        for( int u = 0; u < 44; u++)
        {
        
        b_file << "The number " << u + 1 << " appears " << freq [ u ] << " Times. " << endl << endl;
    
        }
    
    
        cout << "You should have it." << endl << endl;
        cin.get();
    
    
        return 0;
    
    }
    And when I build, there are no errors... but when I try to run the program, It just displays the first "cout" messages, and then
    it just sits there (seemingly) doing nothing. The program does not crash. When I stop debugging I get the following errors.
    What is a "PDB" file?


    Code:
    'Frequencies.exe': Loaded 'C:\Users\Therry.arkashea1-PC\Documents\Visual Studio 2010\Projects\Frequencies\Debug\Frequencies.exe', Symbols loaded.
    'Frequencies.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
    'Frequencies.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
    'Frequencies.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
    'Frequencies.exe': Loaded 'C:\Program Files\AVAST Software\Avast\snxhk.dll', Cannot find or open the PDB file
    'Frequencies.exe': Loaded 'C:\Windows\SysWOW64\msvcp100d.dll', Symbols loaded.
    'Frequencies.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
    The program '[6604] Frequencies.exe: Native' has exited with code 0 (0x0).
    Can someone shed a little light on my problem, so I can understand what is happening? ... Thanks ... Therry

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The problem is in your loops
    Code:
        for( int u = 0; u != EOF; u++)
        {
            a_file >> x;  // reads a number from the file MegaMil
         
            freq [ x - 1 ]++;
       }
    EOF is a value returned by C library functions that read files to indicate they have reached end of file. Its value is (often) negative. Incrementing a value starting at zero will keep producing positive values. That will continue until u has the maximum value an integer can represent (about 2147483647 for a 32 bit signed int). Incrementing a signed integral type past its maximum value then gives undefined behaviour (anything is allowed to happen, potentially including reformatting your hard drive).

    The line "a_file >> x" will not magically set u to be EOF when the end of a_file is reached, so your loop will be executed virtually forever (until a crash occurs). So your code never actually checks if the end of file has been read, and will keep reading forever (until an integer overflow occurs, at which point all bets are off).

    You need to read up a bit more on how to detect end of file and other error conditions in streams. Your code is looping without ever testing such conditions.

    Unrelated to your qustion, your code is merrily incrementing freq[x] where x is the value input. That will also give undefined behaviour (writing over memory outside the array freq) if x is negative or x exceeds 43
    Last edited by grumpy; 04-14-2012 at 05:38 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40
    Thanks guys. I solved the problem with:

    Code:
     for( int u = 0; a_file; u++)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > What is a "PDB" file?
    PDB Files

    > 'Frequencies.exe': Loaded 'C:\Users\Therry.arkashea1-PC\Documents\Visual Studio 2010\Projects\Frequencies\Debug\Frequencies.exe', Symbols loaded.
    This means you can debug your program at the source code level.

    > 'Frequencies.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
    This means you cannot debug ntdll.dll (at the source code level).
    Not that you're likely to want to at the moment.

    Code:
    while ( a_file >> x )
    Would be the way to loop reading the file.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modifying parallel arrays to arrays of structures
    By xkohtax in forum C Programming
    Replies: 7
    Last Post: 07-28-2011, 12:07 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  5. separating line of arrays into array of arrays
    By robocop in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 12:43 AM