Name G M S
Cart 1 0 1
Jane 0 1 0
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <algorithm>
using namespace std;
void scoreChanger();
string line;
int main()
{
string yn;
int ctr = 0;
ifstream infile;
infile.open("WiiTourney.txt");
if (infile.is_open())
{
cout << "This is your current score table: " << endl;
while(getline(infile, line))
{
ctr++;
cout << line << endl;
cout << ctr << endl;
}
cout << endl;
}
else
{
cout << "Unable to open file" << endl;
}
infile.close();
cout << endl;
cout << "Would you like to change the scores? " << endl;
cin >> yn;
transform(yn.begin(), yn.end(), yn.begin(), ::tolower);
if (yn == "yes")
{
scoreChanger();
}
else
{
infile.close();
return 0;
}
return 0;
}
void scoreChanger()
{
string name;
ofstream outfile;
outfile.open("WiiTourney.txt");
if (outfile.is_open())
{
cout << "Who won the game? " << endl;
cin >> name;
transform(name.begin(), name.end(), name.begin(), ::tolower);
if (name == "jane")
{
while(getline(outfile, line))
{
cout << line << endl;
}
}
for (int x = 0; x < line.length(); x++)
{
if (line[x] == 8 && line[x] != 'G')
{
}
}
}
else
{
cout << "Error opening file. " << endl;
exit(1);
}
}
假设我希望能够仅为购物车添加1个游戏列(G)。对我来说问题是我只想更改1
列中的G
,我知道我会遇到问题,只需循环搜索并找到1出现的实例,因为可能有多个1
在一行中。 我也在第while(getline(outfile, line))
行收到错误,上面写着“没有匹配函数来调用'getline(std :: ofstream&amp;,std :: string&amp;)'”
谢谢,非常感谢您的帮助。
答案 0 :(得分:0)
我的第一个想法是表的结构非常均匀,因此您可以使用列和行来确定特定分数的位置。
因为名称是唯一具有可变长度的元素(假设分数不超过9,因为这会产生2个字符),我会首先读取每一行的第一个单词并将其输入到数组中名字。
通过这个,您可以使用行索引和列索引找到特定元素。如果C ++没有包含基于行和列索引获取字符的函数,我会遍历文件并将每个字符添加到二维数组中的相应位置。
例如:
characters[0][0]
将从&#34; Names&#34;
开始返回N.当然,要检索得分,您需要使用特定行的名称长度来获取值:
characters[names[0].length()+1][1]
这会将G下的分数返回到列表中的第一个名称。