这是我的代码:
enum DifficultyKind
{
Normal,
Hard,
Insane
} DifficultyKind;
typedef struct Target_Data
{
my_string name;
int hit_id;
int dollarvalue;
enum DifficultyKind difficulty;
} Target_Data;
enum DifficultyKind read_difficulty_kind (const char *prompt)
{
int temp;
enum DifficultyKind result;
printf("%s\n", prompt);
printf("\n");
printf("1: Normal Difficulty \n");
printf("\n");
printf("2: Hard Difficulty \n");
printf("\n");
printf("3: Insane Difficulty \n");
printf("\n");
temp = read_integer("Please make a selection between 1 and 3: \n");
if (temp < 1) {
printf("\n");
printf("You did not make a selection between 1 and 3\n");
printf("\n");
temp = read_integer("Please make a selection between 1 and 3: \n");
}
if (temp > 3) {
printf("\n");
printf("You did not make a selection between 1 and 3\n");
printf("\n");
temp = read_integer("Please make a selection between 1 and 3: \n");
}
result = temp - 1;
return result;
}
Target_Data read_target_data (const char *prompt)
{
Target_Data result;
enum DifficultyKind Difficulty;
printf("%s\n", prompt);
result.name = read_string("Enter name: ");
result.hit_id = read_integer("Enter hit ID: ");
if (result.hit_id < 0) {
printf("Please enter a value of 0 or higher \n");
result.hit_id = read_integer("Enter hit ID: ");
}
result.dollarvalue = read_integer("Enter $ value of target: ");
if (result.dollarvalue < 0) {
printf("Please enter a value of 0 or higher \n");
result.dollarvalue = read_integer("Enter $ value of target: ");
}
Difficulty = read_difficulty_kind("Please select the level of difficulty this bounty is from the below options:");
return result;
}
void print_target_data (Target_Data *toPrintData)
{
printf("\nDifficulty: %d, Target: %s, Hit ID: %i, $%i,\n", toPrintData->difficulty, toPrintData->name.str, toPrintData->hit_id, toPrintData->dollarvalue);
}
int main()
{
Target_Data *Target_Data;
read_target_data("Please enter the details of your bounty: ");
print_target_data(&Target_Data);
}
程序运行时和输入详细信息后,我得到以下内容:
Please enter the details of your bounty:
Enter name: Jonathan
Enter hit ID: 10
Enter $ value of target: 500
Please select the level of difficulty this bounty is from the below options:
1: Normal Difficulty
2: Hard Difficulty
3: Insane Difficulty
Please make a selection between 1 and 3:
1
Difficulty: 10, Target: , Hit ID: 0, $0,
我已经尝试了很多不同的方法,并且全神贯注地寻求解决方案,但并不知道该怎么做。
为什么难以读出我为点击ID输入的数字......其他细节甚至不显示?
这也是我编译时收到的警告信息:
BountyHunter.c:96:20: warning: incompatible pointer types passing
'Target_Data **' (aka 'struct Target_Data **') to parameter of type
'Target_Data *' (aka 'struct Target_Data *'); remove &
[-Wincompatible-pointer-types]
print_target_data(&Target_Data);
^~~~~~~~~~~~
BountyHunter.c:87:38: note: passing argument to parameter 'toPrintData' here
void print_target_data (Target_Data *toPrintData)
有人请帮忙!
答案 0 :(得分:0)
完全猜测,查看代码结构,并假设read_xxx
函数已得到很好的实现,您的main
应该是:
int main (void)
{
Target_Data Target_Data;
Target_Data = read_target_data("Please enter the details of your bounty: ");
print_target_data(&Target_Data);
}
答案 1 :(得分:0)
由于缺少验证读取所需的所有必要代码,因此代码中存在大量错误,因此难以识别。也就是说,我重写了你的例子,使用静态声明的Target_Data
,它通过地址传递给read_target_data
,这消除了在read_target_data
或{{1}内动态声明结构的需要。 (这可能会使你目前的学习过于复杂)
我不知道你是来自java,还是试图复制java代码,但read_difficulty_kind
完全没有意义。
我还在toPrintData->name.str
err = -1
添加了DifficultyKind
,以便验证代码中返回的值。这取决于您如何执行此操作,但您需要 验证 每个输入,以确保您实际使用的是实际值而不是尝试处理未初始化的值。
(作为旁注,C通常使用小写变量名,为java和C ++留下enum
和camelCase
名称,并保留{{1用于宏的名称等等......当然,它是样式,所以完全取决于你)
话虽如此,这是我认为与你的意图保持一致的重新设计的例子。仔细看看,如果您有任何其他问题,请告诉我。输入例程只是调用UpperCase
(这应该更适当地调用UPPERCASE
并随后调用scanf
,但这是另一天的调用)
fgets
示例使用/输出
sscanf