我很确定这是一个常见的问题,但我找不到类似于我的例子,所以..我有一个名为input.txt的文本文件,其中包含:0.0001234 1.0005434 0.0005678 1.0023423 0.00063452 1.0001546 0.00074321 1.00017654。 现在我想写一个程序将其读入一个数组,然后快速检查它是否有效。 到目前为止,我有:
#include <iostream> #include <fstream> #include <string> using namespace std; int main () { double PA[8]; int n; ifstream myfile ("input.txt"); if (myfile.is_open()) { while (! myfile.eof() ) { getline (myfile,line); cout << PA[line]<< endl; } myfile.close(); } else cout << "Unable to open file"; for (n=0; n<8; n++) // to print the array, to check my work { cout << " {" << PA[n] << "} "; } system("PAUSE"); return 0; }到目前为止,我的问题是我不断收到错误:未声明行。 我想稍后使用浮点数来计算新数据。 我觉得我做错了..有什么帮助吗? 谢谢!
I'm pretty sure that this is a common question, but I can't find an example similar to mine, so.. I have a text file called input.txt that has: 0.0001234 1.0005434 0.0005678 1.0023423 0.00063452 1.0001546 0.00074321 1.00017654 in it. Now I want to write a program to read that into an array, and then quickly check that it worked. So far, I've got:
#include <iostream> #include <fstream> #include <string> using namespace std; int main () { double PA[8]; int n; ifstream myfile ("input.txt"); if (myfile.is_open()) { while (! myfile.eof() ) { getline (myfile,line); cout << PA[line]<< endl; } myfile.close(); } else cout << "Unable to open file"; for (n=0; n<8; n++) // to print the array, to check my work { cout << " {" << PA[n] << "} "; } system("PAUSE"); return 0; }My problem so far is that I keep getting the error: line was not declared. And I want to use the array later with floats to calculate new data. I think I'm doing it wrong for that.. Any help? Thanks!
最满意答案
声明行变量
int n, line = 0; std::string value;适当的加载数据:
getline (myfile,value); PA[line] = atof(value.c_str()); cout << PA[line]<< endl; line++;declare line variable
int n, line = 0; std::string value;proper load data:
getline (myfile,value); PA[line] = atof(value.c_str()); cout << PA[line]<< endl; line++;更多推荐
发布评论