掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
配列の問題 (ID:55004)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
こんなのは参考になりますか? #include <string> #include <fstream> #include <sstream> #include <iostream> int main() { std::ifstream from( "in.txt" ); std::ofstream to( "out.txt" ); if( from.fail() || to.fail() ) { std::cout << "file open error" << std::endl; return 1; } while( true ) { // 行単位でファイル読み込み std::string name, score; if( !std::getline( from, name ) ) // 名前 { break; } if( !std::getline( from, score ) ) // スコア { break; } // "out.txt"ファイル書き込み to << name << '\n' << score << std::endl; // スコア計とデータ数 int total = 0, num = 0; std::istringstream iss( score ); for( int dt; iss >> dt; ++num ) { total += dt; } // 名前,スコア計,データ数,平均値表示 std::cout << name << '\n' << " total = " << total << ", num = " << num << '\n' << " average = " << static_cast< double >( total ) / num << std::endl; } }
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.