I don't know if I did this right.

closed account (yR9wb7Xj)
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// 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>
using namespace 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;
}
Last edited on
What exactly do you want to know? Comments are just there to show you what's going on.
closed account (yR9wb7Xj)
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.
closed account (yR9wb7Xj)
Okay, thanks I get it now.
Topic archived. No new replies allowed.