Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

c - How to make reiterations using yes/no prompt?

I realize a there is an issue with my data overflowing, but my main concern is trying to re run the program at the end to start all over. I've looked through multiple examples through this website, but couldn't really find one that fit my need.

I am not sure if you can see the first part of my code, but I essentially tried to use someones do while example for my program but i just can't figure it out.

If anyone could give any suggestions I would greatly appreciate it!

I'm sure if i keep at it i'll figure it out eventually, but i thought this would be a good question for this website.

Here is my source code:

#include <stdio.h>

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

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

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

   printf("3
" ) /* count down sequence*/;
   sleep(1);
   printf("2
" );
   sleep(1);
   printf("1
" );
   sleep(1);
   printf("SHROOOOM!
" );
   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! 
",distance);
   printf("Would you like another go?(yes, no)
" );
   scanf("%s
", buffer );
 }while (strcmp(buffer, "yes") !=0);

 getchar();

 return 0;

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think this should be enough to give you an idea:

#include<stdio.h>

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


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

            if(scanf(" %c", &menu_choice ) == 1){
                if((menu_choice=='y') || (menu_choice=='Y')){
                    printf("You choosed Yes


");
                    validate = 1;
                }else if((menu_choice=='n') || (menu_choice=='N')){
                    printf("You choosed No


");
                    validate = 2;
                }else{
                    printf("Wrong Input.


");
                    validate = 0;
                }
            }
        }while( validate == 0 || validate == 1);

        printf("Goodbye
");

    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

For things like this i prefer instead something like this:

#include<stdio.h>

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

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

        if(scanf("%d%c",&option,&c) == 0 || c != '
'){
            while((check = getchar()) != 0 && check != '
');
            printf("	I sayed a Number please

");
        }else if(option < min || option > max){
            printf("	The number has to be beetwen %d and %d

",min,max);
        }else{
            break;
        }
    }while(1);

    return option;
}

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

    printf("
Your number is	%d
",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

But if you insist to use yes/no instead of Y/N, then;

#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):	" );

            if(scanf(" %s", menu_choice ) == 1){
                if((strcasecmp(menu_choice, yes) == 0)){
                    printf("You choosed Yes


");
                    validate = 1;
                }else if((strcasecmp(menu_choice, no) == 0)){
                    printf("You choosed No


");
                    validate = 2;
                }else{
                    printf("Wrong Input.


");
                    validate = 0;
                }
            }
        }while( validate == 0 || validate == 1);

        printf("Goodbye
");

    return 0;
}

Output:

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

Please make notice that strcasecmp is found in strings.h and not in string.h.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...