c++ - crash when reading from a file -


first of all, i'd u know im college student (our syllabus in programming not advanced yet)(btw im not asking answers assignments etc, im practicing).

ok have 2 problems

  1. some function in code changes value of array (when dont want to)
  2. i have no idea, seems getting values file store array crashes program, furthermore, after fiddling bit code (i have no idea changed), no longer crashes during said part, still crashes @ end of code execution..

i hope u guys me, i've been searching whole day long.

#include <iostream> #include <fstream> #include <string> #include <iomanip>  using namespace std; /* run program using console pauser or add own getch, system("pause") or input loop */ void readfile (float x[], int &y) {     ifstream load;     string filename;     cout << "enter name of file: ";     cin >> filename;      load.open(filename.c_str());     while (!load.eof()) {         load >> x[y];         y++;     }     if (!load) {         cout << "asd";     }  }  void computec (float x[], int y, float z[]) {     int v=0;      (v=0; v<=y; v++) {         z[v] = (5 * (x[v] - 32)/9);     } }  float average (float x[], int y) {     int v=0;     float sum=0;     (v=0; v<=y; v++) {         sum += x[v];     }      return sum / v; }  void grade (float x[], char grades[], int y, int &hi, int &med, int &lo) {     int v=0;     hi = med = lo = 0;     (v=0; v<=y; v++) {         if ( x[v] >= 35) {         grades[v] = 'h';         hi++;         }          else if ( (x[v] < 35) && (x[v] >= 20) ) {         grades[v] = 'm';         med++;         }          else if ( x[v] < 20 ) {         grades[v] = 'l';             lo++;         }     }  }  void writefile (float x[], float y[], int z, char w[]) {     ofstream save;     int v=0;       (v=0; v <= z; v++) {         cout << "c(celcius)" << left << setw(5);         cout << "f(farenheit)" << left << setw(5);         cout << "description\n";         cout << right << setw(7) << y[v];         cout << left << setw(8) << x[v];         cout << left << setw(8) << w[v];             } } int main(int argc, char** argv) {     int ctr=0, high, medium, low;     float f[ctr], c[ctr], ave;     char grades[ctr];       readfile (f, ctr);     computec (f, ctr, c);     ave = average (c, ctr);     grade (c, grades, ctr, high, medium, low);      cout << "average of temperatures: " << ave << endl;     cout << "number of high temperatures: " << high << endl;     cout << "number of medium temperatures: " << medium << endl;     cout << "number of low temperatures: " << low << endl;      //writefile (f, c, ctr, grades);       return 0; } 

(code https://drive.google.com/file/d/0bxeztcul3q4ozhlqwfdjmdzub0e/view?usp=sharing)

without checking whole code can directly think of parts of code.

let me start with

while (!load.eof()) {         load >> x[y];         y++;     } 

where write more values x[] can hold , program crash in case.

make sure write array maximum allocated memory.

the problem !load.eof() not expect read beyond end of file. execute loop 1 more time much.

second:

 int ctr=0, high, medium, low;     float f[ctr], c[ctr], ave;     char grades[ctr]; 

ctr 0 declare arrays of f, c , grades 0 elements. that's not clever ;) can't read or write them.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -