簡體   English   中英

無法實現永久循環

[英]Having trouble implementing forever loop

我似乎遇到了永遠循環的麻煩,第一次運行該程序似乎正在工作,但是由於某種原因,它似乎跳過了詢問用戶是否要輸入CD的選項...任何幫助將不勝感激,謝謝!

/* 
 * CourseProj.c
 * Create a program (database) that a record shop might use to track its 
    inventory of CDs
 * We need the following fields:
 * - Tittle, Artist, Number of tracks, Album/single, Price
 * This project must be commented
 */

 #include <stdio.h>

 main(){

 char    title [100][61];
 char    artist [100][61];
 int     num_tracks[100];      /* number of tracks on the CD */  
 float   price[100];         
 int     album[100];           /* boolean - is the CD an ALBUM? */
 char    type;                 /* used to read in album/single info */
 int     count = 0;            /* how many Cds are being tracked */
 int     i;                    /* loop counter */


 printf( "Welcome to the CD database.\n");
 printf( "You can store a maximum of 100 CDs.\n");

 /*
  * Loop until they no longer wish to enter any more CDs
  *
  */
 for (;;){      /* forever loops are convenient for this sort of thing */
   /*
    * Ask them if they want to enter another CD
    * Any answer other than y or Y wil be treated as a No
    */
   printf("\nHave you any more CDs to enter (y/n)? ");
   fflush(stdin);
   scanf("%c", &type);
   if (type != 'y' && type !='Y')
     break;


   printf("\n");   /* for neat output */

   // First, the title
   printf("Please enter the details of the CD %d... \n\n", count+1);
   printf("Title? ");
   fflush(stdin);
   scanf("%s", title[count]);


   // input the artist name
   printf("Artist? ");
   fflush(stdin);
   scanf("%s", artist[count]);

   // Now we need to input the number of tracks in the Album
   printf("Number of tracks? ");
   fflush(stdin);
   scanf("%d", &num_tracks[count]);

   // need to check if it's an Album or a single
   for (;;){
     printf("Album or single (a for album, s for single)? ");
     fflush(stdin);
     scanf(" %c", &type);
     if  (type == 'a' || type == 's')
       break;
     printf("Error - only 'a' or 's' are allowed\n");
    }
    album[count] = type == 'a'; // if we get here it must be 'a' or 's'               


    //need to prompt the user for the price of the cd
    printf("Retail price (e.g. 4.65)?");
    fflush(stdin);
    scanf("%f", &price[count]);
    count = count + 1;

    /*
     * Check if we have filled up the array
     */
    if (count == 100){
      printf("You have reached the limits of this program\n\n");
      break;
    }
   }
   /*
    * Output the CD details
    */
   for ( i = 0; i < count; i++){

     printf("\nThe details of CD %d are:\n", i+1);
     printf("==============================\n"); 
     printf("Title: %s\n", title[i]);
     printf("Artist: %s\n", artist[i]);
     printf("Number of track: %d\n", num_tracks[i]);
     //let check what the user input with the boolean


     if (album[i])
       printf("Album\n");

     else
       printf("Single\n");

     printf("Price: %.2f\n", price[i]); 
     printf("===========================\n");


     if ( i < count - 1){ // only do this if there are more CDs to see

     /*
      * A user-friendly way to progress to the next CD
      */
      printf("\nPress ENTER to see the next set of details: ");


      fflush(stdin);
      getchar();
     }
    }

    /*
     * A user-friendly way to exit the program
     */
     printf("\nPress ENTER to exit the program: ");
     fflush(stdin);
     getchar();
    }

嘗試在程序中使用這段代碼,以從用戶輸入中獲取字符。 在Visual Studio 2012中運行它時,它對我有用。

printf("\n Have you any more CDs to enter (y/n)? ") ;
fflush(stdin);

type = getchar() ;

if( type == '\n')
{
   break ;
}

printf("you have typed : %c \n",type) ; /*print the character just to make sure that 
                                         it's the right one the user entered. */                                        

if (type != 'y' && type !='Y')
{
  break;
}

這應該是正確的版本。 每當您要停止等待用戶按下按鍵時,都應該“吃掉”換行符! 這意味着,例如,如果您使用scanf從kwyboard獲取一些數據,則用戶將在最后按Enter確認獲取,該enter是存儲在輸入緩沖區中的換行符,因此我在此處和此處放置了一些getchar()擺脫它們。 fflush()不起作用,因為它只能強制清除stdout和stderr。 解決方法如下:

#include <stdio.h>

main(){
    char    title [100][61];
    char    artist [100][61];
    int     num_tracks[100];      /* number of tracks on the CD */  
    float   price[100];         
    int     album[100];           /* boolean - is the CD an ALBUM? */
    char    type;                 /* used to read in album/single info */
    int     count = 0;            /* how many Cds are being tracked */
    int     i;                    /* loop counter */


    printf( "Welcome to the CD database.\n");
    printf( "You can store a maximum of 100 CDs.\n");

    /*
    * Loop until they no longer wish to enter any more CDs
    *
    */
    for (;;){      /* forever loops are convenient for this sort of thing */
    /*
     * Ask them if they want to enter another CD
     * Any answer other than y or Y wil be treated as a No
     */
    printf("\nHave you any more CDs to enter (y/n)? ");
    scanf("%c", &type);
    getchar();
    if (type != 'y' && type !='Y')        
        break;

    printf("\n");   /* for neat output */

    // First, the title
    printf("Please enter the details of the CD %d... \n\n", count+1);
    printf("Title? ");

    scanf("%s", title[count]);


    // input the artist name
    printf("Artist? ");

    scanf("%s", artist[count]);

    // Now we need to input the number of tracks in the Album
    printf("Number of tracks? ");
    scanf("%d", &num_tracks[count]);

    // need to check if it's an Album or a single
    for (;;){
        printf("Album or single (a for album, s for single)? ");
        scanf(" %c", &type);
        if  (type == 'a' || type == 's')
            break;
        printf("Error - only 'a' or 's' are allowed\n");
    }
    album[count] = type == 'a'; // if we get here it must be 'a' or 's'               


    //need to prompt the user for the price of the cd
    printf("Retail price (e.g. 4.65)?");
    scanf("%f", &price[count]);
    getchar();
    count = count + 1;

    /*
     * Check if we have filled up the array
     */
    if (count == 100){
        printf("You have reached the limits of this program\n\n");
        break;
    }
    /*
     * Output the CD details
     */
    for ( i = 0; i < count; i++){

        printf("\nThe details of CD %d are:\n", i+1);
        printf("==============================\n"); 
        printf("Title: %s\n", title[i]);
        printf("Artist: %s\n", artist[i]);
        printf("Number of track: %d\n", num_tracks[i]);
        //let check what the user input with the boolean


        if (album[i])
            printf("Album\n");
        else
            printf("Single\n");

        printf("Price: %.2f\n", price[i]); 
        printf("===========================\n");

        if ( i < count - 1){ // only do this if there are more CDs to see
            /*
             * A user-friendly way to progress to the next CD
             */
            printf("\nPress ENTER to see the next set of details: ");
            while(getchar() != '\n');
        }
    }
}
/*
 * A user-friendly way to exit the program
 */
 printf("\nPress ENTER to exit the program: ");
 while(getchar() != '\n');

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM