簡體   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