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?
-----------------------
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;
(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.
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.