/

closed account (3796URfi)
sdsd
Last edited on
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>
Last edited on
closed account (3796URfi)
@mutexe

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?
Cuz you're reading in strings. You need to parse the input and convert to numbers. If of course the input can be converted to a number.
closed account (3796URfi)
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?

@mutexe
closed account (3796URfi)
and how do i get the output to print with decimals like ex. Bread calories to become 163.00
Hopefully you still do your own work but this is my take on your problem .

See line 62 for decimal.

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
// Omiting most of your formatting


#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

// constants above main and a little more discriptive 
float const BREAD_CALORIES  = 63.00;
float const CHEESE_CALORIES = 106.00;
float const MAYONNAISE_CALORIES = 49.00;
float const PICKLE_CALORIES = 25.00;

int main(int argc, const char * 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;

}
Last edited on
hmmm @rephlex it doesn't allow for decimal inputs so whats the point of setprescision(2) ....
when your doing arithmetic by float or double in c++ with ints. the value is turned into a decimal number.
closed account (3796URfi)
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.
Topic archived. No new replies allowed.