You enclose the part you want to repeat inside a loop and set the loop to check if the user has inputted 'Y'.
You enclose the part you want to repeat inside a loop and set the loop to check if the user has inputted 'Y'.
Devoted my life to programming...
What kind of a loop should I use?
A do while loop is generally what is used for this sort of thing. It has the pseudo-code syntax of:
The do while construct is a bit more useful than a while one in this context, because you don't have to set "input" to be invalid before entering the loop. What happens is that the code "input = Ask_user_for_input()" runs and sets the input variable before the condition of the loop is checked. Otherwise you would need to use something like this:Code:do { input = Ask_user_for_input(); } while (input == invalid_value);
You can also set the loop to print information prompting the user that input is expected (the loop will take care of printing it for every invalid value the user enters). Hope this helps, good luck!Code:input = invalid_value; // Set it to be invalid so the loop will begin while (input == invalid_value) { input = Ask_user_for_input(); }
WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;
Thanks, that helped clear some things up for me.
I ended up using an if else statement:
When they say no, the program ends, but when they say yes, the whole program starts again. Can you suggest how I can make it so that instead of starting the whole program again, it will just go back to a certain part?Code:printf("\n\nCalculate another student grade (Enter Y for Yes, N for No): "); scanf(" %c", &x); if( x == 'N' || x == 'n'){ return 0; } else if(x == 'Y' || x == 'y'){ return main(); }
Try this:
Code:int c; int i = 0; // You may need this. SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), 0); do{ printf("Now processing number %u\n", ++i); ////////////////// // Your code here. ///////////////////// puts("Another? y = yes, n = no"); c = getchar(); } while(toupper(c) != 'N');
Here is a novel idea, why don't you read about loops in C and then tell us?