for循环的问题
Trouble with for loop
我的程序有问题,我需要写文件每个玩家在5天内玩了多少场游戏,这是我的txt文件:第一行的第一个数字是他们玩了多少天,第2行到第5行的第一个数字是他们玩了多少天,每一行的其他数字是他们在这些天里玩了多少场游戏:
5
5 3 2 3 1 2
3 6 2 4
4 2 2 1 2
3 3 3 3
2 3 4
下面是我的程序:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int programuotojai,programos;
ifstream fin ("duomenys.txt");
fin >> programuotojai; //players
for(int i = 1; i <= 6; i++){
fin >> programos; //games played
cout << programos;
}
}
你能帮我把这个程序写完吗,谢谢。
在阅读了游戏的数量之后,你需要阅读游戏本身。比如:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int programuotojai,programos;
ifstream fin ("duomenys.txt");
fin >> programuotojai; //players
for(int i = 1; i <= programuotojai; i++){
fin >> programos; //games played
cout << programos;
for (int j=0;j<programos;++j) {
int day;
fin>>day;
// ..... do some fancy stuff
}
}
}
也使用programuotojai
代替常量6(如果我得到正确的代码)。
我不会写完整的程序,但在我看来,你必须把每一行的数字加起来。
我想这可能是你正在寻找的:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int players, daysPlayed, totalPlayed;
ifstream fin ("duomenys.txt");
fin >> players; // number of players.
for( int playerCount = 1; playerCount <= players; playerCount++ ) {
totalPlayed = 0;
fin >> daysPlayed; // number of days a player played games.
for ( int dayCount = 1; dayCount <= daysPlayed; dayCount++ ) {
int daysGameCount;
fin >> daysGameCount; // amount of games a player played on each day.
totalPlayed += daysGameCount;
}
cout << totalPlayed << endl;
// ... do whatever you want with the result
}
}
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 在java中解决这段代码时面临循环中的问题
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 在 for 循环中查找问题时遇到困难
- 嵌套for循环C++的问题(初学者)
- 退出简单while循环时出现问题
- 循环无限运行C++解决骑士之旅问题
- Visual Studio 2017循环自动向量化问题
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 嵌套While循环不起作用(C++问题)
- 使用 GMP 的 while 循环出现问题
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- OpenMP for 循环并行性问题
- 这个返回元素位置的基于循环的函数有什么问题?
- 为什么循环会导致指针出现问题?
- 使用基于范围的循环迭代矢量时出现问题
- 在 c++ 中循环遍历数组时遇到问题
- 运行无限循环的最小二叉树问题
- 在C++中循环访问自定义结构列表的小问题
- 在 for 循环中更新两个变量时遇到问题C++