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");
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!.
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!"); } }
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!
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 ❤
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?
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); }
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.
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; }
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;
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();
@@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
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");
Thanks, getchar(); works too
Hello, stumbled on that problem and your solution worked. Thank you!
Thanks a lot, helped me in just 3 seconds
Thanks bro helped me a lot♥♥♥♥
Thks, works!
#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;
}
Hey bro, this code has an error. i solved this by changing "int score " to "int score = 0;" ily bye
@@spicasuzuki1908 hi bro can you give a correct code to me 👈👈
Hi bro!
The "%c" should be changed to "%*c" in the line where you clean
from the input buffer.
Nice
This code works only on Online compilers only . I didnt get correct answer in VS codes.
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!.
thanks bro
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!");
}
}
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!
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 ❤
thankyou so so much!!! first time i encounter someone didn't use cin/cout !!! keep creating more tutorials!!!
That is used for c++, this is a c tutorial😂
Are you actually typing that fast??
He is a skilled programmer, it makes sense
thanks a lot, this is really cool and you're a really cool bro
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?
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);
}
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?
is it possible to randomize the set of questions? new to c program here
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.
Thanks for this tutorial Bro!!!!
Thanks for this tutorial Bro ❤️
i love you bro!
Thanks for this toutorial bro.
Btw which application is this for C-programming
What I need to do to put the right answer if I put the wrong answer?
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;
}
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;
How I add their a limited time?
i wish i could copy that typing speed though lol
How would i do this with two users?
Tutorial how to do C in VSC,i got too much errors
Thanks this literally saved me from my assignment
I've copied the code straight from the pinned comment and im getting a segmentation fault (core dumped), any ideas?
You have to initialize score value:
int score = 0;
and change scanf("%c"); to scanf("%*c"); in clearing guess from input buffer
@@Zygfryd_z_Toussaint Thank you so much you saved me
@@ibeshahoamin4261 You're welcome ;)
solved thanks
@@Zygfryd_z_Toussaint
What if i want to put 4 questions what's the deal with this f*** scanf()??
Hey Bro, I copied your code, but I got 0/3 score although I enter the correct answers.
Hi, you still need help?
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();
@@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
@@James-un8io change scanf("%c"); to scanf("%*c"); in clearing guess from input buffer // credit to op
@@qaw392 does adding * make it a pointer or something