Program always outputs the same, weird number

This is my very first attempt at C++, and I must be missing something big. This is an assignment for my CS class - you have to write a simple store checkout program. I thought everything looked fine and dandy, but every time I run the program, my checkout total is 5.45e159, no matter what weights I input. Where did I screw up?
-----------------------

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

/*
*
*/
double checkout(double produce[]);

int main(int argc, char** argv) {

double input;
double produce[5];
double grandproduce[5];
double total = 0, grandtotal, grosstotal, discount, granddiscount;
int again = 1;

cout << "***** Welcome to Super Fruit/Vegetable Purchaser II Turbo! *****" ;

while (again == 1) {
cout << "\n\nPlease enter the weight (in pounds) for each of the" <<
"\nfollowing purchases.\n";

cout << "Bananas: ";
cin >> produce[1];
grandproduce[1] += input;

cout << "Apples: ";
cin >> produce[2];
grandproduce[2] += input;

cout << "Cucumbers: ";
cin >> produce[3];
grandproduce[3] += input;

cout << "Carrots: ";
cin >> produce[4];
grandproduce[4] += input;

cout << "Oranges: ";
cin >> produce[5];
grandproduce[5] += input;

total = checkout(produce);

grosstotal += total;

if (total > 50.0) {
discount = total * 0.05;
granddiscount += discount;
total -= discount;
}
grandtotal += total;

cout << "Your total comes to $" << total << "." ;
cout << "\nWould you like to do another checkout? Enter 1 for yes or" <<
"\nany other number for no: ";
cin >> again;

}
cout << "*** Total weights ***\n";
cout << "Bananas: " << grandproduce[1] << "\n";
cout << "Apples: " << grandproduce[2] << "\n";
cout << "Cucumbers: " << grandproduce[3] << "\n";
cout << "Carrots: " << grandproduce[4] << "\n";
cout << "Oranges: " << grandproduce[5] << "\n";
cout << "Gross purchases: " << grosstotal << "\n";
cout << "Total discounts: " << granddiscount << "\n";
cout << "Total purchases (discounted): " << grandtotal << "\n";
return 0;

}

double checkout(double produce[]) {

double total;

total += produce[1] * 0.44;
total += produce[2] * 0.99;
total += produce[3] * 1.19;
total += produce[4] * 0.89;
total += produce[5] * 0.79;

return total;
}

In the function checkout, your total starts at some random value. Set it to zero on creation.

grandproduce[1] += input;
What is this supposed to do? input is some random value you never set.

grosstotal begins with some random value.

There is a theme here. You are creating many variables with random values, and then you start using them without ever setting the value.
Last edited on
This doesn't do what you think it does.

1
2
3
cout << "Bananas: ";
cin >> produce[1];
grandproduce[1] += input;


input is uninitialised (always initialise variables to something even zero) and is the cause of the problems with the output.

You probably wanted to do this:
1
2
3
cout << "Bananas: ";
cin >> produce[1];
grandproduce[1] +=  produce[1];


As this will put the input into your array.
Thank you very much - I realized my stupid mistakes and fixed them. I will be sure to initialize variables from now on. Java spoiled me. :P
(always initialise variables to something even zero)


As an aside, C and C++ very explicitly and deliberately by design allows the creation of uninitialised variables. There are good cases when initialising them (for example, to zero) is not wanted.
@doctorkwack101

I forgot to mention, please use code tags in future - the <> button on the right.

@Moschops

OK, I see what you are saying. Just wondering whether to initialise to something is better than not initialising at all.

I was thinking that uninitialised variables is one of the leading reasons for mistakes in code, and especially for a newbie, it is a good idea to initialise.

It is of course different in C++, where initialisation should be done in constructors, but I see a lot of newbie's writing C code with some couts & cin.
Topic archived. No new replies allowed.