Use scanf () input characters

I am trying to enter a role into a linked list, where the role can be’A’,’a’,’G’,’g’,’T’,’t’,’ C’or’c’.

I am not familiar with C yet, I know I messed up something:

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 to a garbage value, in this case “q”.

Enter’x’ to exit the loop, enter any acceptable value and call AddToSequence(), any unacceptable value will be accepted To the warning.

For some reason, no matter what value in newChar, it will jump to other places. It will also skip scanf directly, without waiting for user input, it will execute every time the loop Two loops. Can anyone tell me what went wrong?

Complete plan:

#include
#include

/*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;< br /> 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 do not handle Newline character. The %c specifier will not skip spaces. Try:

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

Or you can add an explicit test.

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

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

I am not familiar with C, I know I messed up something:

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 to a garbage value, in this case "q".

< p>Enter'x' to exit the loop, enter any acceptable value to call AddToSequence(), any unacceptable value will receive a warning.

For some reason, no matter what value is in newChar, It will jump to other places. It will also skip scanf directly without waiting for user input. It will execute two loops each time it loops. Can anyone tell me what went wrong?

Complete plan:

#include
#include

/*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;< br /> 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);< br /> 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;
Ado uble =(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 do not handle newlines. The %c specifier will not skip spaces. Try :

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

Or you can add a clear test.

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

Leave a Comment

Your email address will not be published.