Steemit Learning Club S23W2:Creating quizzes and viewing quiz scores with C++ programs.

in Hindwhale Communityyesterday

Steemian Friends,
Today, I will participate in Steemit Learning Club 23 Week 02 of the Technology and Development Club. I will share how to create a quiz with a C++ program and get the results. I am new to learning C++ programming. I am getting the opportunity to practice my coding knowledge through the technology and development club. I like to learn technology. I am very behind in knowing technology. So I can learn little by little about technology from the Steemit platform. However, I am an electrical engineer. I am very inspired by the technical posts of Sirs @alejos7ven, @kafio and @mohammadfaisal, mentors of the Technologies and Development Club. I read Mentor's programming posts whenever I get time. I have seen many talented programmers on the Steemit platform. I hope mentors will help me by giving suggestions on how to learn the technology.

Creating quizzes and viewing quiz scores with C++ programs..png
Design By Canva

I have divided the quiz program into parts for easy understanding while creating it. First, I made the questions. I wrote three questions on the C++ program as a quiz.

Question 1: What is the file extension for C++?
Ans:cpp
Question 2: Which command is used to display output in C++?
Ans:cout
Question 3: What functions are a must-have to start C++ programming?
Ans: main

Then, I gave four answers to the questions. I used conditions to find the correct answer out of four options. The condition was checked with the correct answer number for each question. If the correct number is typed, the program will answer correctly and add one number to the total score. Thus, if all three questions are answered correctly, the total score will show three. I have broken down the program into parts below. I hope everyone can easily understand my program.

Step-01

Header file or library function: Due to header file #include< iostream>, we use output with Cout and input with Cin in the program. Every C++ program uses a header file. A C++ program has several other header files.

Using namespace std; Because of this writing, Cin:: or Cout:: does not have to be written like this in the program. Its use makes writing programs easier.

#include < iostream>
using namespace std;

Step-02

Then, I started the main function of the program. Every C++ program has a main function. So, I have written the main function of the program below.

int main() {

Step-03

Then, I declared the variable. Every C++ program has to declare its variables. I have given scores to hold data of integer type to declare variables in my quiz program. First, the score started with zero. If the answer is correct, one will be added. Then I gave another variable named answer of integer type. Every question I have will be answered here.

int score = 0;
int answer;

Step-04

The program is first welcomed using Cout and then given a command for the new line through end1. This can also be done with \n.

cout << "Welcome to the C++ Quiz Program! " << endl;


Step-05


Now, I have written the first question for the first quiz in the program. Four options are given for the answer with the question. Then, the program will determine the correct and incorrect answers using the if else condition. I have given the number 2 option program as the correct answer here. If I type 2 as an answer, then my answer will be taken as correct, and my score will be one.

// Question 1
cout << "\nQuestion 1: What is the file extension for C++?\n";
cout << "1) .c\n2) .cpp\n3) .py\n4) .java\n";
cout << "Your answer: ";
cin >> answer;
if (answer == 2)
{
cout << "Correct answer! \n";
score++;
}
else
{
cout << "Wrong answer \n";
}


Step-06


Then, I wrote the second and third questions similarly in the program. I have given options 3 and 2 as correct answers. I hope everyone understands.

// Question 2
cout << "\nQuestion 2: Which command is used to display output in C++?\n";
cout << "1) print\n2) console.log\n3) cout\n4) echo\n";
cout << "Your answer: ";
cin >> answer;
if (answer == 3)
{
cout << "Correct answer! \n";
score++;
}
else
{
cout << "Wrong answer \n";
}
// Question 3
cout << "\nQuestion 3: What functions are a must-have to start C++ programming?\n";
cout << "1) start()\n2) main()\n3) begin()\n4) init()\n";
cout << "Your answer: ";
cin >> answer;
if (answer == 2)
{
cout << "Correct answer! \n";
score++;
}
else {
cout << "Wrong answer \n";
}


Step-07


Then, I wrote a C++ program to display the total score. If my correct answer is three, then the total score will be three. If all three of my answers are correct, a message will be sent to me. If both answers are correct, then a message will be displayed. If the score is not good, then a message will be given. Here, I have used an if-else condition.

// Quiz over, show score
cout << "\n Your total score: " << score << " / 3\n";
if (score == 3) {
cout << "Awesome! You fixed it all! \n";
} else if (score == 2) {
cout << "Good! Try a little harder!\n";
} else {
cout << "No problem, just practice more! \n";
}


Step-08


Then, I wrote a code to terminate the C++ program. We write it to end any C++ program.

return 0;

quiz -1.png

quiz-2.png

I write the program on paper, then go to code blocks and type. After writing the program to the code block, an error occurs. I made a mistake in the double quotation. I have written the program below and given the selfie.

IMG20250225124011.jpg

IMG20250225123457.jpg

Output:
Then, I output the entire program. I type 2 first. The answer to the first question out of four options is number 2, so it gave me one score as the correct answer. Thus, I gave three correct answers, so my total score is three. That's why it gives me a message. I have shown the program's output with a screenshot below.

q ans-1.png

q ans-2.png

q ans-3.png

q ans-4.png

Then, I gave two wrong answers, and my score was one and made a comment. Below is a screenshot.

q ans-5.png

I opened a GitHub account as you suggested in week 01 and placed my program code on GitHub. I don't know much about GitHub usage, though. Below, I have provided the file link containing my code.

https://github.com/mahadisalim/C-project/commit/0e6fb136e43c59ccfb65f4ff4150970326fc5461

Blue line.png

SL No.My Invited Steemit Friends
1@memamun
2@shahariar1
3@lirvic

Steemit.com.png

Sort:  

Thank you @mahadisalim for your efforts, I like your eagerness to learn development, I noticed you've incorporated some of my previous remarks like variable naming and Git usage, let's go through some suggestions for improvement:

Testing multiple scenarios

  • The program working correctly for one test case doesn't mean it works well for all cases, you should test many examples, not just the happy path.
  • Even the best programs will have bugs , that's why the term "bug" exists in programming, good developers test extensively across different scenarios.
  • Try testing with invalid inputs, boundary cases, and unexpected user behavior to make your program more robust.

Input validation.

  • The program could crash if a user enters a letter instead of a number. There no validation to ensure users only input numbers between 1 and 4 .

Code modularity.

  • with only 3 questions ==> the code is manageable, but imagine having 100 questions ==> The code would become very lengthy and difficult to maintain.
  • Consider using functions to make your code more modular , example: create a function for asking questions that can be reused.

Program expandability.

  • If you want to add new questions, you currently need to modify the source code directly, recompile, and run again.
  • A better approach would be to create a function that can add new questions dynamically or read questions from an external file.

GitHub.

  • I think you are uploading your work to GitHub manually instead of using Git commands, I encourage you to learn about Git commands like git add, git commit, and git push.
    Learning these commands will make your workflow more efficient, and they are easy to work with, they will enhance your skills

You work is clear, and the examples you provide are easy to understand. It's good for beginners to learn, also, I see you worked in a C++ editor instead of an online editor, which is great. Keep going, brother

Thanks sir, for showing me the updated topics of C++ program. I'm a new user on GitHub so I did it manually. But I will learn about the three things you mentioned on GitHub. Later, I will add the program codes to GitHub. You are right I need smarter coding knowledge to add a hundred quizzes. I will learn about it.

Coin Marketplace

STEEM 0.15
TRX 0.22
JST 0.031
BTC 84021.98
ETH 2299.61
USDT 1.00
SBD 0.68