Skip to main content

[C++] Write a Program that calculates How Much a Student Organization Earns During its Fund Raising Candy Sale.




Write a Program that calculates How Much a Student Organization Earns During its Fund Raising Candy Sale.
The program should prompt the user to enter then umber of candy bars sold and the amount the organization earns for each bar sold. It should then calculate and display the total amount earned.


How did I approach this problem?
First, I defined the objective of the program, which is to calculate the money raised from selling candy bars. Then, I came up with the names and types of variable for the program. Lastly, I started writing the code.

Here is my code with explanatory comments:
// Amr - http://www.a-wanly.com/
// C++ Excersie
// IDE - Visual Studio 2017
#include "stdafx.h"
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
unsigned int numofCandyBars = 0; /* + We are using unsigned int since it is not possible to have a negative number of candy bars sold, we go from 0 and up.
+ Using unsigned will save up some memory
+ Setting the variable to 0 will intialize it
*/
double priceofEachBar = 0; /* + We are using double as we could be dealing with fraction of numbers. Ex: $1, $2.5, etc...
+ Setting the variable to 0 will intialize it
*/
double earnings = 0; /* + We are using double as we could be dealing with fraction of numbers. Ex: $1, $2.5, etc...
+ Setting the variable to 0 will intialize it
*/
cout << "Student Organization - Candy Bar Earnings:" << endl;
cout << "------------------------------------------\n" << endl;
cout << "How many bars did the student org. sell? - Please Enter a Value: " << endl;
cin >> numofCandyBars; //whatever values the user inputs for the number of candy bars will be stored in numofCandyBars
cout << endl;
cout << "For how much did the student org. sell each bar? - Please Enter a Value" << endl;
cin >> priceofEachBar; //whatever values the user inputs for the price of a candy bar will be stored in priceofEachBar
cout << endl;
//Now we will calculate the earnings
earnings = numofCandyBars * priceofEachBar;
cout << "The Student Org. raised: $" << earnings << "" << endl;
cout << "Thank you for using the earning calculator!\n";
return 0;
}
view raw CandySales.cpp hosted with ❤ by GitHub

Popular posts from this blog

Logic Gate Truth Tables - Reference Guide (Cheat Sheet)

A logic gate is the implementation of a Boolean function. It performs a logic operation on one or more binary input. By binary I mean that the input can either be 1 or 0, nothing else. This guide is intended to be quick guide\reference, I will dive into more details about each gate on transistor\diode level in other posts. The guide cover gates with 2 inputs, meaning that each truth table will have 4 cases (2^2 = 4) AND Gate: Boolean Logic Operator: Q = A  •  B  Input Output A B Q 0 0 0 0 1 0 1 0 0 1 1 1 Summary: in order to achieve 1 on the output, both inputs A AND B must be 1 OR Gate: Boolean Logic Operator: Q = A  +  B  Input Output A B Q 0 0 0 0 1 1 1 0 1 1 1 1 Summary: in order to ...

LTspice Tutorial: How to Design and Simulate a Circuit in LTspice

 If you do not have LTSpice already installed on your machine, please head over to LTSpice's page on Analog Device's website (Link: LTspice Information Center | Analog Devices ). Note: a video version of this tutorial is available here  Click on the applicable download button that suits your machine's operating system. Upon successfully downloading and launching LTspice, you will be greeted with a window that looks something like in the figure below. Alright, now we will need to create a new schematic. That can be done by either of the following approaches:  click on File then choose New Schematic . click on the New Schematic symbol   on the toolbar. on your keyboard, press Ctrl+N . Upon doing so, a new grey-ish screen will pop up... that will be your new empty schematic (just like the one in the figure below). In this example, we will try to design and simulate a very basic voltage divider.  To recap: a voltage divider is a circuit that takes a certain i...

Hercules Motor Controller Library Reference Guide (Controlling the DC Motors)

This guide is not intended to be a tutorial on how to setup the Hercules Motor Controller (HMC) and install its library. If you need help with that, please visit the following link: CLICK HERE (^_^) First, we will need to include the Hercules Motor Controller library so we can use its functions to control the DC motors connected to the HMC, please add #include <Hercules.h> to the top of your source code like in the picture below: To control the motors, we will be using a function called 'MOTOR' followed by an action: To Initialize the DC Motors, we write: MOTOR.begin(); To run them: MOTOR.setRun1(); MOTOR.setRun2(); where setRun1 will run the motor(s) connected to the terminal M1 and setRun2 will run the motor(s) connected to the terminal M2 To stop them: MOTOR.setStop1(); MOTOR.setStop2(); where  setStop1  will stop the motor(s) connected to the terminal M1 and  setStop2  will stop the motor(s) connected to the terminal M2...