C++ Mini Project With Source Code and Output
1.Here is the C++ source code for a straightforward calculator with basic arithmetic capabilities:
#include <iostream>
using namespace std;
int main() {
// Declare variables.
int n1, n2;
char optr;
cout << "Enter two nums: ";
cin >> n1 >> n2;
cout << "Enter an operator to be performed(+, -, *, /): ";
cin >> optr;
switch (optr) {
case '+':
cout << n1 << " + " << n2 << " = " << n1 + n2 << endl;
break;
case '-':
cout << n1 << " - " << n2 << " = " << n1 - n2 << endl;
break;
case '*':
cout << n1 << " * " << n2 << " = " << n1 * n2 << endl;
break;
case '/':
cout << n1 << " / " << n2 << " = " << n1 / n2 << endl;
break;
default:
cout << "Invalid operator." << endl;
}
return 0;
}
Output:
![C++ Mini Project With Source Code and Output](https://static.tutorialandexample.com/cpp/cpp-mini-project-with-source-code-and-output1.png)
2) A C++ program that enables users to add, viewpoint, and handle their to-do lists with features like completing tasks and eliminating unfinished ones:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Task {
public:
string description;
bool isCompleted;
Task(string description, bool isCompleted) {
this->description = description;
this->isCompleted = isCompleted;
}
};
int main() {
vector<Task> taskList;
while (true) {
// Display menu options
cout << "Menu:\n";
cout << "1. Add a task\n";
cout << "2. View tasks\n";
cout << "3. Mark a task as completed\n";
cout << "4. Remove a task from the list\n";
cout << "5. Exit\n";
// Get user input for menu choice
cout << "Enter your choice (1-5): ";
string input;
getline(cin, input);
int choice = stoi(input);
if (choice == 1) {
// Add a task to the list
cout << "Enter the task description: ";
string description;
getline(cin, description);
taskList.push_back(Task(description, false));
} else if (choice == 2) {
// View the task list
cout << "Task List:\n";
for (Task task : taskList) {
cout << task.description << " " << (task.isCompleted ? "(Completed)" : "(Incomplete)") << endl;
}
} else if (choice == 3) {
// Mark a task as completed
cout << "Enter the index of the task to mark as completed: ";
string indexInput;
getline(cin, indexInput);
int index = stoi(indexInput);
if (index >= 0 && index < taskList.size()) {
taskList[index].isCompleted = true;
} else {
cout << "Invalid index!\n";
}
} else if (choice == 4) {
// Remove a task from the list
cout << "Enter the index of the task to remove from the list: ";
string indexInput;
getline(cin, indexInput);
int index = stoi(indexInput);
if (index >= 0 && index < taskList.size()) {
taskList.erase(taskList.begin() + index);
} else {
cout << "Invalid index!\n";
}
} else if (choice == 5) {
// Exit the program
break;
} else {
cout << "Invalid choice! Please enter a valid choice.\n";
}
cout << endl;
}
return 0;
}
Output:
![C++ Mini Project With Source Code and Output](https://static.tutorialandexample.com/cpp/cpp-mini-project-with-source-code-and-output2.png)
![C++ Mini Project With Source Code and Output](https://static.tutorialandexample.com/cpp/cpp-mini-project-with-source-code-and-output3.png)
3) C++ Code for a bank account management system:
#include <iostream>
using namespace std;
class BankAccount {
public:
int account_number;
double balance;
BankAccount(int account_number, double balance) {
this->account_number = account_number;
this->balance = balance;
}
void deposit(double amount) {
balance += amount;
}
void withdraw(double amount) {
if (amount > balance) {
throw runtime_error("Insufficient funds");
}
balance -= amount;
}
double get_balance() {
return balance;
}
};
int main() {
cout << "Enter your account number: ";
int account_number;
cin >> account_number;
cout << "Enter the initial balance: ";
double balance;
cin >> balance;
BankAccount account(account_number, balance);
cout << "Enter the amount to deposit: ";
double amount;
cin >> amount;
account.deposit(amount);
cout << "The balance is now " << account.get_balance() << endl;
// Withdraw money.
cout << "Enter the amount to withdraw: ";
cin >> amount; // <-- Corrected line
account.withdraw(amount);
cout << "The balance is now " << account.get_balance() << endl;
return 0;
}
Output:
![C++ Mini Project With Source Code and Output](https://static.tutorialandexample.com/cpp/cpp-mini-project-with-source-code-and-output4.png)
The number of the account and the starting balance are the first two inputs the user makes to this code. Next, a BankAccount object is created with the specified account identifier and balance.
The user then enters the code for the deposit and withdrawal amount. Then, to deposit and withdraw the funds, it invokes the BankAccount object's deposit() and withdraw() methods.
The account balance is the last thing the code prints.
The prior code has been improved to make it more interactive. The user can use data provided by the console to deposit money into and take money from their savings account.
4) Student Record System: C ++ program to keep track of student records, which should include names, grades, and other pertinent data.
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int id;
int grade_level;
string major;
string courses_enrolled_in;
int grades[5];
int attendance[5];
};
int main() {
// Create a new student record.
Student student;
// Request the name of the student from the user.
cout << "Enter the student's name: ";
getline(cin, student.name);
// Request the student's ID from the user.
cout << "Enter the student's ID: ";
cin >> student.id;
// Request the student's current grade level.
cout << "Enter the student's grade level: ";
cin >> student.grade_level;
// Clear the input buffer after reading the grade level to avoid issues with subsequent input.
cin.ignore();
// Ask for the student's major area of study.
cout << "Enter the student's major: ";
getline(cin, student.major);
// Ask which courses the student is currently enrolled in.
cout << "Enter the courses the student is enrolled in: ";
getline(cin, student.courses_enrolled_in);
// Prompt the user for the student's grades in each course.
for (int i = 0; i < 5; i++) {
cout << "Enter the student's grade for course " << i + 1 << ": ";
cin >> student.grades[i];
}
// Request the student's attendance for each course.
for (int i = 0; i < 5; i++) {
cout << "Enter the student's attendance for course " << i + 1 << ": ";
cin >> student.attendance[i];
// Print the student's complete record.
cout << "Student name: " << student.name << endl;
cout << "Student ID: " << student.id << endl;
cout << "Student grade level: " << student.grade_level << endl;
cout << "Student major: " << student.major << endl;
cout << "Courses enrolled in: " << student.courses_enrolled_in << endl;
cout << "Student grades: ";
for (int i = 0; i < 5; i++) {
cout << student.grades[i] << " ";
}
cout << endl;
cout << "Student attendance: ";
for (int i = 0; i < 5; i++) {
cout << student.attendance[i] << " ";
}
cout << endl;
return 0;
}
}
Output:
![C++ Mini Project With Source Code and Output](https://static.tutorialandexample.com/cpp/cpp-mini-project-with-source-code-and-output5.png)
5) Game of Hangman: C++ code to implement a text-based Hangman in which players must guess words letter by letter.
#include <iostream>
#include <string>
#include <set>
using namespace std;
const int MAX_INCORRECT_GUESSES = 6;
void drawHangman(int incorrectGuessesLeft) {
cout << endl;
// ... Same as before ...
}
int main() {
string secretWord = "helo";
set<char> guessedLetters; // Using a set to store guessed letters
int incorrectGuessesLeft = MAX_INCORRECT_GUESSES;
while (incorrectGuessesLeft > 0) {
drawHangman(incorrectGuessesLeft);
cout << "Guessed letters: ";
for (char letter : guessedLetters) {
cout << letter << " ";
}
cout << endl;
char guess;
cout << "Guess a letter: ";
cin >> guess;
if (guessedLetters.count(guess) > 0) { // Check if the letter has already been guessed
cout << "You already guessed the letter " << guess << ". Try another letter." << endl;
continue;
}
if (secretWord.find(guess) != string::npos) {
cout << "Correct! The word contains the letter " << guess << endl;
guessedLetters.insert(guess); // Add the letter to the set of guessed letters
} else {
cout << "Incorrect! The word does not contain the letter " << guess << endl;
incorrectGuessesLeft--;
}
if (guessedLetters.size() == secretWord.length()) {
cout << "Congratulations! You have guessed the word!" << endl;
break;
}
}
if (incorrectGuessesLeft == 0) {
cout << "Sorry, you have run out of guesses. The word was " << secretWord << endl;
}
return 0;
}
Output:
![C++ Mini Project With Source Code and Output](https://static.tutorialandexample.com/cpp/cpp-mini-project-with-source-code-and-output6.png)
![C++ Mini Project With Source Code and Output](https://static.tutorialandexample.com/cpp/cpp-mini-project-with-source-code-and-output7.png)
6) Create a system that allows users to add, view, or search for books in a library to help manage the volumes there.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Book {
string title;
string author;
string isbn;
};
vector<Book> books;
void addBook() {
string title, author, isbn;
cout << "Enter the title of the book: ";
getline(cin, title);
cout << "Enter the author of the book: ";
getline(cin, author);
cout << "Enter the ISBN of the book: ";
getline(cin, isbn);
Book book = {title, author, isbn};
books.push_back(book);
}
void viewBooks() {
for (Book book : books) {
cout << "Title: " << book.title << endl;
cout << "Author: " << book.author << endl;
cout << "ISBN: " << book.isbn << endl;
}
}
void searchBooks(string title) {
bool found = false;
for (Book book : books) {
if (book.title.find(title) != string::npos) {
cout << "Title: " << book.title << endl;
cout << "Author: " << book.author << endl;
cout << "ISBN: " << book.isbn << endl;
found = true;
}
}
if (!found) {
cout << "Book with title \"" << title << "\" not found." << endl;
}
}
int main() {
int choice;
string title;
do {
cout << endl;
cout << "Library Management System" << endl;
cout << "1. Add book" << endl;
cout << "2. View books" << endl;
cout << "3. Search books" << endl;
cout << "4. Quit" << endl;
cout << "Enter your choice: ";
cin >> choice;
cin.ignore(); // to consume the newline character left by cin
switch (choice) {
case 1:
addBook();
break;
case 2:
viewBooks();
break;
case 3:
cout << "Enter the title of the book to search for: ";
getline(cin, title);
searchBooks(title);
break;
case 4:
cout << "Exiting Library Management System. Goodbye!" << endl;
break;
default:
cout << "Invalid choice" << endl;
}
} while (choice != 4);
return 0;
}
Output:
![C++ Mini Project With Source Code and Output](https://static.tutorialandexample.com/cpp/cpp-mini-project-with-source-code-and-output8.png)
![C++ Mini Project With Source Code and Output](https://static.tutorialandexample.com/cpp/cpp-mini-project-with-source-code-and-output9.png)
7) C++ code to create a system for managing contacts that enables users to add, modify, and search for contacts.
#include <iostream>
#include <string>
using namespace std;
struct Contact {
string name;
string phone_number;
string email;
};
void add_contact(Contact *contacts, int *size) {
Contact contact;
cout << "Enter the contact's name: ";
cin >> contact.name;
cout << "Enter the contact's phone number: ";
cin >> contact.phone_number;
cout << "Enter the contact's email address: ";
cin >> contact.email;
contacts[*size] = contact;
(*size)++;
}
void edit_contact(Contact *contacts, int *size) {
int index;
cout << "Enter the index of the contact you want to edit: ";
cin >> index;
if (index < 0 || index >= *size) {
cout << "Invalid index." << endl;
return;
}
Contact contact;
cout << "Enter the new contact's name: ";
cin >> contact.name;
cout << "Enter the new contact's phone number: ";
cin >> contact.phone_number;
cout << "Enter the new contact's email address: ";
cin >> contact.email;
contacts[index] = contact;
}
void search_contact(Contact *contacts, int size) {
string name;
cout << "Enter the contact's name you want to search for: ";
cin >> name;
for (int i = 0; i < size; i++) {
if (contacts[i].name == name) {
cout << contacts[i].name << " " << contacts[i].phone_number << " " << contacts[i].email << endl;
return;
}
}
cout << "Contact not found." << endl;
}
int main() {
Contact *contacts = new Contact[10];
int size = 0;
while (true) {
cout << endl;
cout << "1. Add contact" << endl;
cout << "2. Edit contact" << endl;
cout << "3. Search contact" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
int choice;
cin >> choice;
switch (choice) {
case 1:
add_contact(contacts, &size);
break;
case 2:
edit_contact(contacts, &size);
break;
case 3:
search_contact(contacts, size);
break;
case 4:
delete[] contacts;
return 0;
default:
cout << "Invalid choice." << endl;
}
}
}
Output:
![C++ Mini Project With Source Code and Output](https://static.tutorialandexample.com/cpp/cpp-mini-project-with-source-code-and-output10.png)
8) Tic-Tac-Toe: C++ console-based variant of the two-player classic game of Tic-Tac-Toe.
#include <iostream>
#include <string>
using namespace std;
const int BOARD_SIZE = 3;
char board[BOARD_SIZE][BOARD_SIZE];
void initialize_board() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = ' ';
}
}
}
void print_board() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
cout << board[i][j];
if (j < BOARD_SIZE - 1) {
cout << " | ";
}
}
cout << endl;
if (i < BOARD_SIZE - 1) {
cout << "---------" << endl;
}
}
}
bool is_winner(char player) {
for (int i = 0; i < BOARD_SIZE; i++) {
if (board[i][0] == player && board[i][1] == player && board[i][2] == player) {
return true;
} else if (board[0][i] == player && board[1][i] == player && board[2][i] == player) {
return true;
}
}
if (board[0][0] == player && board[1][1] == player && board[2][2] == player) {
return true;
} else if (board[0][2] == player && board[1][1] == player && board[2][0] == player) {
return true;
}
return false;
}
int main() {
initialize_board();
char player = 'X';
int turn = 0;
while (true) {
print_board();
cout << "Player " << player << "'s turn: ";
int row, col;
cin >> row >> col;
if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE || board[row][col] != ' ') {
cout << "Invalid move. Try again." << endl;
continue;
}
board[row][col] = player;
if (is_winner(player)) {
print_board();
cout << "Player " << player << " won!" << endl;
break;
}
turn++;
player = (player == 'X') ? 'O' : 'X';
if (turn == BOARD_SIZE * BOARD_SIZE) {
print_board();
cout << "It's a draw!" << endl;
break;
}
}
return 0;
}
Output:
![C++ Mini Project With Source Code and Output](https://static.tutorialandexample.com/cpp/cpp-mini-project-with-source-code-and-output11.png)
![C++ Mini Project With Source Code and Output](https://static.tutorialandexample.com/cpp/cpp-mini-project-with-source-code-and-output12.png)
![C++ Mini Project With Source Code and Output](https://static.tutorialandexample.com/cpp/cpp-mini-project-with-source-code-and-output13.png)
This programme will provide a two-player console version of the popular board game Tic-Tac-Toe. The participants will alternately place their symbols on a 3x3 board used for the game. The game is won by the first player to line up three of their icons in a row, column or diagonal.
The code can be run by compiling it using a C++ compiler and saving it as a.cpp file. You can run the code when it has been compiled by using the command that follows in a terminal window:
./tic_tac_toe
You can now play a game of Tic-Tac-Toe versus another person after doing this.