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;
}
}
Related
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.
here is a demo code for explaining what I'm actually looking for
I want to call a 'case' of "switch" - statement from somewhere else in the code in C Language.
Here is the source code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
char a;
int main()
{
printf("Enter a number: ");
scanf("%c", &a);
switch(a){
case 'a':
printf("This is for A & C");
break;
case 'b':
printf("This is for only B");
break;
case 'c':
// Here i want to call "case 'a':"
goto case 'a';
// how can a call another case here?
break;
default:
printf("Default");
break;
}
getch();
return 0;
}
Thanks is Advance :D
You need the so-called "fall-through" case:
So instead of this:
case 'a':
printf("This is for A & C");
break;
case 'b':
printf("This is for only B");
break;
case 'c':
// Here i want to call "case 'a':"
Write this:
case 'a':
case 'c':
printf("This is for A or C");
break;
case 'b':
printf("This is for only B");
break;
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;
I'm currently taking the Video Game Programming course at the Art Institute of Vancouver, in my second quarter and I have a programming class. I'm currently stuck on a bit of homework and I'm unsure why. I'm a beginner when it comes to programming. I have experience with HTML, CSS and Javascript.
The problem I have involves creating a bill for a telephone service customer, but it is entirely dependant on the type of service they are using. The code needs to be executed if the service code they input is either 'r' or 'p', and any other input results in an error. However I'm unsure where to proceed after this.
`
int account, minutes1, minutes2;
char s;
float cost;
printf("Please enter your account number. \n");
scanf_s("%d" , &account);
if(account == -1)
return 0;
printf("Please enter your service code(r for regular or p for premium). \n"); /*checking service*/
scanf_s(" %s" , &s);
switch(s)
{ /*execute specific parameters when r or p are selected as service codes.*/
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o': printf("Error. Please choose a correct service code (r for regular or p for premium). \n");
break;
case 'p': printf("Please enter how many minutes you've used between 6am and 6pm. \n");
scanf_s("%d" , &minutes1);
if(minutes1 > 75)
minutes1 = (minutes1-75)*.10;
printf("Please enter how many minutes you've used between 7pm and 5am. \n");
scanf_s("%d" , &minutes2);
if(minutes2 > 100)
minutes2 = (minutes2-100)*.05;
cost = 25+minutes1+minutes2;
printf("Your account number is %d, your service code is premium, you used %d minutes during the day and %d minutes during the night and your bill comes to $%lf. \n", account, minutes1, minutes2, cost);
break;
case 'q':
case 'r': printf("Please enter how many minutes you've used. \n");
scanf_s("%d", &minutes1);
if(minutes1 > 55)
minutes1 = (minutes1-55)*.20;
cost = 10+minutes1;
printf("Your account number is %d, your service code is regular, you used %d minutes and your bill comes to %lf. \n");
break;
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z': printf("Error. Please choose a correct service code (r for regular or p for premium). \n");
break;
}`
If you are scanning for character you should use scanf("%c",&s);
Also just use:
switch(s){
case 'p':
...
break;
case 'r':
...
break;
default:
error
}
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