3 C++ Beginner Projects
👋 Welcome to my blog, where I showcase some of my favorite programming projects! In this post, I'm excited to share three fun and useful projects that I've created using C++.
First up, we have the "Simple Calculator". This project may seem basic, but don't let its simplicity fool you! With this calculator, you'll be able to crunch numbers like a pro. Plus, it's a great starting point for anyone new to programming.
Next, we have the "Inspiration Generator". Feeling a bit stuck or uninspired? This program has got you covered. With just a few clicks, you can generate and display inspiring quotes that will motivate and uplift you.
Last but not least, we have the "Planetary Weight Calculator". Ever wonder how much you would weigh on Jupiter? Look no further! With this program, you can easily calculate the weight of an object based on its mass and the gravitational acceleration of a given planet.
All three projects are simple yet practical, making them great for beginners or anyone looking for a quick coding fix. I hope you enjoy tinkering with them as much as I did! 🚀
Simple Calculator
🧮 The "Simple Calculator" is a great beginner project that will teach you the basics of C++ programming. In this project, you will learn how to create a calculator that can perform simple arithmetic operations such as addition, subtraction, multiplication, and division.
The calculator works by taking in two numbers and an operator (+, -, *, or /) as inputs from the user. It then performs the operation and displays the result to the user.
The program uses a switch statement to determine which operation to perform based on the operator input by the user. For example, if the user enters "+", the program will add the two numbers together and display the result.
The "Simple Calculator" is a fun and useful project that will give you a solid foundation in C++ programming. Give it a try and see how many complex calculations you can solve with just a few lines of code! 🔢
Here's an explanation of each line of code:
#include <iostream>
This line includes the iostream library, which provides input and output functionality.
using namespace std;
This line tells the program to use the standard namespace.
int main() {
This line declares the main function, which is the entry point of the program.
float num1, num2, result;
This line declares three float variables: num1, num2, and result.
char op;
This line declares a character variable op, which will hold the arithmetic operator (+, -, *, /)
input by the user.
cout << "Enter first number: ";
cin >> num1;
These lines prompt the user to enter the first number, and then store it in the num1 variable.
cout << "Enter second number: ";
cin >> num2;
These lines prompt the user to enter the second number, and then store it in the num2 variable.
cout << "Enter operator (+, -, *, /): ";
cin >> op;
These lines prompt the user to enter an arithmetic operator, and then store it in the op variable.
switch(op) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
cout << "Invalid operator";
return 0;
}
These lines use a switch statement to determine which arithmetic operation to perform based on the value of op
. Depending on the value of op
, the program performs addition, subtraction, multiplication, or division on num1
and num2
, and then stores the result in the result
variable. If the user enters an invalid operator, the program prints an error message and exits.
cout << num1 << " " << op << " " << num2 << " = " << result;
This line displays the result of the arithmetic operation to the user.
return 0;
}
This line indicates the end of the main function, and returns the value 0 to the operating system to indicate successful completion of the program.
📣 Inspiration Generator📣
Are you feeling down or uninspired? Are you looking for a quick pick-me-up or some words of wisdom? Look no further than our Inspiration Generator!
Our program generates and displays inspiring quotes from some of the world's most well-known figures, including Theodore Roosevelt, Mahatma Gandhi, Thomas Edison, Winston Churchill, and more!
Using a vector to store the quotes, our program randomly selects a quote and displays it with the author's name. With each run, you can get a different quote to inspire and motivate you to tackle your day.
So, whenever you need a little extra boost, run our Inspiration Generator and get inspired!
Here's a line-by-line explanation of the code for the Inspiration Generator project:
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
The code starts by including several C++ standard libraries that are used in the program. iostream
is used for input and output, string
is used to represent text strings, vector
is a data structure used to store and manipulate collections of data, cstdlib
is used for various system-level functions, and ctime
is used for manipulating time and dates.
using namespace std;
This line indicates that we are using the std
namespace, which is a collection of functions and objects that are provided by the C++ standard library.
struct Quote {
string text;
string author;
};
This defines a structure called Quote
which holds information about a quote, including the text of the quote and the author.
int main() {
srand(time(0));
This is the main function, where the program starts running. The srand
function seeds the random number generator with the current time, so that the generated numbers will be different each time the program is run.
vector<Quote> quotes = {
{"Believe you can and you're halfway there.", "Theodore Roosevelt"},
{"Be the change that you wish to see in the world.", "Mahatma Gandhi"},
{"I have not failed. I've just found 10,000 ways that won't work.", "Thomas Edison"},
{"Success is not final, failure is not fatal: It is the courage to continue that counts.", "Winston Churchill"},
{"You miss 100% of the shots you don't take.", "Wayne Gretzky"},
// Add more quotes here
};
This creates a vector called quotes
that holds a collection of Quote
structures. In this example, the vector is initialized with a few sample quotes and their authors.
int index = rand() % quotes.size();
This generates a random index between 0 and the size of the quotes vector minus one. This index will be used to select a random quote from the vector.
cout << quotes[index].text << endl;
cout << "- " << quotes[index].author << endl;
These lines print the selected quote to the console, along with the name of the author.
return 0;
}
This line indicates that the main function has finished running and the program should exit with a return value of 0.
The Planetary Weight Calculator
The Planetary Weight Calculator is a simple C++ program that allows the user to calculate the weight of an object on a specific planet. The program uses a map to hold the gravitational acceleration values for each planet and prompts the user to enter the mass of the object in kilograms and the name of the planet. Then, it calculates the weight of the object in Newtons and displays the result to the user.
Let's take a closer look at the code.
First, the necessary header files are included:
#include <iostream>
#include <string>
#include <map>
Then, a map called "gravities" is defined to hold the gravitational acceleration values for each planet:
map<string, double> gravities = {
{"Earth", 9.81},
{"Moon", 1.62},
{"Mars", 3.71},
{"Jupiter", 24.79},
{"Saturn", 10.44},
{"Uranus", 8.87},
{"Neptune", 11.15}
};
In the main function, two variables are defined to hold the mass of the object and the name of the planet:
double mass;
string planet;
Then, the user is prompted to enter the mass of the object in kilograms and the name of the planet:
cout << "Enter the mass of the object in kilograms: ";
cin >> mass;
cout << "Enter the name of the planet: ";
cin >> planet;
The program then calculates the weight of the object in Newtons using the formula weight = mass * gravitational acceleration:
double gravity = gravities[planet];
double weight = mass * gravity;
Finally, the result is displayed to the user:
cout << "The weight of the object on " << planet << " is " << weight << " Newtons." << endl;
That's it! The program is simple but useful for anyone interested in knowing how much they would weigh on different planets.
Here is the full code:
#include <iostream>
#include <string>
#include <map>
using namespace std;
// Define a map to hold the gravitational acceleration values for each planet
map<string, double> gravities = {
{"Earth", 9.81},
{"Moon", 1.62},
{"Mars", 3.71},
{"Jupiter", 24.79},
{"Saturn", 10.44},
{"Uranus", 8.87},
{"Neptune", 11.15}
};
int main() {
double mass;
string planet;
// Prompt the user to enter the mass of the object in kilograms
cout << "Enter the mass of the object in kilograms: ";
cin >> mass;
// Prompt the user to enter the name of the planet
cout << "Enter the name of the planet: ";
cin >> planet;
// Calculate the weight of the object in Newtons
double gravity = gravities[planet];
double weight = mass * gravity;
// Display the result
cout << "The weight of the object on " << planet << " is " << weight << " Newtons." << endl;
return 0;
}
By the way, DO NOT MISS OUR YOUTUBE CHANNEL
Here is the link to our latest video: