0

I am a beginner in programming. I am using Dev C++ and there is a bug. Can anyone help me to find out...

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
#include <stdlib.h>
#include<windows.h>
#include <time.h>

void main()
{
    printf("Welcome to MCQs Test.\nPhysics CH # 1\nExercise MCQs......\nChoose the correct answer.\n\n");
    
    char ans;
    int true = 0, false = 0, count = 0;
    printf("1) Which of the following is an example of Simple Harmonic Motion?\n\n");
    printf("A. Motion of a simple pendulum\nB. The motion of ceiling fan\nC. The spinning of the Earth around its axis\nD. A bouncing ball on a floor\n\n");
    printf("Select your answer (a, b, c, d): ");
    scanf("%c", &ans);
    if(ans == 'a'){
        printf("\nCorrect ^_^\n\n");
        true++;
    } else if(ans != 'a'){
        printf("\nOops! Your answer is Wrong.\nThe Correct answer is: \'A. Motion of a simple pendulum\'  \nBetter luck Next time....\n\n");
        false++;
    }
    count++;
    printf("Press any key to continue...\n\n");
    getch();
    
    printf("2) If the mass of the bob of a pendulum is increased by a factor of 3, the period of the pendulum`s motion will\n\n");
    printf("A. be increased by a factor of 2\nB. remain the same\nC. be decreased by a factor of 2\nD. be decreased by a factor of 4\n\n");
    printf("Select your answer (a, b, c, d): ");
    scanf("%c", &ans);
    if(ans == 'b'){
        printf("\nCorrect ^_^\n\n");
        true++;
    } else if(ans != 'b'){
        printf("\nOops! Your answer is Wrong.\nThe Correct answer is: \'B. remain the same\'  \nBetter luck Next time....\n\n");
        false++;
    }
    count++;
    printf("Press any key to continue...\n\n");
    getch();
    
    
    
    printf("%d of your answers is/are correct while %d is/are incorrect out of %d Questions\n", true, false, count);
    printf("Press any key to continue...\n\n");
    
    getch();
}

The output is:

Welcome to MCQs Test.
Physics CH # 1
Exercise MCQs......
Choose the correct answer.

1) Which of the following is an example of Simple Harmonic Motion?

A. Motion of a simple pendulum
B. The motion of ceiling fan
C. The spinning of the Earth around its axis
D. A bouncing ball on a floor

Select your answer (a, b, c, d): a

Correct ^_^

Press any key to continue...

2) If the mass of the bob of a pendulum is increased by a factor of 3, the period of the pendulum`s motion will

A. be increased by a factor of 2
B. remain the same
C. be decreased by a factor of 2
D. be decreased by a factor of 4

Select your answer (a, b, c, d): 

Here is the actual problem as it is not taking any input and just showing the result.

Oops! Your answer is Wrong.
The Correct answer is: 'B. remain the same'
Better luck Next time....

Press any key to continue...

1 of your answers is/are correct while 1 is/are incorrect out of 2 Questions
Press any key to continue...
hyde
  • 60,639
  • 21
  • 115
  • 176
  • Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). Here (as in most user interaction) it will be easier to obtain every input with `fgets` and then process the string. – Weather Vane Oct 20 '22 at 19:37
  • You're mixing `getch` with `scanf`--not a good idea. I'd define a buffer (e.g.) `char buf[100];` Then, I'd use `fgets(buf,sizeof(buf),stdin);` for all input. Then, when needed, `ans = buf[0];` – Craig Estey Oct 20 '22 at 19:41
  • With `%c` in scanf format string, always put a space before to discard any extra spaces and newlines: `scanf(" %c", ...)`. – hyde Oct 20 '22 at 19:42
  • Also always check `scanf` return value! Consider writing helper functions like `int scan_int(void) {...}` to keep your code neat. – hyde Oct 20 '22 at 19:44

0 Answers0