Need for help again!!

Discussion in 'C++' started by bryant058, Nov 19, 2006.

  1. bryant058

    bryant058 Guest

    i post an article for help before and people help me(thx).And i try
    another way to modify my source code and succedd, but i don't realize
    the logic of computer = =!Can't any body explain my new source?
    my ex-source code didn't contain line 7(largest=number).As a
    result,when i type every number=0,the
    largest is 4.
    -----<my new source code>----------------
    using namespace std;
    int main()
    int counter=0;
    int largest,number;
    cout<<"Enter a Number:";
    if (number>largest)


    cout<<"The largest is "<<largest<<endl;

    return 0;
    bryant058, Nov 19, 2006
    1. Advertisements

  2. bryant058

    John Carson Guest

    number is not initialised, so initialising largest with it is as bad as not
    initialising number.

    What you need to do is initialise number with 0 (assuming that the numbers
    you type in are non-negative). Do this:

    int largest = 0, number;
    The two lines above do nothing useful and should be omitted.
    John Carson, Nov 19, 2006
    1. Advertisements

  3. bryant058

    John Carson Guest

    I meant to say:so initialising largest with it is as bad as not initialising
    John Carson, Nov 19, 2006
  4. bryant058

    Jim Langston Guest

    number is not initialized. I can contain anything. 0, 12, 234987687,
    anything. Better to make this line:
    int largest = 0, number = 0;
    You don't have to initialize number, but if they put in bogus data (x) for
    the cin it remains unchanged.
    This is doing nothing. No reason to have it. A smart compiler will
    optimize this away too.
    ++counter; does the same thing and is easier to follow (once you're used to
    You could also have written this as:
    counter += 1;
    That being said, if you are doing this 10 times, why not just make it a for

    for ( counter = 1; counter <= 10; ++counter )
    Jim Langston, Nov 20, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.