【问题标题】:Is there a way to create a gender evaluation有没有办法创建性别评估
【发布时间】:2019-04-23 23:04:18
【问题描述】:

我正在尝试让 C 程序的用户输入 M 代表男性和 F 代表女性 如果不满足 M 或 F 则循环。 下面我尝试了以下代码,但它循环两次给出正确答案。 例如,在输入 M 后,它会循环并打印

FALSE 性别 .. 请输入 M 代表男性,M 代表 女性

有什么办法可以解决吗?

目标是,如果用户输入 M 或 F,则无需再次输入即可工作。
如果还有其他问题,它可能会询问多次

 #include "Gender.h"
 #include<stdio.h>


  char sex;
  void Gender(){

  printf("\nEnter Student Gender ('example M for Male  F for Female):\n");

  scanf(" %s",&sex);



      while (sex != "M" || sex != "M"){
       printf ("\n FALSE gender .. please enter M for Male and M for Female:\n");
        scanf(" %s",&sex);
        printf("\nStudent Sex :%c", sex,"\n");
        return;
      }


  }

【问题讨论】:

  • while 语句中的表达式检查了两次"M"。此外,要比较 C 中的字符串,请使用 strcmp 函数。
  • 并使用&amp;&amp; 而不是||
  • "%s" 消耗前导空格,因此不需要 " %s" 中的空格 - 但是,您不检查返回(忽略手动 EOF)并且您无法删除任何无关的第一个字符之外的字符(例如,在读取 sexint c = getchar(); while (c != '\n' &amp;&amp; c != EOF) c = getchar(); 之后)这超出了您在 char sex"%s" 之间的类型不匹配,期望一个指向 char 的指针具有足够的存储空间来保存字符串(包括 nul -终止字符)
  • M 代表男性,M 代表女性(在循环内)?我认为女士们有权感到恼火。循环内的return 表示人们可以重试一次,但未经测试。您还丢弃了用户输入的结果——这不是很好。您当然应该返回所选的性别吗?可能值得增加重试次数并阻止人们进入循环。例如,在 10 次尝试失败后停止。
  • 我可以否决分配此内容的讲师吗?

标签: c


【解决方案1】:

sex 是单个 char 变量,但您将其视为指向字符串的指针。

  1. 将您的scanf 调用更改为在格式字符串中使用%c(单个字符),而不是%s
  2. sex 值的测试更改为使用单引号(即if (sex == 'M'))。

请注意,尽管如果变量 sex 被视为单个 char 时,此代码不应该需要字符串比较,但如果您要在未来的项目中比较字符串,您会不要使用运算符==!= - 而是使用&lt;string.h&gt; 中的函数strcmp

【讨论】:

  • 应该指出当用户确实输入了不是 M 或 F 的内容时,您应该阅读到换行符并在流中处理该数据
【解决方案2】:

几件事,我提供了一个使用 cmets 的实现...

包括 ctype.h 以使用 toupper(3) 并允许大写和小写输入。 你可以使用字符比较,也可以使用字符串比较,我都给了你,选择一个。

#include "Gender.h"
#include <stdio.h>

//you should really define these in your Gender.h file...
#define MALE "M"
#define FEMALE "F"
//you should really define these in your Gender.h file...
static const char* GenderAllowed = MALE" for Male and "FEMALE" for Female";
static const char* GenderPrompt = "Enter Student Gender";

//compare as string[1] - keep either this
#include <string.h>
int sexValidc( char* sex ) {
    return ( (0==strncmp(sex,MALE,1)) || (0==strncmp(sex,FEMALE,1)) );
}

//compare only first character - or keep this
#include <ctype.h>
int sexValids( char* sex ) {
    char sexch = toupper(*sex); //fold first character to upper case
    return ( (*MALE==sexch) || (*FEMALE==sexch) );
}

char Gender() {
    char sex[69+1]; //since you wanted to use %s, need a buffer, your &char allows stack overflow & wild memory pointer-ing
    printf("\n%s (%s):\n",GenderPrompt,GenderAllowed); //DRY - Dont Repeat Yourself...
    int done = 0;
    while( !done ) {
        scanf(" %69s",sex); //avoid stackoverflow :-)
        if( !sexValidc( sex ) ) {
//printf("blech, entered: %s\n",sex); //debugging, remove when comfortable
            printf("\n FALSE gender .. %s %s:\n",GenderPrompt,GenderAllowed);
        }
        else { //valid
            done = 1;
        }
    }
    printf("\nStudent Sex :%c\n", *sex);
    return *sex; //once you enter valid value, return it...
}

int main() {
    Gender();
}

另请阅读有关自动分配缓冲区空间的内容,difference between %ms and %s scanf

【讨论】:

  • 我正在尝试这种方式 char sex; void Gender(){ printf("\n输入学生性别(例如 M 代表男性 F 代表女性):\n"); scanf("%c",&sex); switch(sex){ case 'M': case 'm': printf("Student Sex : Male");休息;案例'F':案例'f':printf(“学生性别:女性”);休息;默认值:printf("\n FALSE 性别.. 请输入 M 代表男性和 F 代表女性:\n"); } }
  • 您只能使用其中一个 sexValid 函数 - 我已将名称更改为以 'c' 和 's' 结尾,并猜到了您想要的...
猜你喜欢
  • 2019-06-02
  • 1970-01-01
  • 1970-01-01
  • 2014-08-07
  • 2013-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-30
相关资源
最近更新 更多