【问题标题】:Using input in the main body so the user can edit在主体中使用输入,以便用户可以编辑
【发布时间】:2016-11-02 00:01:45
【问题描述】:
#include <stdio.h>
#include <string.h>

enum Race {wood_elf, human, high_elf, khajiit, orc, dwarf};

enum Class {archer, assassin, healer, knight, mage, paladin};

typedef struct Character
{
    char name[20];
    enum Class class_;
    enum Race race_;
    int perception;
    int stealth;
    int strength;
    int charisma;
    int agility;
    int intelligence;   
    int health;
    int magika;
    int stamina; 
} Character;

int main(int argc, char* argv[])
{

    int mc = 1;

    while(mc != 2) {
        printf("Welcome to Ceatures and Computers, please enter a number to select an option: \n\n");
        printf("1.) Create Character \n");
        printf("2.) Quit \n");

        scanf("%i", &mc);

        if(mc == 1)
        {
        }

        if(mc == 2)
        {
            printf("Thank you for playing. \n");
        }

        if(mc > 2)
            printf("Please enter number 1 or 2 to select a menu option. \n");
    }
    return 0;   
}

我目前正在尝试制作一款基于文本的游戏,并且正在上大学。我很快就要离开了,我想我会试试这个网站,看看当我不能问老师的时候它会有多有用。我正在处理的当前部分是让用户输入他们的角色名称、职业、种族,以及具有最大数量和总和的技能,就像 Fallout 一样。希望一切顺利,我可以回到这个网站寻求帮助,并有更多的知识来帮助他人。

【问题讨论】:

  • 如果mc 小于1 怎么办?
  • 总是检查来自scanf()的返回值;如果它显示“0”,则可能表示用户在您希望他们输入12 的位置输入a

标签: c input enums main


【解决方案1】:

欢迎。
一些建议

如果用户输入像“one”这样的文字,会发生什么?
始终检查错误

if (scanf("%i", &mc) != 1){
    printf("incorrect input.\n");
    return 1;
}

还要检查用户输入的值是否正确。如果不采取适当的行动。

使用这个:

typedef struct{
    //...
} Character;

而不是(在您的情况下这是多余的命名):

typedef struct Character{
        //...
} Character;

我希望这会有所帮助。

【讨论】:

  • 我不确定您使用typedef 建议的目的是什么(我可以猜到,但我不确定)。请注意,如果您使用任何 POSIX 标头,using _t as a suffix for a type name 是为系统保留的,因此您不应将约定用于您自己的类型。我也不相信你的“最后但并非最不重要”的评论——我不同意它(尤其是因为打印这样的值需要&lt;inttypes.h&gt; 和诸如PRId32PRIX32 之类的宏)。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-07
  • 1970-01-01
  • 2020-08-15
  • 2018-08-01
相关资源
最近更新 更多