C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用scanf()输入字符的问题大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图将一个角色输入一个链表,其中的角色可以是’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初始化为@L_618_2@值,在本例中为“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 ("\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;

大佬总结

以上是大佬教程为你收集整理的使用scanf()输入字符的问题全部内容,希望文章能够帮你解决使用scanf()输入字符的问题所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。