0

program is skipping 2nd choice of a,b,c or d. It stops after "Write answer (a/b/c/d):" in that moment, program stops and doesn't let the user choose from the options mentioned before. How can i fix that with some beginner fix? (I'm programming only like 2 weeks) Thanks.

Here's the source code:

#include <stdio.h>

#include <stdlib.h>

 int main()

 {
   char choice;
   char choice1;

printf("Welcome.\n");
printf("-------------------\n");

printf("Who created Microsoft?\n");
printf("a) Steve Jobs\n");
printf("b) Tim Cook\n");
printf("c) Bill Gates\n");
printf("d) Mark Zuckerberg\n");

printf("\nWrite answer (a/b/c/d): ");
scanf("%c",&choice);

switch(choice)
{
    case 'a': printf("\nIncorrect.\n");
        break;

    case 'b': printf("Incorrect.\n");
        break;

    case 'c': printf("Correct.\n");
        break;

    case 'd': printf("Incorrect.\n");
        break;

}

printf("Who owns MySQL?\n");
printf("a) Microsoft\n");
printf("b) Apple\n");
printf("c) Google\n");
printf("d) Oracle\n");

printf("\nWrite answer: (a/b/c/d): ");
scanf("%c",&choice1);

switch(choice1)
{
    case 'a': printf("Incorrect.\n");
        break;

    case 'b': printf("Incorrect.\n");
        break;

    case 'c': printf("Incorrect.\n");
        break;

    case 'd': printf("Correct.\n");
        break;
}

    return(EXIT_SUCCESS);

}

Can someone who can make it also explain why did he choose such solution? Thank you in advance.

Kira
  • 1
  • 3
  • try this `scanf(" %c",&choice1);` – marko Oct 20 '17 at 16:47
  • you're not taking into account the fact that after each option you press the enter key – Chris Turner Oct 20 '17 at 16:47
  • There are many duplicates of this question on Stack Overflow, but they are hard to find. The solution is to replace `"%c"` in `scanf` with `" %c"` Look closely, there is a difference! – Sergey Kalinichenko Oct 20 '17 at 16:48
  • Thanks for quick replies, I fixed it as you said guys. @dasblinkenlight Sorry for the duplicate, I searched for same question but couldn't find the solution. Should I remove this question? – Kira Oct 20 '17 at 16:56
  • @TomKarzes I think OP has not reached loops yet in her studying of programming. Her problem was skipping the second `scanf`. – Sergey Kalinichenko Oct 20 '17 at 17:00
  • @dasblinkenlight Should i remove the question as you said it's duplicated? – Kira Oct 20 '17 at 17:02
  • @kira its okay ^^ feel free to ask us anything, but first always try to google, try to debug your program and stuff like that – marko Oct 20 '17 at 17:04
  • @Mare70 Sure will do that, thanks for the tips and sorry once again for duplicate question. – Kira Oct 20 '17 at 17:05
  • @Kira no problem, you didn't know, and you actually described your problem well, so we could answer you quickly. Just continue learning ^^ – marko Oct 20 '17 at 17:06
  • @Kira The main reason to keep duplicates around is to increase visibility of answers to other questions by providing an alternative search path to them. Since your question is not very searchable, I would recommend deleting it. Obviously, there's nothing wrong with your question, so the ultimate decision is yours. – Sergey Kalinichenko Oct 20 '17 at 17:07
  • @TomKarzes Oh, come on! Relax, its all cool :)) Mistakes like that happen to everyone, and that's completely normal. I didn't answer to you earlier because i didn't want to clutter this comment section. Cheers! – marko Oct 20 '17 at 19:35

0 Answers0