2010-11-02 95 views
1

在我的代码中,我试图忽略新行。有没有更好的方法来做到这一点?如何在没有 n的情况下获得角色?

 do 
     { 
      scanf("%c",&wouldCount); 
     } while(wouldCount == '\n'); 

原代码

#include <stdio.h> 

int main() 
{ 
    char Yes = 'Y'; 
    char wouldCount; 
    int counter; 
    int start; 
    int end; 
    int increment; 
    const END_DEFAULT = 1; 
    const INCREMENT_DEFAULT = 1; 
    printf("Would you like to count?"); 
    scanf("%c",&wouldCount); 
    while(wouldCount == 'Y' || wouldCount == 'y') 
    { 
     printf("Please enter start number."); 
     scanf("%d",&start); 
     printf("Please enter a number you would like to stop at?"); 
     scanf("%d",&end); 
     printf("Please enter a number to increment by."); 
     scanf("%d",&increment); 
     if(end < start) 
     { 
      end = start + END_DEFAULT; 
     } 
     if(increment <= 0) 
     { 
      increment = INCREMENT_DEFAULT; 
     } 
     for(counter = start; counter < end; counter += increment) 
     { 
      printf("%d\n",counter); 
     } 
     printf("Would you like to count?"); 
     do 
     { 
      scanf("%c",&wouldCount); 
     } while(wouldCount == '\n'); 
    } 
return 0;  
} 
+1

我不明白你的意思。是否需要忽略换行符? – Jim 2010-11-02 02:49:29

+0

当我在运行程序时按Enter键时,我不希望它退出。 – user494243 2010-11-02 02:55:30

回答

3

你可以改变的scanf( “%C”,& wouldCount);到scanf(“\ n%c”,& wouldCount);以及放弃do/while循环。这将告诉scanf忽略没有输入字符的输入。

请参阅scanf c++ reference

相关问题