How to prompt the user to loop in C? - c

I’m new to C and currently doing a Jack ‘N Poy that requires the project to restart when the user prompt it like by answering y. However, I can’t seem to grasp its pattern. Have tried it on C++ and it worked but not on C. Can anyone help? I want the user to have the ability to play again after finishing the game.
Here is the code:
#include<stdio.h>
int main (){
char firstPlayer, secondPlayer, again;
do{
printf("Jack 'n Poy)\nEnter Player 1 input: ");
scanf("%c ", &firstPlayer);
printf("Enter Player 2 input: ");
scanf("%c", &secondPlayer);
switch (firstPlayer){
case 'x':
switch (secondPlayer){
case 'x':
printf("Draw");
break;
case 's':
printf("Player 2 wins");
break;
case 'p':
printf("Player 1 wins");
}
break;
case 's':
switch (secondPlayer){
case 'x':
printf("Player 1 wins");
break;
case 's':
printf("Draw");
break;
case 'p':
printf("Player 2 wins");
}
break;
case 'p':
switch (secondPlayer){
case 'x':
printf("Player 2 wins");
break;
case 's':
printf("Player 1 wins");
break;
case 'p':
printf("Draw");
}
break;
default:
printf("Invalid input");
}
printf("\nPlay again? (y/n)");
scanf("%c", &again);
}
while (again == 'y');
printf("Thank you for playing");
return 0;
}
Have also tried adding a substitute variable for the y and yes it does loop, but the loop skips the first question.
Your help are very much appreciated. Thank you very much!

You need to change the format to:
scanf(" %c", ...);
in all scanfs. Note the ' ' before %c

Your again is a string. You can use strcmp(expectedString,inputString) as the condition at while. You will need to include the string.h header file to be able to do this.
Alternatively, change again to char type, i.e. char again;
And then use %c in the last scanf, i.e. scanf("%c", &again);. You may face skipped character scenario if spaces or newlines are entered. In that case, refer to this answer for guidance.

Related

C - Switch-case prints case twice

I have written the following switch-case:
char input;
int run = 1;
while(run){
printf("Would you like to update the student's name? Enter Y or N (Y=yes, N=no)\n");
input = getchar();
switch (input)
{
case 'N':
run = 0;
break;
case 'n':
run = 0;
break;
case 'Y':
printf("Please enter the updated name\n");
scanf("%s", st->name);
run = 0;
break;
case 'y':
printf("Please enter the updated name\n");
scanf("%s", st->name);
run = 0;
break;
case '\n':
break;
default:
printf("Wrong input. Please enter a valid input (Y or N)\n");
}
}
When I run it does this:
Please enter the id of the student that you would like to update
1
Would you like to update the student's name? Enter Y or N (Y=yes, N=no)
Would you like to update the student's name? Enter Y or N (Y=yes, N=no)
Why does it print the question twice? Can anyone help?
Other than that the cases run as expected.
The function getchar reads all characters including new line characters. Instead use
scanf( " %c", &input );
Also your switch statement has a duplicated code. Write for example
switch (input)
{
case 'N':
case 'n':
run = 0;
break;
case 'Y':
case 'y':
printf("Please enter the updated name\n");
scanf("%s", st->name);
run = 0;
break;
//...
The same approach you can use for other labels of the switch statement. and remove this code
case '\n':
break;

Im having a problem with a while loop and switch

while(Game == 0)
{
gotoxy(100,40);
setForeColor(MY_COLOR_WHITE);
printf("A- Jogar uma partida de Anabi\n");
gotoxy(100,42);
printf("B- Carregar partida anterior\n");
gotoxy(100,44);
printf("C- Descri%c%co do jogo\n", 135, 198);
gotoxy(100,46);
printf("D- Sair do jogo\n");
scanf("%c", &choice);
switch(choice)
{
//This part of the code should break my loop
case 'A':
case 'a':
gotoxy(100, 52);
printf("Please enter your name bellow\n");
gotoxy(98, 53);
printf("->");
scanf("%s",NamePlayer);
Game = 1;
break;
case 'B':
case 'b':
// in here I am going to call a function, the function that reads the file previously saved
//also here I am going to know if there is any saved files, and give an output to the user
break;
case 'C':
case 'c':
//This gets me to the description of the game, rules, etc.. and its working fine with the loop
DescricaoDoJogo();
break;
case 'D':
case 'd':
exit(0);
break;
default:
//This default is giving me trouble, should I use and If, else? Because when i choose the option C, and I get back to the menu what is inside the default executes
printf("Wrong choice.. Try again.. A, B, C or D..");
scanf(" %c", &choice);
system("cls");
break;
}
}

Switch statement inside a do-while loop in C [duplicate]

This question already has answers here:
abnormal behavior of scanf [duplicate]
(3 answers)
Closed 3 years ago.
I´m trying to make a program that tells you if a letter is a vowel o consonant and then ask the user if he/she wants to use the program again.
Using just the switch statement works totally fine. My problem comes when inserting the do-while loop. On the first try, the program works smoothly, but in the second and following loops, after asking the user if he wants to try again by typing 1, the program "jumps" the part where the user writes its input (scanf("%c", &letter)) and executes the rest of the program considering the previously typed 1 as the input and messing up everything.
I´ve tried looking for the answer on similar questions and videos, but I´m quite new and I just can´t grasp it. I have another program with a similar problem. I will really appreciate your help :)
int main()
{
char letter;
int new_try;
do {
printf("Is you letter a vocal or a consonant?\n");
printf ("\nPlease submit a letter:\n");
scanf("%c", &letter);
switch(letter) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("This letter is a vowel\n");
break;
default : printf("This letter is a consonant\n");
break;
}
printf("Do you wish to try again?\n [1.Yes 2. No]\n");
scanf("%d", &new_try);
}
while(new_try != 2);
return 0;
}
You need to clean the input buffer. Otherwise, as long as there are characters in your input buffer, the program will not wait for new input from user. Here is the solution.
#include <stdio.h>
int main() {
char letter;
int new_try;
do{
printf("Is you letter a vocal or a consonant?\n");
printf ("\nPlease submit a letter:\n");
scanf("%c", &letter);
while ((getchar()) != '\n');
switch(letter) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': printf("This letter is a vowel\n");
break;
default : printf("This letter is a consonant\n");
break;
}
printf("Do you wish to try again?\n [1.Yes 2. No]\n");
scanf("%d", &new_try);
}while(new_try != 2);
return 0;
}
Update
As #David C. Rankin mentioned, using scanf(" %c", &letter); can also addresss your issue. However, it doesn't address a matching failure. In the above case, you also need to validate the input.

How can I make switch-case statements case insensitive?

How can I make a switch-case statement to not be case sensitive?
Say I made something like this:
#include <stdio.h>
char choice;
int main ()
{
char choice;
printf("Will you choose A,B, or C?\n>");
scanf(" %c", &choice);
switch(choice)
{
case 'A':
printf("The First Letter of the Alphabet");
break;
case 'B':
printf("The Second Letter of the Alphabet");
break;
case 'C':
printf("The Third Letter of the Alphabet");
break;
}
}
It would only respond to capital letters. How do I make it respond to lower case letters?
toupper in <ctype.h> converts a character to uppercase:
#include <stdio.h>
#include <ctype.h>
char choice;
int main ()
{
printf("Will you choose A,B, or C?\n>");
scanf(" %c", &choice);
switch(toupper(choice)) // Changed line
{
case 'A':
printf("The First Letter of the Alphabet");
break;
case 'B':
printf("The Second Letter of the Alphabet");
break;
case 'C':
printf("The Third Letter of the Alphabet");
break;
}
You simply need this :-
switch(choice)
{
case 'A':
case 'a':
printf("The First Letter of the Alphabet");
break;
case 'B':
case 'b':
printf("The Second Letter of the Alphabet");
break;
case 'C':
case 'c':
printf("The Third Letter of the Alphabet");
break;
}
and so on to continue your series.
Actually,what it does is that it bypasses(skims) upto bottom until it finds the first break statement matching the case thereby executing all the cases encountered in between!!!
Before the switch(), add:
choice = toupper(choice);
And if you haven't already got it, #include <ctype.h> to get the prototype.
You can give 2 cases one by one,
switch(choice)
{
case 'A':
case 'a':
printf("The First Letter of the Alphabet");
break;
case 'B':
case 'b':
printf("The Second Letter of the Alphabet");
break;
case 'C':
case 'c':
printf("The Third Letter of the Alphabet");
break;
}

using switch case, error on the default part

although default part mustn't run,
it runs al case, I thanks for all helps at now, what should i do? This code all runs i writes on screen default case.
do
{
scanf("%c",&choice);
switch(choice)
{
case 'T':
printf("Enter edges of Triangle1\n");
scanf("%d%d",&edge1,&edge2);
Triangle1(edge1,edge2);
break;
case 't':
printf("Enter edges of Triangle1\n");
scanf("%d%d",&edge1,&edge2);
Triangle2(edge1,edge2);
break;
case 'R':
printf("Enter edges of square\n");
scanf("%d%d",&edge1,&edge2);
Rectangle(edge1,edge2);
break;
case 'S':
printf("Enter one edge of square\n");
scanf("%d",&edge);
Square(edge);
break;
case 'C':
printf("Enter radius of circile\n");
scanf("%d",&radius);
Circle(radius);
break;
default:
printf("Wrong input\n");
break;
}//end of switch
printf("\n");
}while(choice!='e');
The newline gets passed as well as input. Try calling "getc(stdin)" after the first scanf() and it should work :)
Check out for example: http://home.datacomm.ch/t_wolf/tw/c/getting_input.html#newline

Resources