how come no one here uses for loops?

Discussion in 'C++' started by davy.zou@brentwood.bc.ca, Mar 8, 2007.

  1. Guest

    #include <iostream.h>
    #include <math.h>

    void centscalculation(int cents, int &count2, int &count7, int
    &count9) {
    int Total, diff, mindiff=999999, x, y, z, Nstamp, NewNstamp=999999;
    //x is for 9cents, y is for 7cents, z is for 2cents;

    for(x=cents/9; x>=0; x--) {

    for(y=(cents-x*9)/7; y>=0; y--) {
    z=(cents-x*9-y*7)/2;

    Total=(z*2+y*7+x*9);

    if (Total>=cents) {
    } else {
    Total=Total+2;
    z=z+1;
    }

    diff=Total-cents;

    if (diff<mindiff) {
    mindiff=diff;
    count2=z;
    count7=y;
    count9=x;
    } else if (diff==mindiff) {
    Nstamp=x+y+z;
    NewNstamp=count2+count7+count9;
    if (Nstamp<NewNstamp) {

    count2=z;
    count7=y;
    count9=x;
    }
    }
    }
    }
    }


    void main () {

    int cents;

    int count2, count7, count9;
    while (true) {

    cout<<"Enter cents, 0 to terminate: "<<endl;
    cin>>cents;

    if (cents<0) {
    cout<<"Error."<<endl;
    continue;
    }

    if (cents==0) {
    break;
    }

    centscalculation(cents, count2, count7, count9);
    cout<<"answer is "<<count2<<" 2 cents stamps "<<count7<<" 7 cents
    stamps "<<count9<<" 9 cents stamps."<<endl;
    cout<<"The total number of stamps is "<<count2+count7+count9<<endl;
    }
    }
    , Mar 8, 2007
    #1
    1. Advertising

  2. Ron Natalie Guest

    wrote:

    We use for loops extensively, what are you talking about.

    > #include <iostream.h>


    No such header in standard C++.


    > int Total, diff, mindiff=999999, x, y, z, Nstamp, NewNstamp=999999;
    > //x is for 9cents, y is for 7cents, z is for 2cents;
    >

    This isn't even good C coding style. It's retched C++. Declare in
    the smallest scope they are needed and provide initializers.

    > for(x=cents/9; x>=0; x--) {
    >
    > for(y=(cents-x*9)/7; y>=0; y--) {


    A commment explaining what the hell it is you're attempting to do here
    would be nice. Is y really supposed to count to zero here or just down
    to cents - (x-1)*9?

    What is your question anyhow?

    >
    >
    > void main () {


    The above is illegal in C and C++.

    >
    > int cents;
    >
    > int count2, count7, count9;
    > while (true) {
    >
    > cout<<"Enter cents, 0 to terminate: "<<endl;
    > cin>>cents;


    You should test to see if this fails.

    >
    Ron Natalie, Mar 8, 2007
    #2
    1. Advertising

  3. Lionel B Guest

    On Thu, 08 Mar 2007 06:53:38 -0800, davy.zou wrote:

    Hey! I use for loops!

    [snip some code with for loops]

    Wow! so do you!

    (did I miss the point?)

    --
    Lionel B
    Lionel B, Mar 8, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Spendius
    Replies:
    1
    Views:
    732
    Chris Uppal
    Apr 21, 2004
  2. Angela K
    Replies:
    7
    Views:
    378
    Mad Hamish
    Oct 26, 2003
  3. eric bin
    Replies:
    1
    Views:
    287
    Morris Dovey
    Nov 5, 2003
  4. Kobu
    Replies:
    25
    Views:
    723
    Mike Wahler
    Jan 23, 2005
  5. Me
    Replies:
    2
    Views:
    237
Loading...

Share This Page