taking out the pointless formatting would be a good start.
don't don't use upper case for your variables.
split stuff into their own methods as well (e.g. "CalcBreadCalories(number)").
edit: in fact you could use an enum to store the food types.
EDIT 2: or use a look up map (e.g. std::map<std::string, double>
The format is supposed to be like that but i couldnt paste it here to look like that
Other than that, what should i use so the output could come out with decimals? Float?
And are my formulas right like the BREAD *A and how could i make it work because it doesnt run properly is there any advice you can gove me to make it work or are there any errors in my last code?
can you give me an example that relates to my code?
because I'm getting confused on what to do and where to place them.
is my code format correct also? or does it need fixing?
// Omiting most of your formatting
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std;
// constants above main and a little more discriptive
floatconst BREAD_CALORIES = 63.00;
floatconst CHEESE_CALORIES = 106.00;
floatconst MAYONNAISE_CALORIES = 49.00;
floatconst PICKLE_CALORIES = 25.00;
int main(int argc, constchar * argv[])
{
string name;
double total_calories = 0.0;
// stick to one style of formating for new lines
// use \n or endl not both where possible just for
// the sake of consistency
cout << "Welcome to my calorie counter" << endl;
cout << "Please enter your name: ";
// reading until new line
getline(cin, name, '\n');
// the rest is really practice for me (as I am a student of c++ also)
// but here is my take.
//
int burger[4];
// Using enumeration for array values.
enum ingredients_t {
bread_slices = 0,
cheese_slices,
mayo_grams,
pickle_pieces
};
cout << "Hello " << name << endl;
cout << "Please enter number of slices of bread used (0 if not used) : ";
cin >> burger[bread_slices];
cout << endl;
cout << "Please enter number of slices of cheese used (0 if not used) : ";
cin >> burger[cheese_slices];
cout << endl;
cout << "Please enter number of mayonnaise in grams used (0 if not used): ";
cin >> burger[mayo_grams];
cout << endl;
cout << "Please enter number of pieces of pickle used (0 if not used): ";
cin >> burger[pickle_pieces];
cout << endl;
// Adding a little formating here.
cout << fixed << setprecision(2);
cout << setfill('.') << left << setw(35) << "Bread Calories :" << right << " " << burger[bread_slices]*BREAD_CALORIES << endl;
cout << setfill('.') << left << setw(35) << "Cheese Calories :" << right << " " << burger[cheese_slices]*CHEESE_CALORIES << endl;
cout << setfill('.') << left << setw(35) << "Bread Calories :" << right << " " << burger[mayo_grams]*MAYONNAISE_CALORIES << endl;
cout << setfill('.') << left << setw(35) << "Bread Calories :" << right << " " << burger[pickle_pieces]*PICKLE_CALORIES << endl;
cout << endl << endl;
total_calories += burger[bread_slices]*BREAD_CALORIES;
total_calories += burger[cheese_slices]*CHEESE_CALORIES;
total_calories += burger[mayo_grams]*MAYONNAISE_CALORIES;
total_calories += burger[pickle_pieces]*PICKLE_CALORIES;
cout << setfill('.') << left << setw(35) << "Total Calories :" << right << " " << total_calories << endl;
return 0;
}
oh i got it now! thank you!
just one more question how do i place
cout << "Welcome to my calorie counter" << endl;
in the center of the page?
sorry im a beginner I'm starting to get these slowly
That is a good question. I am not sure. I am just starting as well. I think the issue with centering the text, is that cout (common output) is printed on a variety of screens, and there is not constant value for what the center is. But I am just taking a guess here.