我只想将这些值硬编码到一个表中。当我尝试使用二维数组时,我遇到了处理字符和整数的问题。当我做一个结构体时,我已经得到了这个结果,但它并没有将信息分成几列,我不知道如何以这种方式对它进行格式化。 (我只做了3行刚开始时,如果我让他们的工作,其余的将仅仅是相同的)结构数组
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
typedef struct table
{
std::string game;
int year;
float rating;
int num_voters;
}t;
void processTab(t*);
int main()
{
t tabl[2] = {0,0};
int i;
processTab(tabl);
for(i=0; i<2; i++)
{
std::cout << "Game: " << setw(20) << tabl[i].game;
std::cout << "\tYear: " << setw(4) << tabl[i].year;
std::cout << "\tRating: " << fixed << setprecision(2) << tabl[i].rating;
std::cout << "\tVoters: " << setw(6) << tabl[i].num_voters;
}
system("pause");
return 0;
}
void processTab(t*tab)
{
(tab[0].game, "twilight struggles");
tab[0].year = 2005;
tab[0].rating = 8.226;
tab[0].num_voters = 10690;
(tab[1].game, "Agricloa");
tab[1].year = 2007;
tab[1].rating = 8.17;
tab[1].num_voters = 23738;
(tab[2].game, "Puerto Rico");
tab[2].year = 2002;
tab[2].rating = 8.163;
tab[2].num_voters = 27433;
}
Table Data:
Game (0) Year (1) Rating (2) Num Voters (3)
Twilight Struggle 2005 8.226 10690
Agricola 2007 8.17 23738
Puerto Rico 2002 8.163 27433
Through the Ages 2006 8.153 8137
Power Grid 2004 8.02 21655
Le Havre 2008 7.972 9258
Eclipse 2011 7.968 3194
Brass 2007 7.911 5814
Dominion: Intrigue 2009 7.895 10889
Caylus 2005 7.878 13878
你的问题是什么? (也就是说你的结构看起来像C++中的C代码,你只需要写'struct table {};') –
可能的错误:你的t.game数据类型是char,你试图将字符串复制到它。可能的错误2:使用strcpy! – Thomas
除非'game'总是单个字符,否则''游戏''应该使用'std :: string'而不是'char'。另外,你的'strcpy'行不应该编译,但是如果你使用'std :: string'(你只需要使用'='运算符)就不会使用它们。 –