繁体   English   中英

C ++不从文本文件中将值读入数组

[英]C++ Not reading in values into array from text file

我一直试图将文本文件中的值读入2个数组中,但是最终我的namesscores数组中什么都没有。 这是我所拥有的:

const int size = 6;
int names[size] = { 0 };
int scores[size] = { 0 };
int name;
ifstream inputFile;
inputFile.open("input.txt"); //opens up textfile

inputFile >> name;
while (!inputFile.eof()){
    inputFile >> names[x] >> scores[x];
    cout << names[x] << scores[x];
    x++;
}

input.txt

6
Alice 50
Bob 100
Cathryn 75
Don 90
Emily 80
Flora 60
George 95

name的取值为6,但namesscores的取值为1。 有什么问题的想法吗?

您的程序无法正常工作,因为您不小心将names初始化为数组类型int ,而不是类型std::string 这会破坏整行inputFile >> names[x] >> scores[x];

愚蠢的错误。 只是做的数据结构std::string叫做names ,并把东西在里面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM