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

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

问题描述

我想输入一个字符到一个链表,其中字符可以是'A','A','G','G','T','T','C'或' C'。

我还没有熟悉C,我知道我搞砸的东西在这里:

  {做
  的printf(\\ n输入一个新的核苷酸:\\ n);
  scanf函数(%C,&安培; newChar);
          / *检查* /
  如果(newChar =='A'||
     newChar =='一'||
     newChar =='G'||
     newChar =='G'||
     newChar =='T'||
     newChar =='T'||
     newChar =='C'||
     newChar =='C')
  {
    AddToSequence(newChar);
    大小++;
  }其他{
    的printf(\\ NBAD元素);
  }
}而(newChar ='X'!);

newChar初始化为垃圾值,在此情况下,Q

输入的x退出循环,在进入任何可接受的值调用AddToSequence(),以及任何不可接受的值获得一个警告。

由于某些原因,不管是什么值是newChar,它会跳转到其他人。这也将直接跳过去scanf函数不等待用户输入和每次循环的时间做两个循环。谁能告诉我,我要去哪里错了?

全部程序:

 #包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;/ *为节点结构声明* /
结构节点{
   焦炭核苷酸;
   结构节点*点;
}*开始;/ *添加一个核苷酸链。如果创建不存在链存在一个新的链接列表。* /
无效AddToSequence(CHAR核苷酸){
  结构节点*禄,*第一;
  //动态内存分配的节点
  首先=(结构节点*)malloc的(的sizeof(结构节点));
  第一代和GT;核苷酸=核苷酸;
  第一代和GT;点= NULL;
  如果(开始== NULL){
    / *如果列表是空的* /
    开始=第一;
  }其他{
    / *结尾插入的元素* /
    LOC =启动;
    而(loc->点!= NULL){
      LOC = loc->点;
      loc->点=第一;
    }
  }
}/ *显示元件* /
无效显示(){
  结构节点*禄;
  如果(开始== NULL){
    的printf(\\ n \\ NLIST为空);
    返回;
  }
  LOC =启动;
  的printf(\\ n \\ NLIST是:);
  而(LOC!= NULL){
    的printf(%C,loc->核苷酸);
    LOC = loc->点;
  }
  的printf(\\ n);
}/ *查找和链条的比例显示由每个核苷酸。 * /
空隙率(INT大小){
  结构节点*禄;
  如果(开始== NULL){
    的printf(\\ n \\ NLIST为空);
    返回;
  }
  LOC =启动;
  的printf(\\ n \\ NLIST是:);
  INT A = 0,G = 0,T = 0,C = 0;
  双Adouble = 0,修正gdouble = 0,Tdouble = 0,Cdouble = 0;
  而(LOC!= NULL){
    如果(loc->核苷酸=='A'||'A'){A ++;}
    如果(loc->核苷酸=='G'||'G'){G ++;}
    如果(loc->核苷酸=='T'||'T')【T ++;}
    如果(loc->核苷酸=='C'||'C'){C ++;}
    LOC = loc->点;
  }
  的printf(\\ n);  / *转换为加倍百分比为int损失precision * /
  Adouble = A;
  修正gdouble = G;
  Tdouble = T;
  Cdouble = C;
  Adouble =(Adouble /大小)* 100;
  修正gdouble =(修正gdouble /大小)* 100;
  Tdouble =(Tdouble /大小)* 100;
  Cdouble =(Cdouble /大小)* 100;
  的printf(\\娜:%F,Adouble);
  的printf(\\ NG:%F,修正gdouble);
  的printf(\\ NT:%F,Tdouble);
  的printf(\\ NC:%F,Cdouble);
}/ *有超出此龙* /
诠释主(){
  INT导航,大小= 0;
  焦炭newChar ='Q';
  做{/ *菜单* /
    的printf(\\ n 1.创建/ extend序列\\ n);
    的printf(\\ n 2.显示顺序\\ n);
    的printf(\\ n 3.计数\\ n);
    的printf(\\ n 0退出\\ n);
    的printf(\\ n请选择一个选项(0〜3)\\ n);
    scanf函数(%d个,&安培;导航);
    开关(导航){
      案件0:/ * *退出/
        打破;
      案例1:/ *添加核苷酸* /
        做{
          的printf(\\ n输入一个新的核苷酸:\\ n);
          scanf函数(%C,&安培; newChar);
          / *一些错误检查* /
          如果(newChar =='A'|| newChar =='一'|| newChar =='G'|| newChar =='G'|| newChar =='T'|| newChar =='T'|| newChar =='C'|| newChar =='C'){
            AddToSequence(newChar);
            大小++;
          }其他{
            的printf(\\ NBAD元素);
          }
        }而(newChar ='X'!);
        打破;
      案例2:
        显示();
        打破;
      案例3:
        百分比(大小);
        打破;
      默认:
        的printf(\\ n \\ NBAD选择请选择其他\\ n);
    }
  }而(导航!= 0);
  返回0;
}


解决方案

您不处理​​换行符。在%C 说明不跳过空白。尝试:

  scanf函数(%C,&安培; newChar);
    / * ^< - 让`scanf`吃的换行符。 * /

也许增加一个明确的测试。

  scanf函数(...);
如果(newChar =='\\ n')
    继续;

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'.

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

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 is initialized with a junk value, in this case 'q'.

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

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?

Full program:

#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 ("\n\nList is empty");
    return;
  }
  loc=start;
  printf("\n\nList 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 ("\n\nList is empty");
    return;
  }
  loc=start;
  printf("\n\nList 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 Sequence\n");
    printf("\n 2. Display Sequence\n");
    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 ("\n\nBad choice. Please select another.\n");
    }
  } while (navigate !=0); 
  return 0 ;
}

解决方案

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 == '\n')
    continue;

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

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