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

标签 c input linked-list

我正在尝试将一个字符输入到链表中,其中字符可以是 '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(),任何 Not Acceptable 值都会收到警告。

由于某种原因,无论newChar中有什么值,都会跳转到else。它还会直接跳过 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 ("\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 ;
}

最佳答案

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

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

或者添加一个显式测试。

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

关于c - 使用scanf()输入字符的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13710867/

相关文章:

C - 如何绘制星号中相邻矩形条的轮廓

c - 从字节的 char[] 中获取 int

javascript - 使用按钮在文本区域中输入文本,希望每个条目都从新行开始

java - 在 Java 中使用带有链表的迭代器

php - 使用这样的表达式而不是使用普通表达式的好处

c - 这两种具有结构的 typedef 实现是否等效?

php - 将输入与掩码匹配

Javascript - 通过用户输入和排序按钮创建的列表

c - 我试图通过递归打印反向链接列表,但它不起作用

C - 反向递归链表 : why use double pointer?