[英]C++ Not reading in values into array from text file
我一直試圖將文本文件中的值讀入2個數組中,但是最終我的names
和scores
數組中什么都沒有。 這是我所擁有的:
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,但names
和scores
的取值為1。 有什么問題的想法嗎?
您的程序無法正常工作,因為您不小心將names
初始化為數組類型int
,而不是類型std::string
。 這會破壞整行inputFile >> names[x] >> scores[x];
。
愚蠢的錯誤。 只是做的數據結構std::string
叫做names
,並把東西在里面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.