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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
#include <iostream>
#include<string>
#include <iomanip>
using namespace std;
string getName(ostream &output, iostream &input);
int getNumberCalories(ostream &output, iostream &input);
int calcCalorieAmount(int numofcalories, double percentofcalories);
int getGrams(double calorierange, int caloriespergram );
int printRange(ostream &output);
//my function
void printStuff(int cal, int lo, int hi);
int main()
{
/*Also if you make comments for what specific functions are suppose to do & what they are doing, it helps. */
/*
Defining the function after main is clean coding, and using the function prototype above main tells main "Hey main, when you run through the code inside you, you may encounter one or all of these functions.(Note everything takes place in the function main.) If you do encounter one of the functions listed above go to there definition below and apply what variables I (the programmer) has provided in main. (This can be user input, coder input, file input, and what ever else input is out there. )
*/
/*Right now the only thing that will happen in your program is the declaration of the 3 variables with a value of 0. If you want the functions to get called you would have them inside the function main(){ ... } */
int calories =5; /* notice I changed the values for you to see how this works */
int low = 10;
int high = 100;
printStuff(calories, low, high); /*Notice 3 things here no need for function type, no need for variable type, and the actual parameters are different from the formal parameters. If you want more help you need to read at a minimum: http://www.cplusplus.com/doc/tutorial/basic_io/ */
return 0;
}
//comments /* or comments */
// ^- notice the two different ways to comment,
//single line comment
/* Multi line
comment :: The reason behind the comments on how to comment is because without comments is a guessing game to the helper.*/
string getName(ostream &output, iostream &input)
{
string name;
/* To be clear here this means that your output/input will be coming from a file or files not a user. might want to check the syntax on this */
output << "Enter Name";
input >> name;
/* For user input and output: http://www.cplusplus.com/doc/tutorial/basic_io/ <- Read me from top to bottom */
return name; /* Note that return (name or what ever) simply means is accessible outside the scope of the function getName */
}
int getNumberCalories(ostream &output, iostream &input)//these parameters are for some one who is inputting a file.
{
int calories;
output <<"Enter number of calories you are planning to eat a day: ";
input >> calories;
return calories;
}
// 5000 35% => 1750
// 1237 29% => 358.73
// 2007 12% => 240.84
int calcCalorieAmount(int numofcalories, double percentofcalories){
double const caloriespercent = numofcalories*percentofcalories;
return caloriespercent;
}
// 600 calories, 5 calories per gram => 120 grams
// 750 calories, 10 calories per gram => 75 grams
// 1503 calories, 35 calories per gram => 43 grams
int getGrams(double calorierange, int caloriespergram )
{
const double caloriesbygram = calorierange/caloriespergram;
return caloriesbygram;
}
void printRange(ostream &output, string nutrient, double lowrange, double highrange, int gram){
output<<nutrient<< left<<setw(15);
output<< lowrange<< right<<setw(8);
output<<highrange<<right<<setw(8);
output<<gram<< right<<setw(8);
return ;
}
/*Here is an example of how to use a function. */
void /* void means it has no return, & print stuff is the function name */ printStuff(int cal, int lo, int hi){
/*notice the formal parameters(int cal, int lo, int hi), are not the same as the actual parameters, which you can see in main. */
cout<<"Hello World"<<endl;
cout<<"The calories the user put in are: "<< cal <<endl;
cout <<"The low value the user put in is: "<<lo << endl;
cout <<"The high value entered: "<<hi <<endl;
}
|