当从一个文件中读取时崩溃
首先,我想让你知道我是一个大学生(我们的编程教学大纲不是那么先进)(顺便说一句,我没有要求我的任务等答案,我只是练)。当从一个文件中读取时崩溃
行,所以我有2个问题
- 我的代码中的一些功能,改变了我的数组的值(当我不希望它)
- 我真的不知道,但似乎得到了一些从一个文件存储到我的数组中的值会使程序崩溃,此外,在用代码弄弄了一下之后(我不知道什么改变了),它在所述部分期间不再崩溃,但它仍然在代码的末尾崩溃执行..
我希望你们可以帮助我,我b整天都在寻找。
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void readFile (float x[], int &y) {
ifstream load;
string filename;
cout << "Enter the name of the 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;
for (v=0; v<=y; v++) {
z[v] = (5 * (x[v] - 32)/9);
}
}
float average (float x[], int y) {
int v=0;
float sum=0;
for (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;
for (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;
for (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 the 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;
}
(代码从https://drive.google.com/file/d/0BxeZTCUL3Q4oZHlqWFdjMDZub0E/view?usp=sharing)
没有检查全部的代码,我可以直接想到的代码的某些部分。
让我先
while (!load.eof()) {
load >> x[y];
y++;
}
你写大于x []可以保留和你的程序将在这种情况下崩溃更多的价值。
确保只写入您的阵列,直到分配的最大内存。
问题是:load.eof()不会做你期望的操作,因为它会从文件末尾读取。它会再次执行你的循环。
二:
int ctr=0, high, medium, low;
float F[ctr], C[ctr], ave;
char grades[ctr];
ctr
是0
所以你声明的F, c and grades
阵列0元素。这不是很聪明;)你不能读或写他们。
我该如何解决这个问题?我试过不将ctr初始化为0(这会使程序在启动时崩溃)。我需要程序本身来确定要存储到数组中的数据的数量(然后,数字变成数组的大小)有什么办法可以实现这一点? – 2014-12-03 14:26:12
是以几种方式。您可以使用std :: vector之类的“容器”,或者在运行时使用“new”运算符动态分配内存。 – Stefan 2014-12-03 14:29:02
欢迎来到SO。如果你想获得帮助,你的问题可以做一些改进。尝试读这:http://stackoverflow.com/help/how-to-ask告诉我们什么不工作。 – 2014-12-02 08:39:14
欢迎来到stackoverflow.com。请花些时间阅读[帮助页面](http://stackoverflow.com/help),尤其是名为[“我可以问些什么话题?”]的章节(http://stackoverflow.com/help/)讨论话题)和[“我应该避免问什么类型的问题?”](http://stackoverflow.com/help/dont-ask)。另请[请阅读如何提出好问题](http://stackoverflow.com/help/how-to-ask)。您可能还想了解如何创建[最小化,完整和可验证的示例](http://stackoverflow.com/help/mcve)。 – 2014-12-02 08:39:56
至于你的问题,首先不要做'while(!load.eof())',因为它不会像你期望的那样工作。在你试图从文件末尾读取之后,'eofbit'标志将不会被设置,所以你的循环会迭代一次到很多次。其次,在你的'main'函数中,你声明数组'F'和'C'具有* zero size *!这意味着对它们的每次访问(读或写)都将超出导致[* undefined behavior *](http://en.wikipedia.org/wiki/Undefined_behavior)的界限。 – 2014-12-02 08:45:37