C quiz game 💯

Поділитися
Вставка
  • Опубліковано 15 жов 2024
  • C quiz game tutorial example explained
    #C #quiz #game

КОМЕНТАРІ • 61

  • @101thetruthguy
    @101thetruthguy Рік тому +57

    For those who got wrong answers even if you typed the correct ones. At the command scanf("%c"); //clear
    from input buffer
    just add an * after the % ->scanf("%*c");

    • @axiking6894
      @axiking6894 Рік тому +7

      Thanks, getchar(); works too

    • @ayaandtheworld
      @ayaandtheworld Рік тому +1

      Hello, stumbled on that problem and your solution worked. Thank you!

    • @DaniM2002
      @DaniM2002 Рік тому +2

      Thanks a lot, helped me in just 3 seconds

    • @ARSNAL794
      @ARSNAL794 Рік тому

      Thanks bro helped me a lot♥♥♥♥

    • @ospina79
      @ospina79 Рік тому

      Thks, works!

  • @BroCodez
    @BroCodez  3 роки тому +51

    #include
    #include
    int main()
    {
    char questions[][100] = {"1. What year did the C language debut?: ",
    "2. Who is credited with creating C?:",
    "3. What is the predecessor of C?: "};
    char options[][100] = {"A. 1969", "B. 1972", "C. 1975", "D. 1999",
    "A. Dennis Ritchie", "B. Nikola Tesla", "C. John Carmack", "D. Doc Brown",
    "A. Objective C", "B. B", "C. C++", "D. C#"};
    char answers[] = {'B', 'A', 'B'};
    int numberOfQuestions = sizeof(questions)/sizeof(questions[0]);
    char guess = ' ';
    int score = 0;
    printf("QUIZ GAME
    ");
    for(int i = 0; i < numberOfQuestions; i++)
    {
    printf("*********************
    ");
    printf("%s
    ", questions[i]);
    printf("*********************
    ");
    for(int j = (i * 4); j < (i * 4) + 4; j++)
    {
    printf("%s
    ", options[j]);
    }
    printf("guess: ");
    scanf("%c", &guess);
    scanf("%*c"); //clear
    from input buffer
    guess = toupper(guess);
    if(guess == answers[i])
    {
    printf("CORRECT!
    ");
    score++;
    }
    else
    {
    printf("WRONG!
    ");
    }
    }
    printf("*********************
    ");
    printf("FINAL SCORE: %d/%d
    ", score, numberOfQuestions);
    printf("*********************
    ");
    return 0;
    }

    • @spicasuzuki1908
      @spicasuzuki1908 2 роки тому +2

      Hey bro, this code has an error. i solved this by changing "int score " to "int score = 0;" ily bye

    • @RimodCoder
      @RimodCoder 2 роки тому +1

      @@spicasuzuki1908 hi bro can you give a correct code to me 👈👈

    • @vedrecide
      @vedrecide 2 роки тому +1

      Hi bro!
      The "%c" should be changed to "%*c" in the line where you clean
      from the input buffer.

    • @joshuatan5305
      @joshuatan5305 2 роки тому

      Nice

    • @harshak1ng237
      @harshak1ng237 2 роки тому +1

      This code works only on Online compilers only . I didnt get correct answer in VS codes.

  • @Omsingh-pc5db
    @Omsingh-pc5db 4 місяці тому +3

    if anyone is wondering
    just scanf doesn't work, make an extra char variable and use the extra scanf to take input for the extra variable like
    char null;
    scanf("%c",&guess);
    scanf("%c",&null);
    it shall work then.
    Welcome!.

  • @PSIwolf39
    @PSIwolf39 9 місяців тому +4

    Here's a quiz that I made:
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    int main(){
    char nullptr;
    char questions[4][75] = {"Where was mount rushmore carved?", "what is 11 + 2?", "What language is windows written in?","Is killing people illegal?"};
    char options[4][75] = { "A. The moon
    B. Earth
    C. Pluto (IT'S A REAL PLANET!)",
    "A. 23
    B. 12
    C. The age of consent in Japan",
    "A. C standard
    B. C#
    C. Python",
    "A. Yes
    B. No
    C. Depends on where you live"};
    char correctAnswers[4] = "BCAC";
    char userInput = '\0';
    int questionAmmount = sizeof(questions)/sizeof(questions[0]);
    int incrementPerQuestion = 100/questionAmmount;
    int score = 0;
    for(int i = 0; i < questionAmmount; i++){
    printf("
    Question #%d: %s
    %s
    ",i+1,questions[i],options[i]);
    scanf("%c",&userInput);
    scanf("%c",&nullptr);
    userInput = toupper(userInput);
    if(correctAnswers[i] == userInput){
    score+=incrementPerQuestion;
    }
    }
    printf("You got %d%% of the questions correct.",score); // two percent signs (%%) prints a % sign.
    if(score == 100){
    printf("
    CONGRATULATIONS!");
    }
    }

  • @salamoyt3762
    @salamoyt3762 Місяць тому +1

    if you have a problem with your scanf("%c"); dont use it, just make a space before the %c of the original scanf function like : scanf(" %c",guess); and that's it!

  • @fedibaklouti3239
    @fedibaklouti3239 11 місяців тому

    I have an issue !
    Scanf() returns the characters with a space behind like( A) for example
    How to fix it?
    Should i make it scanf("/n%c", &guess)??
    Coz it fixes it for this exp but i don't know if it's right or not!!!!
    Please answer ❤

  • @elleriee7083
    @elleriee7083 2 роки тому +1

    thankyou so so much!!! first time i encounter someone didn't use cin/cout !!! keep creating more tutorials!!!

    • @jurescuiacob2237
      @jurescuiacob2237 8 місяців тому +2

      That is used for c++, this is a c tutorial😂

  • @jamesdarrigo8277
    @jamesdarrigo8277 3 роки тому +13

    Are you actually typing that fast??

    • @BossKing1243
      @BossKing1243 Рік тому +3

      He is a skilled programmer, it makes sense

  • @NNNedlog
    @NNNedlog 2 роки тому +1

    thanks a lot, this is really cool and you're a really cool bro

  • @divinityinversace
    @divinityinversace Рік тому

    let's say an user types more than one character for guess input. this ends up leading to the rest of the user inputs for questions being skipped. how do you make it so it just goes to the next question instead of straight to the end of quiz?

    • @PSIwolf39
      @PSIwolf39 9 місяців тому

      You can do that by using an fgets() function and only using the character at index [0] as an answer.
      example:
      #include
      #include
      #include
      #include
      #include
      #include
      #include
      int main(){
      char temporary[100];
      char userInput;
      int score = 0;
      printf("Answer the following quesitions using \'t\' for true and \'f\' for false.
      ");
      printf("question 1: 2+2=5?
      ");
      fgets(temporary,100,stdin);
      userInput = temporary[0];
      if(userInput == 'f'){
      score++;
      }
      printf("question 2: 5+2=7?
      ");
      fgets(temporary,100,stdin);
      userInput = temporary[0];
      if(userInput == 't'){
      score++;
      }
      printf("your score is %d/2",score);
      }

  • @rociolpz143
    @rociolpz143 Рік тому

    What if you want to guess a number and you use srand but still want to create a 2-D array to print options? Can this be done?

  • @nikwl0
    @nikwl0 Рік тому

    is it possible to randomize the set of questions? new to c program here

    • @divinityinversace
      @divinityinversace Рік тому

      yea um idk if u figured it out by now but what i did was have an array to store the question order. then i made a function that gets random numbers from 0 to 3 (using rand()) and have it store each random number in the array.
      also you want to make sure it does not store duplicate random numbers so i'd make a loop to just check if the number is already there in the array.
      after this just return the array to your main function (this requires use of pointers and dynamic memory allocation so idk maybe not beginner friendly?).
      btw the array is just the question numbers so like 0 would be the first and 1 the second etc.. you could then use if statements to check if the array element is 0 and if it is print the question.

  • @shiwari1280
    @shiwari1280 3 роки тому +1

    Thanks for this tutorial Bro!!!!

  • @Nino-rr5tn
    @Nino-rr5tn 3 роки тому +1

    Thanks for this tutorial Bro ❤️

  • @30secondsmeme30
    @30secondsmeme30 2 роки тому +1

    i love you bro!

  • @ceb50_vedantsuryakantmali41
    @ceb50_vedantsuryakantmali41 2 роки тому

    Thanks for this toutorial bro.
    Btw which application is this for C-programming

  • @NoEscpFromMyMind
    @NoEscpFromMyMind 2 роки тому

    What I need to do to put the right answer if I put the wrong answer?

  • @saworieza
    @saworieza 2 роки тому +1

    Can someone tell me why when i run the below code. I get wrong when i enter 2 as the first answer?
    #include
    #include
    int main() {
    //introduce option
    char option;
    char easy_questions[][100] ={
    "1. What is 5 - 3?.",
    "2. What is 5 + 4?",
    "3. What is 6 - 2?"
    };
    char easy_answers[3] = {'2','9','4'};
    // calculate number of questions that we have
    int numberOfQuestions = sizeof(easy_questions)/sizeof(easy_questions[0]);
    // store guess
    char guess;
    // track score
    int score, grade;
    printf("Welcome To Maths Tester Pro.
    Select a Difficulty.
    ");
    //select options
    printf("1. Easy
    ");
    printf("2. Medium
    ");
    printf("3. Hard
    ");
    printf("> ");

    //get choice from user
    option=getchar();

    switch(option){
    case '1':
    printf("Easy Mode Selected
    ");
    for(int i=0; i < numberOfQuestions; i++){
    printf("%s ", easy_questions[i]);
    printf("You have 3 lives remaining
    ");
    printf("Answer: ");
    scanf("%c", &guess);
    while ((getchar()) != '
    '); // clear
    from input buffer
    //check if answer is correct
    if (guess==easy_answers[i]){
    printf("Correct!
    ");
    score++;
    } else {
    printf("Wrong
    ");
    }
    };
    //display user score
    printf("Test Complete
    You Scored %d/%d
    ", score, numberOfQuestions);
    break;
    case '2':
    printf("Medium Mode Selected
    ");
    break;
    case '3':
    printf("Hard Mode Selected
    ");
    break;
    default:
    printf("Your Selection is Incorrect
    ");
    break;
    }
    return 0;
    }

    • @Nissan-S15
      @Nissan-S15 Рік тому +3

      when you take input from user instead of using
      scanf("%c",%guess)
      do:-
      scanf(" %c",&guess) //space between " and % sign
      Also do
      int score =0; instead of int score;

  • @mizanurrahmanhridoy68
    @mizanurrahmanhridoy68 2 роки тому +1

    How I add their a limited time?

  • @Animix3.00-qh9zo
    @Animix3.00-qh9zo Рік тому

    i wish i could copy that typing speed though lol

  • @adityakulkarni1640
    @adityakulkarni1640 2 роки тому

    How would i do this with two users?

  • @sergejgligoric1736
    @sergejgligoric1736 2 роки тому

    Tutorial how to do C in VSC,i got too much errors

  • @kill_106
    @kill_106 2 роки тому +5

    Thanks this literally saved me from my assignment

  • @CommonWealthSnow
    @CommonWealthSnow Рік тому

    I've copied the code straight from the pinned comment and im getting a segmentation fault (core dumped), any ideas?

    • @Zygfryd_z_Toussaint
      @Zygfryd_z_Toussaint Рік тому +6

      You have to initialize score value:
      int score = 0;
      and change scanf("%c"); to scanf("%*c"); in clearing guess from input buffer

    • @ibeshahoamin4261
      @ibeshahoamin4261 Рік тому +2

      @@Zygfryd_z_Toussaint Thank you so much you saved me

    • @Zygfryd_z_Toussaint
      @Zygfryd_z_Toussaint Рік тому +1

      @@ibeshahoamin4261 You're welcome ;)

    • @darouss1
      @darouss1 Рік тому

      solved thanks
      @@Zygfryd_z_Toussaint

  • @fedibaklouti3239
    @fedibaklouti3239 11 місяців тому

    What if i want to put 4 questions what's the deal with this f*** scanf()??

  • @yakutsoraya1423
    @yakutsoraya1423 2 роки тому +2

    Hey Bro, I copied your code, but I got 0/3 score although I enter the correct answers.

    • @gabri2692
      @gabri2692 2 роки тому

      Hi, you still need help?

    • @James-un8io
      @James-un8io 2 роки тому +2

      It didn't work for me either and I don't have a clear understanding of how buffer seems to work either
      but you can make the program work by replacing
      scanf("%c", &guess);
      scanf("%c"); //clear
      from input buffer
      with
      guess=getch();

    • @James-un8io
      @James-un8io 2 роки тому

      @@gabri2692 sorta if you can help me understand how buffers work in c
      also copied code didn't work for me either I had to make some changes
      what was the error in the copied code
      if you know the answers to all that

    • @qaw392
      @qaw392 Рік тому

      @@James-un8io change scanf("%c"); to scanf("%*c"); in clearing guess from input buffer // credit to op

    • @James-un8io
      @James-un8io Рік тому

      @@qaw392 does adding * make it a pointer or something