使用 scanf() 输入字符的问题 [英] Problems with character input using scanf()

查看:24
本文介绍了使用 scanf() 输入字符的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个字符输入到一个链表中,该字符可以是 'A'、'a'、'G'、'g'、'T'、't'、'C' 或 'c'.

I'm trying to input a character into a linked list, where the character can be 'A','a','G','g','T','t','C' or 'c'.

我还不熟悉 C,我知道我在这里搞砸了:

I'm not yet familiar with C and I know I've screwed something up here:

do{
  printf ("
Enter a new nucleotide: 
");
  scanf("%c",&newChar);
          /* Checking */
  if(newChar == 'A' ||
     newChar == 'a' || 
     newChar == 'G' || 
     newChar == 'g' || 
     newChar == 'T' || 
     newChar == 't' || 
     newChar == 'C' || 
     newChar == 'c' )
  {
    AddToSequence(newChar);
    size++;
  } else {
    printf ("
Bad Element");
  }
}while(newChar != 'x');

newChar 被初始化为垃圾值,在本例中为 'q'.

newChar is initialized with a junk value, in this case 'q'.

输入x"退出循环,输入任何可接受的值会调用 AddToSequence(),任何不可接受的值都会收到警告.

Entering 'x' exits the loop, entering any acceptable value calls AddToSequence(), and any unacceptable value gets a warning.

由于某种原因,无论newChar中的值是什么,它都会跳转到else.它还会直接跳过 scanf 而不等待用户输入,并且每次循环时执行两次循环.谁能告诉我哪里出错了?

For some reason, no matter what value is in newChar, it will jump to the else. It will also jump straight past the scanf without waiting for entry from the user and do two loops every time it loops. Can anyone tell me where I'm going wrong?

完整程序:

#include<stdio.h>
#include<stdlib.h>

/*Structure declaration for the node*/
struct node{
   char nucleotide;
   struct node *point;
}*start;

/* Adds a nucleotide to the chain. Creates a new linked list if no chain exists exists.*/
void AddToSequence(char nucleotide){
  struct node *loc, *first;
  //Dynamic memory is been allocated for a node
  first=(struct node*)malloc(sizeof(struct node));
  first->nucleotide=nucleotide;
  first->point=NULL;
  if(start==NULL){
    /*If list is empty*/
    start=first;
  }else{
    /*Element inserted at the end*/
    loc=start;
    while(loc->point!=NULL){
      loc=loc->point;
      loc->point=first;
    }
  }
}

/* Display elements */
void Display(){
  struct node *loc;
  if(start == NULL){
    printf ("

List is empty");
    return;
  }
  loc=start;
  printf("

List is : ");
  while(loc!=NULL){
    printf ("%c", loc->nucleotide);
    loc=loc->point;
  }
  printf ("
");
}

/* Finds and displays percentage of the chain made up of each nucleotide. */
void Percentage(int size){
  struct node *loc;
  if(start == NULL){
    printf ("

List is empty");
    return;
  }
  loc=start;
  printf("

List is : ");
  int A = 0, G =0, T =0, C = 0;
  double Adouble = 0, Gdouble =0, Tdouble=0, Cdouble=0;
  while(loc!=NULL){
    if(loc->nucleotide=='A' || 'a'){A++;}
    if(loc->nucleotide=='G' || 'g'){G++;}
    if(loc->nucleotide=='T' || 't'){T++;}
    if(loc->nucleotide=='C' || 'c'){C++;}    
    loc=loc->point;   
  }
  printf ("
"); 

  /* Convert to double for percentages as int loses precision */
  Adouble =A;
  Gdouble =G;
  Tdouble =T;
  Cdouble =C; 
  Adouble =(Adouble/size)*100;
  Gdouble =(Gdouble/size)*100;
  Tdouble =(Tdouble/size)*100;
  Cdouble =(Cdouble/size)*100; 
  printf("
A: %f", Adouble);
  printf("
G: %f", Gdouble);
  printf("
T: %f", Tdouble);
  printf("
C: %f", Cdouble); 
}

/* There be dragons beyond here */
int main(){
  int navigate, size =0;
  char newChar = 'q';
  do{ /* Menu */
    printf("
 1. Create / Extend Sequence
");
    printf("
 2. Display Sequence
");
    printf("
 3. Count 
");
    printf("
 0. Exit 
");
    printf("
Please select an option (0 to 3)
");
    scanf("%d",&navigate);  
    switch (navigate){
      case 0: /* Exit */
        break;
      case 1: /* Add nucleotides */
        do{
          printf ("
Enter a new nucleotide: 
");
          scanf("%c",&newChar);
          /* Some error checking */
          if(newChar == 'A' || newChar == 'a' || newChar == 'G' || newChar == 'g' || newChar == 'T' || newChar == 't' || newChar == 'C' || newChar == 'c' ){
            AddToSequence(newChar);
            size++;
          } else {
            printf ("
Bad Element");
          }
        }while(newChar != 'x');
        break;
      case 2:
        Display();
        break;
      case 3:
        Percentage(size);
        break;
      default:
        printf ("

Bad choice. Please select another.
");
    }
  } while (navigate !=0); 
  return 0 ;
}

推荐答案

您不处理换行符.%c 说明符不会跳过空格.试试:

You don't handle the newline. The %c specifier doesn't skip blanks. Try:

scanf(" %c", &newChar);
    /* ^ <-- Makes `scanf` eat the newline. */

或者添加一个明确的测试.

Or maybe add an explicit test.

scanf(...);
if (newChar == '
')
    continue;

这篇关于使用 scanf() 输入字符的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆