Dear Kmess...!
Your indication to my wrong use of '0' and '1' as switch-cases has been of some help to me. You see I've changed the case values with 'a' and 'b' instead of '1' and '0'. Its effect is that my programme doesn't directly jump to the default statement now but all the same, even if the user answers correctly, it ignores the case'a' and always goes to case 'b' and executes the code within that:
Code:
case'b':
printf("Wrong Answer\nTry again\n");
scanf("%s",&guess[15]);
if(guess[15]==JAPAN)
printf("Correct\nYour score is %d.",score+2);
else
{printf("Wrong again\nTry once more");
scanf("%s",&guess[15]);
if(guess[15]==JAPAN)
printf("right this time\nscore=%d",score+1);}
break;
default:
printf("SORRY You LOST!\nyour score is %d.",score);
Kmess please dont misunderstand me. Though I'm a beginner but not an ill-organised person. This programme has been pre-thought. I've an Algorithm for it. I have a flowchart too which I could send to you if you like. Feel free to ask. In my programme, I've used 'The Conditional operator' for the decision-making step, which is the only ternary operator in Turbo C
Code:
guess[15] = (guess[15]==JAPAN) ? 'a' : 'b'; /*conditional operator*/
Its purpose is to test the condition in the parentheses (in this case "guess[15]==JAPAN"). If found true, it allots the value preceding the colon ('a' in this case) to the variable operand (guess[15]); and vice versa.
In my prog. the conditional operator always allots the value 'b' to the variable. This is what has now become the problem.I added a 'printf' statement right after the conditional operator to check out its value. like this
Code:
scanf("%s",&guess[15]);
guess[15] = (guess[15]==JAPAN) ? 'a' : 'b'; /*conditional operator*/
printf("%c",guess[15]);
and it always prints value 'b' even when the user answers correctly, which confirms the error.
PHP Code:
Another problem is that you have commented out the definition of the integer variable "score". Therefore, the compiler sees subsequent references to "score" as an undeclared identifier: it can't proceed.
No use even after removing the comments. Infact the compiler did initialize 'score' before as good as it does now after removing comments.
PHP Code:
I'm curious why you chose to create an autonomous block of code, starting from before the scanf() statement and extending to the getch() statement near the end of the program (i.e. an extra set of curly braces).
Yes, let me explain. Actually I would be adding more questions to this prog. So I wanted to separate different Q's coding into different blocks. I now see where the first brace should have been. Like
Code:
void main()
{
char guess[15],JAPAN='q';
int score=0;
clrscr();
printf("\"WELCOME\"\nPlease turn on your Caps Lock.\n");
{
printf("Which country is called \"The Land of The Risisng Sun\" ?\n");
scanf("%s",&guess[15]);
guess[15] = (guess[15]==JAPAN) ? 'a' : 'b'; /*conditional operator*/
switch ( guess[15] )
{
case'a':
printf("That's corrrrrrect\n");
printf("Your score is %d.",score+4);
break;
case'b':
printf("Wrong Answer\nTry again\n");
scanf("%s",&guess[15]);
if(guess[15]==JAPAN)
printf("Correct\nYour score is %d.",score+2);
else
{printf("Wrong again\nTry once more");
scanf("%s",&guess[15]);
if(guess[15]==JAPAN)
printf("right this time\nscore=%d",score+1);}
break;
default:
printf("SORRY You LOST!\nyour score is %d.",score);
}
}
getch();
}
And as to your concern regarding the function definition using "void main()", it is the default criterion of function definition in Turbo C.
I also request you to help me writing the code lines for strcmp() and tell me the prototype used for it.
Thanks.
Slytherin
Help shall always be given to those at Hogwarts who ask for it...!
Albus Dumbledore