View Single Post
  #1 (permalink)  
Old February 1st, 2007, 04:38 PM
proslambano proslambano is offline
Authorized User
Points: 174, Level: 3
Points: 174, Level: 3 Points: 174, Level: 3 Points: 174, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2006
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default weird problem with Ch 9 example

in the section on using for loops with a continue statement, pgs 139-140, I get a different result for the variable "product" depending on whether I compile it and run it from a command prompt, or whether I press ctrl+F5 in visual studio (running without debugging). This happens when I continually enter the number 0 for the cin value variable. (ten 0's)

running at the command prompt, the value for "product" at the end is 1, as it is initialized in the program

With ctrl-F5, it is 0.

Anyone know what this is? Not that it's important in the big scheme of things, but I'd like to know why there is an inconsistency.

thanks in advance

and here's the code for cut and paste. Also, you might try uncommenting my comments lines, building, and then running from the DOS prompt - for some reasons, the lines, when uncommented, make the value change too.

Code:
// for loop
#include <iostream>
#include <iomanip>

using std::endl;
using std::cout;
using std::cin;
using std::setw;

int main()
    {
        int i = 0, value = 0, product=1;
        for (i=1; i<=10; i++)
            {
                //cout << "product now equals " << product << endl;
                //cout << "value now equals " << value << endl;
                //cout << "product * value now equals " << (product *= value) << endl;
                cout << "Enter an integer: ";
                cin >> value;

                if (value == 0)
                    continue;

                product *= value;
            }
        cout << "Product (ignoring zeros): " << product << endl;

        return 0;
    }
Sincerely,
Brian
__________________
Sincerely,
Brian