![](/img/trans.png)
[英]I'm having trouble in C++ geting input from a user in a function, adding to an array, and printing that array
[英]I'm Having Trouble Skipping Certain Characters from an Input Text File (C++)
(我很抱歉,與我在此網站上看到的大多數問題相比,這個水平太低了,但我的想法已經用完了,不知道還有誰要問。)
我正在做一個學校項目,該項目要求我從名為in06.txt的文件中讀取籃球統計數據。 文件in06.txt的外觀如下:
5
電話17 24 9 31 28
R 4 5 1 10 7
A 9 2 3 6 8
S 3 4 0 5 4
我需要讀取第一個數字5並將其存儲到一個名為“游戲”的變量中。 從那里,我必須從第二行讀取數字,並確定高,低和平均值。 對於第3、4和5行,我必須做同樣的事情(FYI,字母P,R,A和S表示“點”,“籃板”,“助攻”和“偷球”。)
因為我只學習了幾周的編程知識,所以我不想因為直接進入項目的各個方面而感到不知所措。 因此,我首先要確定每條線的平均值。 我的計划是保持每條線的運行總和,然后將運行總和除以游戲數,在這種情況下為5。
這是我的代碼:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
int games;
int points_high, points_low, points_total;
int rebounds_high, rebounds_low, rebounds_total;
int assists_high, assists_low, assists_total;
int steals_high, steals_low, steals_total;
double points_average, rebounds_average, assists_average, steals_average;
ifstream fin;
ofstream fout;
fin.open("in06.txt");
if( fin.fail() ) {
cout << "\nInput file opening failed.\n";
exit(1);
}
else
cout << "\nInput file was read successfully.\n";
int tempint1, tempint2, tempint3, tempint4;
char tempchar;
fin >> games;
fin.get(tempchar); // Takes the endl; from the text file.
fin.get(tempchar); // Takes the character P from the text file.
while( fin >> tempint1 ) {
points_total += tempint1;
}
fin.get(tempchar); // Takes the endl; from the text file.
fin.get(tempchar); // Takes the character R from the text file.
while( fin >> tempint2 ) {
rebounds_total += tempint2;
}
fin.get(tempchar); // Takes the endl; from the text file.
fin.get(tempchar); // Takes the character A from the text file.
while( fin >> tempint3 ) {
assists_total += tempint3;
}
fin.get(tempchar); // Takes the endl; from the text file.
fin.get(tempchar); // Takes the character S from the text file.
while( fin >> tempint4 ) {
steals_total += tempint4;
}
cout << "The total number of games is " << games << endl;
cout << "The value of total points is " << points_total << endl;
cout << "The value of total rebounds is " << rebounds_total << endl;
cout << "The value of total assists is " << assists_total << endl;
cout << "The value of total steals is " << steals_total << endl;
return 0;
}
這是(不正確的)輸出:
Input file was read successfully.
The total number of games is 5
The value of total points is 111
The value of total rebounds is 134522076
The value of total assists is 134515888
The value of total steals is 673677934
我已經讀了好幾個小時的關於教科書中文件輸入的內容,希望能找到一些能表明為什么我的程序輸出錯誤值的東西。 但是,我什么也沒找到。 我還在該論壇和其他論壇上研究了類似的問題,但是解決方案使用的是我尚未了解的方法,因此,我的老師不允許在我的項目代碼中使用這些方法。 我看到的一些方法是數組和getline函數。 我們尚未了解任何一個。
注意:我的老師不希望我們存儲輸入文件中的每個整數。 他希望我們一次打開文件並存儲游戲數,然后使用循環和if語句從每一行確定高,平均和低數。
如果有人可以幫助我,我將不勝感激! 謝謝!
您已聲明所有這些變量:
int games;
int points_high, points_low, points_total;
int rebounds_high, rebounds_low, rebounds_total;
int assists_high, assists_low, assists_total;
int steals_high, steals_low, steals_total;
double points_average, rebounds_average, assists_average, steals_average;
然后增加它們:
points_total += tempint1;
這些變量從未初始化為已知值(0),因此它們中有垃圾。 您需要初始化它們。
除了OldProgrammer所說的以外,您還錯誤地讀取了整數。 這樣的循環
while( fin >> tempint2 ) {
rebounds_total += tempint2;
}
發生錯誤時將停止。 也就是說,它到達EOF或提取遇到無法格式化為整數的數據-換句話說, good()
返回false
。 正如您似乎認為的那樣 ,它並不會在一行的結尾處停止閱讀。 設置錯誤標志后,所有進一步的提取操作都會失敗,直到您清除標志為止。 在您的情況下,循環在P
之后開始讀取,提取五個整數,但隨后在下一行遇到R
並出錯。
將其更改為一個讀取固定數目的整數的循環,或者使用std::getline
將整行讀取到std::string
,將其放入std::stringstream
並從那里讀取。
無論如何,學習編寫健壯的代碼。 檢查提取是否成功 ,並計算獲得多少元素。
最多讀取5個整數的循環示例:
int i;
int counter = 0;
while (counter < 5 && file >> i) {
++counter;
// do something with i
}
if (counter < 5) {
// hm, got less than 5 ints...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.