Code does not work completely

closed account (3796URfi)
sdf
Last edited on
First, use code tags, because now your code is nearly unreadable.
And secondly, state your problem precisely.
closed account (3796URfi)
sdf
Last edited on
I would do something like this:
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
#include <iostream> // cin, cout, getline
#include <string> // string

//Declaring using, so we don't have to use std:: prefix every time
using std::cin;
using std::cout;
using std::string;
using std::endl;

//Here main function starts
int main(int argc, char* argv[] )
{
    ///Data Initialization:
    //For user input
    int breadSlices, cheeseSlices, mayonnaiseGrams, picklePieces;
    //Declaring constant values of calories.
    //Note: constant values are usually named with capital letters
    const int BREADCALORIES = 63, CHEESECALORIES = 106, MAYONNAISECALORIES = 49, PICKLECALORIES = 25;
    //String for asking user's name
    string name;
    //And total calories count.
    int totalCalories;
    ///End of initialization

    ///Program:


    cout<< "Welcome to my Calorie Counter." << endl;
    cout<< "Please enter your name: ";
    //Take user input(name)
    getline(cin, name);
    //Now take number of ingredients
    cout<< endl << endl;
    cout<< "Hi " << name <<", please enter number of ingredient used on your sandwich." << endl << endl;
    cout<< "Please enter number of slices of bread used (0 if not used) :";
    cin>> breadSlices;
    cout<< "Please enter number of slices of cheese used (0 if not used) :";
    cin>> cheeseSlices;
    cout<< "Please enter number of mayonnaise in grams used (0 if not used): ";
    cin>> mayonnaiseGrams;
    cout<< "Please enter number of pieces of pickle used (0 if not used): ";
    cin>> picklePieces;
    cout<< endl;
    //Display calories
    cout<< "Bread Calories: " << BREADCALORIES * breadSlices << endl;
    cout<< "Cheese Calories:" << CHEESECALORIES * cheeseSlices << endl;
    cout<< "Mayonnaise Calories: " << MAYONNAISECALORIES * mayonnaiseGrams << endl;
    cout<< "Pickle Calories: " << PICKLECALORIES * picklePieces << endl;
    //Calculate total calories.
    totalCalories = (BREADCALORIES*breadSlices) + (CHEESECALORIES*cheeseSlices)
     + (MAYONNAISECALORIES*mayonnaiseGrams) + (PICKLECALORIES*picklePieces) ;

    cout<< endl << "Total Calories:" << totalCalories << endl;

    cout<< "There were " << totalCalories << " calories in my lunch yesterday." <<endl;
    cout<< "What is for lunch today? Funny!!!!" <<endl;

    return 0;
}


You may want to check it out:
http://www.holub.com/goodies/rules.html

Generally it's good to follow some rules when you are programming.
For example, when you create objects, name them so that you can read your code nearly as easily as it was written in English.

e.g.: "A" or "B" Doesn't say much. However, "breadSlices" is obvious way of telling us, what this data store.

Another thing is that when you program, you should choose data proper for your problem.

If your calories are integers(62.0, 52.0 or anything.0 are basically integers), and you will multiply them by integers(number of slices/grams/whatever), you will have an integer as a result. Therefore, no need to use float.

And when you declare data, it's good to declare it in one place.

And forget about using namespace std;. Use std:: or using std::(component) instead.

There are also some conventions about how to name variables, etc. but it doesn't matter that much for you(yet).

If you have any questions, feel free to ask.

Cheers!
Topic archived. No new replies allowed.