windows error

This is a discussion on windows error within the C++ Programming forums, part of the General Programming Boards category; when I run this program in dev C++ I get a windows error screen that pops up but it compiles ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    76

    windows error

    when I run this program in dev C++ I get a windows error screen that pops up but it compiles and runs; However, if I run this in visual C++ 6.0 it runs just fine and is error free? Any suggestions?

    FILE INPUT:

    76 89 150 135 200 76 12 100 150 28 178 189 167 200 175 150 87 99 129 149 176 200 87 35 157 189

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    using namespace std;
    // prototypes
    void get_student_grade(int range[8], ifstream& infile, int& score);
    void fill_array_boundaries(string boundaries[8]);
    void print_results(string boundaries[8], int range[8]);
    
    ////////////////////////////////////////////////////////////////////////////
    int main(int argc, char *argv[])
    {
        int range[8];
        string boundaries[8];    
        int score;
        
    ifstream infile;     
    infile.open("C:\\scoresin.txt");   
     
    
              get_student_grade(range, infile, score);
              fill_array_boundaries(boundaries);
              print_results(boundaries, range);
    
    infile.close();       
    system("PAUSE");
    return EXIT_SUCCESS;
    }
    ////////////////////////// Function definitions //////////////////////////
    // get student grades and find the frequency for each range.
    void get_student_grade(int range[8], ifstream& infile, int& score)
    {
         // initialize array range.
         int i;
         for(i =0; i<=8; i++)
        range[i] = 0;
        //--------------------//
    do{              
     infile >> score; // input score from file.
     
       if(score >= 0 && score <= 24)
        range[0] = range[0] + 1; 
       if(score >= 25 && score <= 49)
        range[1] = range[1] + 1;
       if(score >= 50 && score <= 74)
         range[2] = range[2] + 1;
       if(score >= 75 && score <= 99)
         range[3] = range[3] + 1;
       if(score >= 100 && score <= 124)
         range[4] = range[4] + 1;
       if(score >= 125 && score <= 149)
         range[5] = range[5] + 1;
       if(score >= 150 && score <= 174)
         range[6] = range[6] + 1;
       if(score >= 175 && score <= 200)
         range[7] = range[7] + 1;   
    }while(!infile.eof());      
    }  
    // initialize array with score boundaries
    void fill_array_boundaries(string boundaries[8])
    {
      boundaries[0]= "   0 - 24";
      boundaries[1] = "  25 - 49";
      boundaries[2] = "  50 - 74";
      boundaries[3] = "  75 - 99";
      boundaries[4] = "100 - 124";
      boundaries[5] = "125 - 149";
      boundaries[6] = "150 - 174";
      boundaries[7] = "175 - 200";
    }   
    void print_results(string boundaries[8], int range[8])
    {
    int i;
      cout << "Score Boundaries" << setw(35) <<  "Number of Students" << endl;
         for(i = 0; i <= 8; i++) 
            cout << boundaries[i] << "   " << setw(30) << right << range[i] << endl;
    }

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
     for(i =0; i<=8; i++)
        range[i] = 0;
    You're overrunning the array bounds (setting range[8])
    Fix:
    Code:
     for(i =0; i<8; i++)
        range[i] = 0;
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    man that's it!

    I have got to watch out for that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  3. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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