2012-06-17 129 views
-1

我只想将这些值硬编码到一个表中。当我尝试使用二维数组时,我遇到了处理字符和整数的问题。当我做一个结构体时,我已经得到了这个结果,但它并没有将信息分成几列,我不知道如何以这种方式对它进行格式化。 (我只做了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 
+0

你的问题是什么? (也就是说你的结构看起来像C++中的C代码,你只需要写'struct table {};') –

+0

可能的错误:你的t.game数据类型是char,你试图将字符串复制到它。可能的错误2:使用strcpy! – Thomas

+0

除非'game'总是单个字符,否则''游戏''应该使用'std :: string'而不是'char'。另外,你的'strcpy'行不应该编译,但是如果你使用'std :: string'(你只需要使用'='运算符)就不会使用它们。 –

回答

4

我认为你正在寻找的是<iomanip>

#include <iomanip> 

std::cout << "Game: " << setw(20) << tabl[i].game; 
std::cout << "\tYear: " << setw(4) << tabl[i].year; 
std::cout << "\tRating: " << fixed << setprecision(3) << tabl[i].rating; 
std::cout << "\tVoters: " << setw(6) << tabl[i].num_voters; 
std::cout << std::end; 

注意事项:
setw写出的东西的时候,所以它总是会至少在一定宽度
setprecision指定多少个小数位显示
fixed使得莲花添加填充g点从不使用使用科学计数法

您的game成员是一封信,您正试图为其分配一个字符串。不要在C++中使用strcpy,而应使用std::string类。

#include <string> 
struct table 
{ 
    std::string game; 
    int year; 
    double rating; 
    int num_voters; 
}; 

避免using namespace std;,当你到了许多的命名空间复杂的代码,那几个字母是一个很小的代价,以避免混乱。
避免endl:它刷新缓慢的缓冲区。如果您只是想换行,请使用'\n'。正确

std::vector<table> tbal = { 
       {"twilight struggles ", 2005, 8.226, 10690}, 
       {"Agricola   ", 2007, 8.17 , 23738}, 
       {"Puerto Rico   ", 2002, 8.163, 27433} 
       }; 
+0

这是否是函数定义(在main之后)? – user1251302

+0

避免使用命名空间标准的建议是不恰当的。一个人必须把智力应用于一切。有坏境的环境,好的场景,初学者只能访问后一类的场景。但更重要的是,不应该教导使用机械规则。应该教会思考。其次,考虑'endl'效率的建议是不好的。为了提高效率,不要使用iostreams。另外,不要担心小东西。初学者不应该被教导去思考微观效率。这是初学者意大利面条最常见的原因。 –

+0

-1为“如果你有一个更新的编译器”,这是ungood。自从ARM(C++ 98之前)以来,一直支持聚合的初始化。 –

1

首先,你需要定义table(为struct坏名,顺便说一句):
此外,您还可以使用新的初始化语法来初始化列表。您正试图使用​​game来存储字符串,但已将其定义为只有一个char。您可能希望将其改为std::string

那么你可能想要做一个operator<<重载的格式,以参考table作为类型。 @MooingDuck已经涵盖了格式化本身相当不错,所以它主要是你怎么包的问题:

std::ostream &operator<<(std::ostream &os, table const &t) { 
    os << "Game: " << setw(20) << t.game; 
    os << "\tYear: " << setw(4) << t.year; 
    os << "\tRating: " << fixed << setprecision(2) << t.rating; 
    return os << "\tVoters: " << setw(6) << t.num_voters;  
} 

伴随着的是,你几乎可以肯定想从一个数组更改tablstd::vector<table>

std::vector<tabl> tabl; 

然后处理表变为:

其他
std::copy(tabl.begin(), tabl.end(), std::ostream_iterator<table>(std::cout, "\n")); 

一个小细节:你似乎有两个完全不同的/ SE parate功能,都命名为processTab。您可能想要重命名其中至少一个。只是看了看他们,我可能会打一个initializeTable或其他的东西。

2

简短回答:使用std::vector,不是原始数组。

以下是接近原来的代码,因为我可以做到,引入的新功能的最小数量为你:

#include <assert.h>   // assert 
#include <iostream>   // std::cout, std::endl 
#include <stddef.h>   // ptrdiff_t 
#include <string>   // std::string 
#include <utility>   // std::begin, std::end 
#include <vector>   // std::vector 
using namespace std; 

typedef ptrdiff_t  Size; 

template< class Container > 
Size countOf(Container& c) { return end(c) - begin(c); } 

struct Game 
{ 
    string game; 
    int  year; 
    double rating; 
    int  num_voters; 
}; 

void initialize(vector<Game>& games) 
{ 
    assert(countOf(games) == 0); 
    games.resize(3); 

    games[0].game = "twilight struggles"; 
    games[0].year = 2005; 
    games[0].rating = 8.226; 
    games[0].num_voters = 10690; 

    games[1].game = "Agricloa"; 
    games[1].year = 2007; 
    games[1].rating = 8.17; 
    games[1].num_voters = 23738; 

    games[2].game = "Puerto Rico"; 
    games[2].year = 2002; 
    games[2].rating = 8.163; 
    games[2].num_voters = 27433; 
} 

int main() 
{ 
    vector<Game> games; 

    initialize(games); 
    for(int i = 0; i < countOf(games); ++i) 
    { 
     cout << i << endl; 
     cout <<"\tGame: " << games[i].game << endl; 
     cout<<"\tYear: " << games[i].year << endl; 
     cout<<"\trating: " << games[i].rating << endl; 
     cout<<"\tnum voters: " << games[i].num_voters << endl; 
    } 
} 

有办法只是数据更直接地宣布,包括大括号初始化;看看你的C++教科书。