简体   繁体   中英

How to use a character to quit a loop in c Language?

How to use q as character to exit ? How to use q as a quit to this code. It loops till it gets the ASCII values of q. `

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int pos=0,neg=0,zero=0,i=0,num;
  printf("Input numbers.");
  scanf("%d",&num);
  for(i=0;num!="q";i++)
  {

      if(num>0)
      {
          pos++;
      }
      if(num<0)
      {
          neg++;
      }
      if(num==0)
      {
          zero++;
      }
  }
  printf("You entered \n\tpositive number::: %d times\nNegative number:::%d times\nZero:::%d times",pos,neg,zero);


}

`

You should move the scanf line inside the loop.

for(i=0;num!="q";i++)
{
  scanf("%d",&num);
  ...
scanf("%d",&num);      /*     <-- for initializing num before using    */
for(i=0;num!='q';i++)  /*     <-- compare with 'q' and not with "q"    */
{
     scanf("%d",&num); /*     <-- take input                           */
     //your code 
}

Comapare with 'q' ( character ) and not with string literal "q" which comparises of 'q' and '\\0' ( take note of single quotes ).

And also you need to take input inside loop , or else value of num does not change in loop .

Try changing for to while and place scanf inside loop.

scanf("%d",&num);
while(num!="q")
{
    if(num>0)
    {
        pos++;
    }
    if(num<0)
    {
        neg++;
    }
    if(num==0)
    {
        zero++;
    }
    scanf("%d",&num);
}

First you should actually loop:

while(1) { // infinite loop
 ...
}

... then scan for integers inside the loop:

while(1) { // infinite loop
 scanf("%d",&num)
 ...
};

... then remember that, oops, the user can enter "q" that is not a number

while(1) { // infinite loop
 char input[100];
 scanf("%s", input);
 if( strcmp( input, "q" ) == 0 ) {
     break;
 }
 else {
    sscanf( input, "%d", &num); 
 }
 ...
}

... then add your tests

   while(1) { // infinite loop
     char input[100];
     scanf("%s", input);
     if( strcmp( input, "q" ) == 0 ) {
         break;
     }
     else {
        sscanf( input, "%d", &num); 
     }

     if(num>0) {
        pos++;
     }
     if(num<0) {
        neg++;
     }
     if(num==0) {
        zero++;
     }
   }

I would do it this way:

int fldCnt = scanf("%d", &num);
while (fldCnt == 1)
{
    // your code
    fldCnt = scanf("%d", &num);
}

When you enter a "q", zero fields get converted to %d .

#include <stdio.h>

int main(void){
    int pos=0, neg=0, zero=0;
    int i, ch, num;

    printf("Input numbers.\n");
    do {
        while(1 == scanf("%d", &num)){//input into inside loop
            if(num > 0)
                pos++;
            else if(num < 0)
                neg++;
            else //if(num==0)
                zero++;
        }
        ch = getchar();//q not allow `scanf("%d", &num)`
    } while(ch != 'q' && ch != EOF);
    printf("You entered \n\tpositive number:::%d times\n\tNegative number:::%d times\n\tZero:::%d times\n",pos,neg,zero);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM