I just learned structs, and I'm trying to get the idea of commenting, if somebody can give it a look it would be awesome. Also this is not my code I got this code from learncpp.com in the tutorial section. I did the problem but sadly failed, I got half right and decided to give it another try. I did look at the solution on the second try to see what I was doing wrong.
// Lesson 4.7 on learncpp.com
/*
* 1) You are running a website, and you are trying to keep track of how much money you make per day from advertising.
* Declare an advertising struct that keeps track of how many ads you’ve shown to readers
* , what percentage of users clicked on ads, and how much you earned on average from each ad that was clicked.
* Read in values for each of these fields from the user.
* Pass the advertising struct to a function that prints each of the values,
* and then calculates how much you made for that day (multiply all 3 fields together).
*
*/
#include <iostream>
usingnamespace std;
// I used a struct that I declare it as Advertisement in order to contain 3 different variables inside,
//this will help it pass through a function easily w/o having to pass each variable individually
struct Advertisement{
int adsShown;
double percentOfClickedAds;
double averageEarnedPerClick;
};
/* I used a void b/c we didn't need a return in this case, we just need to print out information
I passed the struct that I declare it as Advertisement into the function
Inside the function in between the block scope, it prints out information of
advertisements shown, percentage of ads click on, & avg. earn from each ads.
and total earnings. I did the calculation here b/c it needs to calculate the total earnings of the ads */
void printInfo(Advertisement ads){
cout << " Number of advertisements shown:"<< " " << ads.adsShown << endl;
cout << "Number of percentage of advertisement clicked on:" << ads.percentOfClickedAds << "%" << endl;
cout << "Average earned from each advertisements" << ads.averageEarnedPerClick << endl;
cout << "Total earnings: $ " <<
(ads.adsShown * ads.percentOfClickedAds / 100 *ads.averageEarnedPerClick) << endl;
}
int main(){
// I called the declare structure Advertisement to pass the declare variables here
// Not really sure what this is suppose to be awesome if someone explain?
Advertisement ads;
// The user is ask how many ads were shown today.
cout << "How ads were shown today?";
// This shows the user how many number of advertisements is being shown from the function above.
cin >> ads.adsShown;
// The user is ask the percentage of click ads
cout << "What were the percentage of the users that click on the ads?";
// This shows the user how many percentage the users click on to see an ad
cin >> ads.percentOfClickedAds;
// The user is ask what is the average earning per click
cout << "What was the average earning per click?";
// This shows the user to see the average earn of ads per clicked
cin >> ads.averageEarnedPerClick;
/* I called the function here that I declared it as ads to print out the
/information in b/w the block scope of the function*/
printInfo(ads);
return 0;
}
I want to know this, why this is here, what is it doing etc.
1 2 3 4
// I called the declare structure Advertisement to pass the declare variables here
// Not really sure what this is suppose to be awesome if someone explain?
Advertisement ads;
ads is called an instance I think of the Advertisement structure; its kind of like you're creating a variable of type Advertisement. ads in main() let's you do things like cin >> ads.adsShown;. In other words it lets you assign values to the variables in the Advertisement structure.