【问题标题】:How to make reiterations using yes/no prompt?如何使用是/否提示进行重申?
【发布时间】:2015-09-03 19:27:14
【问题描述】:

我意识到我的数据溢出存在问题,但我主要关心的是尝试在最后重新运行程序以重新开始。我通过这个网站浏览了多个示例,但找不到真正适合我需要的示例。

我不确定你是否可以看到我的代码的第一部分,但我基本上尝试使用别人做的例子来做我的程序,但我就是想不通。

如果有人可以提出任何建议,我将不胜感激!

我敢肯定,如果我坚持下去,我最终会弄明白的,但我认为这对这个网站来说是个好问题。

这是我的源代码:

#include <stdio.h>

int main (void) {
 int days;/* user will input number of days light will travel*/
 int answer;
 char buffer[256];

 printf(" \n" );
 printf("\t**-**-**-**Welcome to the LIGHT RAY!**-**-**-**\n");
 printf(" \n" );
 printf("\tTo get an idea of how unbelieveably fast light is!\n");
 printf("\t come climb aboard the LIGHT RAY!\n", );
 do
 {
   printf(" \n" );
   printf(" \n");
   printf("\tHow many days would you like to travel?\n");
   scanf("%d", &days);

   printf("processing...\n" ) /* fictional terminal computing information*/;
   sleep(2);
   printf("Initializing warp drive...\n" );
   sleep(1);

   printf("3\n" ) /* count down sequence*/;
   sleep(1);
   printf("2\n" );
   sleep(1);
   printf("1\n" );
   sleep(1);
   printf("SHROOOOM!\n" );
   sleep(1);

   int day_time=days * 86400/*86,400 seconds is equal to 1 day*/;
   int distance=day_time*186000/*light travels 186,000 miles per second!*/;



   printf("Congratulations, you have traveled %lld miles! \n",distance);
   printf("Would you like another go?(yes, no)\n" );
   scanf("%s\n", buffer );
 }while (strcmp(buffer, "yes") !=0);

 getchar();

 return 0;

}

【问题讨论】:

  • 您应该在编译时出现完整警告(gcc 上的-Wall -Wextra -Wpedantic),并修复所有警告。另外:要在 gcc 上获得良好的警告,您还需要将其设置为优化 (-O3)。
  • 不要使用 int 使用 double,因为 86400 * 186000 已经超出 32 位有符号整数的范围 1 天,更不用说一年了。
  • while (strcmp(buffer, "yes") !=0) 应该测试 ==0 以匹配字符串。
  • @WeatherVane 为什么使用double 而不是long long 或(甚至更好)uint64_t?如有必要,无需引入凌乱的浮点数:)
  • 不要在此处使用scanf ("%s\n", buffer) - 使用fgets (buffer, 255, stdin) 以确保安全(并避免缓冲区溢出)。请注意,fgets 会自动将 NULL 字符附加到输入中,因此我们使用 255 而不是 256 为其留出空间。

标签: c loops iteration


【解决方案1】:

我认为这应该足以给你一个想法:

#include<stdio.h>

int main(void){
    int     validate;
    char    menu_choice;


        validate = 0;
        do{
            printf("Would you like another go?(y/n):\t" );

            if(scanf(" %c", &menu_choice ) == 1){
                if((menu_choice=='y') || (menu_choice=='Y')){
                    printf("You choosed Yes\n\n\n");
                    validate = 1;
                }else if((menu_choice=='n') || (menu_choice=='N')){
                    printf("You choosed No\n\n\n");
                    validate = 2;
                }else{
                    printf("Wrong Input.\n\n\n");
                    validate = 0;
                }
            }
        }while( validate == 0 || validate == 1);

        printf("Goodbye\n");

    return 0;
}
Would you like another go?(y/n):  1
Wrong Input.
Would you like another go?(y/n):    k
Wrong Input.


Would you like another go?(y/n):    y
You choosed Yes


Would you like another go?(y/n):    Y
You choosed Yes


Would you like another go?(y/n):    N
You choosed No


Goodbye

对于这样的事情,我更喜欢这样的事情:

#include<stdio.h>

int checkInput(int min, int max){
    int option,check;
    char c;

    do{
        printf("Please type a number beetwen %d and %d:\t",min,max);

        if(scanf("%d%c",&option,&c) == 0 || c != '\n'){
            while((check = getchar()) != 0 && check != '\n');
            printf("\tI sayed a Number please\n\n");
        }else if(option < min || option > max){
            printf("\tThe number has to be beetwen %d and %d\n\n",min,max);
        }else{
            break;
        }
    }while(1);

    return option;
}

int main(void){
    int number = checkInput(0,1);

    printf("\nYour number is\t%d\n",number);

    return 0;
}
Please type a number beetwen 0 and 1: 2e
    I sayed a Number please
Please type a number beetwen 0 and 1:   g45
    I sayed a Number please

Please type a number beetwen 0 and 1:   75
    The number has to be beetwen 0 and 1

Please type a number beetwen 0 and 1:   1

Your number is  1

但是如果你坚持使用yes/no而不是Y/N,那么;

#include<stdio.h>
#include<strings.h>

int main(void){
    int     validate;
    char    menu_choice[5];
    char *yes = "yes";
    char *no = "no";

        validate = 0;
        do{
            printf("Would you like another go?(yes/no):\t" );

            if(scanf(" %s", menu_choice ) == 1){
                if((strcasecmp(menu_choice, yes) == 0)){
                    printf("You choosed Yes\n\n\n");
                    validate = 1;
                }else if((strcasecmp(menu_choice, no) == 0)){
                    printf("You choosed No\n\n\n");
                    validate = 2;
                }else{
                    printf("Wrong Input.\n\n\n");
                    validate = 0;
                }
            }
        }while( validate == 0 || validate == 1);

        printf("Goodbye\n");

    return 0;
}

输出:

Would you like another go?(yes/no):   sadasdas
Wrong Input.
Would you like another go?(yes/no): 213212
Wrong Input.


Would you like another go?(yes/no): Yes
You choosed Yes


Would you like another go?(yes/no): YeS
You choosed Yes


Would you like another go?(yes/no): YES
You choosed Yes


Would you like another go?(yes/no): No
You choosed No


Goodbye

请注意 strcasecmp 位于 strings.h 而不是 string.h

【讨论】:

    【解决方案2】:

    感谢大家的意见,您的反馈非常宝贵!如果我需要的话,我可能会在某一天再次参考它

    如果你们有兴趣,这里是我的最终产品,尽管我可能会继续摆弄它:

     #include <stdio.h>
    
    int main (void) {
      int days;/* user will input number of days light will travel*/
      int validate=0;
      char menu_choice;/* choices... choices*/
    
      printf(" \n" );
      printf("\t**-**-**-**Welcome to the LIGHT RAY!**-**-**-**\n");/*carny introduction */
      printf(" \n" );
      printf("\tCome one come all \n" );
      printf("\tget an idea of how unbelieveably fast light is!\n");
      do{/*loop for return trip*/
          printf("\tTake a trip on the LIGHT RAY?(y/n):\t" );
          if(scanf(" %c", &menu_choice ) == 1){
              if((menu_choice=='y') || (menu_choice=='Y')){
    
                  printf("\tAhh... Good choice!\n\n");/*responce and input*/
                  printf(" \n" );
                  printf(" \n");
                  printf("\tHow many days would you like to travel?\t");
                  scanf("%d", &days);
    
                  printf("processing...\n" ) /* fictional terminal computing information*/;
                  sleep(2);
                  printf("Initializing warp drive...\n" );
                  sleep(1);
    
                  printf("3\n" ) /* count down sequence*/;
                  sleep(1);
                  printf("2\n" );
                  sleep(1);
                  printf("1\n" );
                  sleep(1);
                  printf("SHROOOOM!\n\n" );
                  sleep(1);
    
                  long long day_time=days * 86400/*86,400 seconds is equal to 1 day*/;
                  long long distance=day_time*186000/*light travels 186,000 miles per second!*/;
    
    
                  printf("Congratulations, you managed not to get trapped in the space time continuum and manage to travel %lld miles! \n\n",distance);
    
    
    
                  validate = 1;
              }else if((menu_choice=='n') || (menu_choice=='N')){
                  printf("\n" );
                  printf("\tStep aside you're holding up the line!!!\n\n\n");
                  validate = 2;
              }else{
                  printf("\n" );
                  printf("\tWHAT did you just call my mother!.\n\n\n");
                  validate = 0;
              }
          }
      }while( validate == 0 || validate == 1);
    
    
    
    
    
      getchar();
    
      return 0;
    }
    

    最后一个想法,是否可以在第一次迭代后更改操作语句 例如:

    你想通过光线开始旅行吗?

    。 . .

    恭喜……

    您想再去一次旅行吗? /重复循环/

    当像“yes”这样的角色被赋予days_travel时,我也遇到了一些奇怪的互动。

    我将如何解决这个问题,我正在考虑一个 if then 语句,但我不太确定如何构建它?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-28
      • 2012-06-30
      • 1970-01-01
      • 2020-11-28
      • 2017-11-21
      • 1970-01-01
      • 2021-09-03
      • 2013-03-01
      相关资源
      最近更新 更多