calculate the number of calories in a sandwich

Hi i'm doing an introductory course in c++ and having a problem with this assignment:
Write a program that computes the number of calories in a peanut butter and jelly sandwich (two pieces of bread, some amount of peanut butter, some amount of jelly or preserves).

Assume that peanut butter is 7 calories per gram. And let's use Sara Beth's All-Natural Preserves that are only 2 calories per gram. And take note: an ounce is 28 grams.

The program reads in: the number of calories in a slice of bread, and then reads in the amount peanut butter used and the amount of preserves used. These amounts are in ounces!

The program prints exactly one line of output as follows:

YOUR SANDWICH: nnn CALORIES

where "nnn" is the number of calories your program computed.

here is my code so far:

[#include <iostream>
using namespace std;

int main ( ){
double caloriesperslice;
double totalcalperslice = caloriesperslice*2;
double ounceofpb, ounceofjl;
double pb = ounceofpb *28.0;
double jelly = ounceofjl * 28.0;;
double totalcalpb = pb * 7;
double totalcaljl = jelly * 2;
cin>> caloriesperslice;
cin >> ounceofpb;
cin >>ounceofjl;
cout<<" YOUR SANDWICH: "<<totalcalperslice + totalcalpb + totalcaljl <<" CALORIES";
return 0;
}][/code]

i'm testing this program in codelab and it's telling me my output is wrong. Any help would be appreciated.

thanks
You have the right idea, but you're not expressing it properly.
Your code will be executed in the exact order you wrote it, such that
1
2
a=b+1;
b=a*2;
is not the same as
1
2
b=a*2;
a=b+1;

Likewise,
1
2
double totalcalperslice = caloriesperslice*2;
cin >>caloriesperslice;
Is not the same as
1
2
cin >>caloriesperslice;
double totalcalperslice = caloriesperslice*2;

At the moment that you're using caloriesperslice, ounceofpb, and ounceofjl, you still haven't filled them with the user's input, nor initialized them with any meaningful value, so they contain garbage.
Topic archived. No new replies allowed.