【发布时间】:2016-01-19 21:02:43
【问题描述】:
声明一个描述单个视频游戏的结构。 视频游戏有名称、类型、平台、开发商、发行年份、年龄下限、价格和 他们是否支持应用内购买。您需要为每个选择适当的数据类型 要存储在结构中的信息。
- 将结构命名为:Video_Game。
- 在main函数本地声明三个视频游戏结构变量,分别称为game1、game2 和游戏 3。
- 将上述 Candy Crush Saga (King, 2012) 示例的详细信息分配给 game1 的成员。
- 将上述 Halo 4(343 Industries,2014)示例的详细信息分配给 game2 的成员。
- 分配给game3的成员,你最喜欢的游戏的详细信息……如果你不玩游戏, 检查你的智能手机......你肯定在那里玩过什么......如果没有,检查相关的应用商店 排行榜并找到一款可能成为您新宠的游戏!
- 接下来,声明一个名为 print_video_game_details() 的函数,该函数接受一个参数 那是指向前面声明的视频游戏结构的指针。在这个函数中,打印出 传递给函数的游戏,如上图所示的 Candy Crush Saga 和 Halo 4 风格 示例。
- 接下来,从 main 调用 print_video_game_details 函数 3 次,传入 成员全部设置后,game1、game2、game3的地址。
到目前为止我的代码:
#include <stdio.h>
#include <string.h>
struct video_game
{
char* title;
char* genre;
char* developer;
int year;
char* platform;
int lower_age;
float price;
char* inapp_purchase;
}game1, game2;
void print_video_game_details()
{
for(int i =1; i<=3; i++)
{
printf("Title: %s", game[i].title); // game[i] is showing an error "undeclared"
printf("Genre: %s", game[i].genre);
printf("Developer: %s", game[i].developer);
printf("year of release: %d", game[i].year);
printf("platform: %s", game[i].platform);
printf("lower age: %d", game[i].lower age);
printf("price: %f", game[i].price); //is showing an error "incompatible"
printf("inapp_purchase: %s", game[i].inapp_purchase);
}
}
int main(void)
{
game1.title = "Candy crush saga";
game1.genre = "Match-Three Puzzle";
game1.developer = "King";
game1.year = "2012";
game1.platform = "Android, ios, Windows Phone";
game1.lower_age = "7";
game1.price = "$0.00";
game1.inapp_purchase = "yes";
print_video_game_details();
}
我无法打印出结构,因为它无法编译。
错误:
prog.c: In function 'print_video_game_details':
prog.c:27:33: error: 'struct video_game' has no member named 'lower'
printf("lower age: %d", game[i].lower age);
^
prog.c:27:40: error: expected ')' before 'age'
printf("lower age: %d", game[i].lower age);
^
prog.c: In function 'main':
prog.c:39:15: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
game[0].year = "2012";
^
prog.c:41:20: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
game[0].lower_age = "7";
^
prog.c:42:16: error: incompatible types when assigning to type 'float' from type 'char *'
game[0].price = "$0.00";
^
【问题讨论】:
-
您在 main 末尾缺少一个右括号 ( } )。如果它不能编译,你应该发布编译器给你的错误信息。
-
确保你在 main 的末尾,右括号之前调用你的
print_video_game_details方法。 -
@fefe 谢谢。我已经编辑了这个 sn-p。你现在可以看看吗?
-
还有一个有用的提示,
game[i]指的是名为game的数组中的索引i,但您的程序中没有任何数组。现在,让print_video_game_details()打印关于game1 的信息。 -
@AlexPogue 亲爱的先生,问题是我必须使用此代码打印 3 组游戏详细信息,因此我放置了游戏 i 而不是游戏 1...
标签: c function struct parameters printf