Can anyone help figure out whats wrong with this C++ program?

derpmaster

Honorable
Jan 14, 2013
9
0
10,510
0
#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
double cup, gallon, liter, pint, quart, ounces, milliliter, teaspoon, tablespoon ;
string horizontalLine (50, '-') ;

cout << "HELLO RACHEL RAY! \n" << endl ;
cout << "Enter the Number of Cups you wish to convert! \n" << endl ;
cout << "CUPS:" << endl ;
cin >> cup ;

cup = 8 * ounces ;
gallon = 16 * cup ;
liter = 4.25 * cup ;
cup = 236.59 * milliliter ;
pint = 2 * cup ;
quart = 4 * cup ;
ounces = cup / 8 ;
milliliter = cup / 236.59 ;
teaspoon = cup / 48 ;
tablespoon = cup / 16 ;

cout << setw(10) << fixed << "CUPS" << "CONVERTS TO" << endl ;
cout << horizontalLine << endl ;

cout << cup << ounces << "FLUID OUNCES" << endl ;
cout << cup << gallon << "GALLONS" << endl ;
cout << cup << liter << "LITERS" << endl ;
cout << cup << milliliter << "MILLILITERS" << endl ;
cout << cup << pint << "PINTS" << endl ;
cout << cup << quart << "QUARTS" << endl ;
cout << cup << tablespoon << "TABLESPOONS" << endl ;
cout << cup << teaspoon << "TEASPOONS" << endl ;

cout << horizontalLine << endl ;

cout << "HAPPY COOKING!" << endl ;

return 0 ;
}
 

Pinhedd

Distinguished
Moderator
We have a "no homework questions" policy, but I can't resist a good programming tutorial.

The first mistake that you made is that you declared a whole bunch of local variables (enclosed within a code block {}) without initializing them. You've given them a type, which tells the program how to interpret the values stored at those memory locations, but you've not told it what the values at those locations are. Variables within code blocks can be managed either automatically on the program stack (which is created by the operating system when the program is run), or statically as part of the program binary (which is created by the compiler when the program is compiled). By default, variables in code blocks are managed automatically. The explicit way to do this is to preface them with the 'auto' qualifier, but this is implied so you will almost never see it used as it's redundant. Static variables use the 'static' qualifier instead (which is not implicit) are guaranteed to be initialized to zero by the compiler at compile time and will persist between function calls. Variables declared outside of code blocks (commonly called global variables) are implicitly static, but there are some minor restrictions on this that you don't need to bother with until you start compiling multi-file projects.

There is no guarantee that automatic variables are initialized to anything, they will simply adopt the value at the memory location at which they are placed in the stack frame. If you run the same program multiple times and obtain non-deterministic output there's a good chance that you have an uninitialized variable somewhere in your code. Your compiler should warn you about this. However, if you start doing dynamic memory management down the line, you may have a harder time hunting them down. Get into the practice of always assigning default values to your variables when you declare them.

The second mistake that you made is that you read a value from the standard input into the variable 'cups' and then immediately overwrite it with the value contained in 'ounces' multiplied by 8. However, as I just established above, 'ounces' is not initialized to a deterministic value. Thus, 'cups' may take a different value every time the program is run.

The correct way to do this would be to assign to 'ounces' the value in 'cups' multiplied by 8 because there are 8 ounces in a cup.

`ounces = cups * 8`

If you read in 'ounces' instead and wish to convert this to cups, you would want to assign to cups the value in ounces divided by 8.

Next, you wish to convert the volume in cups to the volume in gallons. We know the volume in cups, we do not know the value in gallons. The variable which we want to update goes on the left hand side of an assignment statement, this is also known as the lvalue (you may see this in debug statements). So, the correct way would be

'gallons = cups / 16'

In machine language this statement would read "take the double precision floating point value located at the symbol cups, divide it by 16, and store the result at the symbol gallons". The variable on the left is assigned the result of the evaluation of the expression on the right.

I hope that this helps a bit.
 

ex_bubblehead

Distinguished
Moderator
You defined: "double cup, gallon, liter, pint, quart, ounces, milliliter, teaspoon, tablespoon"
but you didn't initialize any of them.

Since this resembles a homework assignment I'll refrain from further comment.
 

derpmaster

Honorable
Jan 14, 2013
9
0
10,510
0
What do you mean by initializing, its not homework its a lab that I've already turned in. Id prefer to know where i went wrong.
 

derpmaster

Honorable
Jan 14, 2013
9
0
10,510
0


Wait isn't "double" the initializer? Defining them as the names of the values? or do i need to give them all a value that can be updated... kinda like this

int diameter = 0.0,
slices = 0.0,
area = 0.0,
oneSlice = 14.125;
 

derpmaster

Honorable
Jan 14, 2013
9
0
10,510
0
K I got it working now i just need to figure out the math, how do you save X is equal to a number of Y? Is there a differnt symbol for that stuff?
 

Zenthar

Distinguished
Dec 31, 2007
250
0
18,960
9

One thing I notice is the following:
Code:
cin >> cup ;

cup = 8 * ounces ;
You read "cup" but then overwrite it right after with the value of "8 * onces", the later possibly having a value of 0 since it was never assigned any value.

 

Pinhedd

Distinguished
Moderator
We have a "no homework questions" policy, but I can't resist a good programming tutorial.

The first mistake that you made is that you declared a whole bunch of local variables (enclosed within a code block {}) without initializing them. You've given them a type, which tells the program how to interpret the values stored at those memory locations, but you've not told it what the values at those locations are. Variables within code blocks can be managed either automatically on the program stack (which is created by the operating system when the program is run), or statically as part of the program binary (which is created by the compiler when the program is compiled). By default, variables in code blocks are managed automatically. The explicit way to do this is to preface them with the 'auto' qualifier, but this is implied so you will almost never see it used as it's redundant. Static variables use the 'static' qualifier instead (which is not implicit) are guaranteed to be initialized to zero by the compiler at compile time and will persist between function calls. Variables declared outside of code blocks (commonly called global variables) are implicitly static, but there are some minor restrictions on this that you don't need to bother with until you start compiling multi-file projects.

There is no guarantee that automatic variables are initialized to anything, they will simply adopt the value at the memory location at which they are placed in the stack frame. If you run the same program multiple times and obtain non-deterministic output there's a good chance that you have an uninitialized variable somewhere in your code. Your compiler should warn you about this. However, if you start doing dynamic memory management down the line, you may have a harder time hunting them down. Get into the practice of always assigning default values to your variables when you declare them.

The second mistake that you made is that you read a value from the standard input into the variable 'cups' and then immediately overwrite it with the value contained in 'ounces' multiplied by 8. However, as I just established above, 'ounces' is not initialized to a deterministic value. Thus, 'cups' may take a different value every time the program is run.

The correct way to do this would be to assign to 'ounces' the value in 'cups' multiplied by 8 because there are 8 ounces in a cup.

`ounces = cups * 8`

If you read in 'ounces' instead and wish to convert this to cups, you would want to assign to cups the value in ounces divided by 8.

Next, you wish to convert the volume in cups to the volume in gallons. We know the volume in cups, we do not know the value in gallons. The variable which we want to update goes on the left hand side of an assignment statement, this is also known as the lvalue (you may see this in debug statements). So, the correct way would be

'gallons = cups / 16'

In machine language this statement would read "take the double precision floating point value located at the symbol cups, divide it by 16, and store the result at the symbol gallons". The variable on the left is assigned the result of the evaluation of the expression on the right.

I hope that this helps a bit.
 
Thread starter Similar threads Forum Replies Date
E Apps General Discussion 0
SomeAccount5 Apps General Discussion 3
N Apps General Discussion 1
Z Apps General Discussion 0
E Apps General Discussion 1
A Apps General Discussion 2
K Apps General Discussion 2
T Apps General Discussion 0
Neon The (Anthro) Umbreon Apps General Discussion 3
Stephsniz Apps General Discussion 5
D Apps General Discussion 0
S Apps General Discussion 1
D Apps General Discussion 2
jsimenhoff Apps General Discussion 2
S Apps General Discussion 1
S Apps General Discussion 2
T Apps General Discussion 1
D Apps General Discussion 3
F Apps General Discussion 1
S Apps General Discussion 1

ASK THE COMMUNITY