加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用scanf()输入字符的问题

发布时间:2020-12-16 09:09:44 所属栏目:百科 来源:网络整理
导读:我试图将一个角色输入一个链表,其中的角色可以是’A’,’a’,’G’,’g’,’T’,’t’,’C’或’c’. 我还不熟悉C,我知道我搞砸了一些东西: do{ printf ("nEnter a new nucleotide: n"); scanf("%c",newChar); /* Checking */ if(newChar == 'A' || newCh
我试图将一个角色输入一个链表,其中的角色可以是’A’,’a’,’G’,’g’,’T’,’t’,’C’或’c’.

我还不熟悉C,我知道我搞砸了一些东西:

do{
  printf ("nEnter a new nucleotide: n");
  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 ("nBad Element");
  }
}while(newChar != 'x');

newChar初始化为垃圾值,在本例中为“q”.

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

出于某种原因,无论newChar中有什么价值,它都会跳转到其他地方.它也会直接跳过scanf,无需等待用户输入,每次循环时都会执行两次循环.谁能告诉我哪里出错了?

完整计划:

#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 ("nnList is empty");
    return;
  }
  loc=start;
  printf("nnList is : ");
  while(loc!=NULL){
    printf ("%c",loc->nucleotide);
    loc=loc->point;
  }
  printf ("n");
}

/* Finds and displays percentage of the chain made up of each nucleotide. */
void Percentage(int size){
  struct node *loc;
  if(start == NULL){
    printf ("nnList is empty");
    return;
  }
  loc=start;
  printf("nnList 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 ("n"); 

  /* 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("nA: %f",Adouble);
  printf("nG: %f",Gdouble);
  printf("nT: %f",Tdouble);
  printf("nC: %f",Cdouble); 
}

/* There be dragons beyond here */
int main(){
  int navigate,size =0;
  char newChar = 'q';
  do{ /* Menu */
    printf("n 1. Create / Extend Sequencen");
    printf("n 2. Display Sequencen");
    printf("n 3. Count n");
    printf("n 0. Exit n");
    printf("nPlease select an option (0 to 3)n");
    scanf("%d",&navigate);  
    switch (navigate){
      case 0: /* Exit */
        break;
      case 1: /* Add nucleotides */
        do{
          printf ("nEnter a new nucleotide: n");
          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 ("nBad Element");
          }
        }while(newChar != 'x');
        break;
      case 2:
        Display();
        break;
      case 3:
        Percentage(size);
        break;
      default:
        printf ("nnBad choice. Please select another.n");
    }
  } while (navigate !=0); 
  return 0 ;
}

解决方法

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

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读