while (some value) loops

This is a discussion on while (some value) loops within the C++ Programming forums, part of the General Programming Boards category; Howdy, I am messing with while loops and can not figure out how to get the following while statments to ...

  1. #1
    In The Light
    Join Date
    Oct 2001
    Posts
    598

    while (some value) loops

    Howdy,
    I am messing with while loops and can not figure out how to get the following while statments to work.

    ______Code_____

    //---------------------------------------------------------------------------
    /* Divide 2 numbers 1) Show the decimal value and
    2) Show fractional value with remainder*/


    #include <vcl.h>
    #include <iostream.h>
    #include <conio.h>
    #include <stdio.h>
    int a, b, c, d, e; //declare variables

    float divide (int c, int d); //function prototype
    int main(int argc, char* argv[])
    {
    divide (c, d); //call divide function
    getchar();
    return 0;
    }

    float divide (int c, int d)
    {
    double f, g;
    float h;
    int remainder;
    cout << "\nEnter a value for C: ";
    cin >> c;
    while (c<1) {
    cout << "must be more than 0!! \n";
    cout <<" Enter a value for C: ";
    cin >>c;

    }
    cout << "\nEnter a value for D: ";
    cin >>d;
    while (d<1) {
    cout << "must be more than 0!! \n";
    cout <<" Enter a value for D: ";
    cin >>d;
    }

    e=c/d;
    f =c; //change c to double???
    g =d; //change d to double???
    h=f/g; //get value as double to print as decimal

    cout <<"\nDecimal value: " << h<<"\n";
    if (e<1) {cout<< c<<"/"<<d<<" is less than 1: \n";
    return 0;}
    remainder =c%d;
    if (e<1 && remainder<1){cout<<"Rem is less than 1: \n";
    return 0;}
    else;
    cout <<c<<"/"<<d<<"="<<e<<" with a remainder of " <<remainder;

    return 0;
    }

    clearly when a decimal value is entered for variable c or d i get a infinite loop. my question is how can i test for values less than 0 for example .01 or .23 and get the result i'm looking for, "Tell the user to enter a larger value"?

    thanks for any help

    M.R.

  2. #2
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    It's because your c and d are integer variables. If you say d=0.23, it will be truncated and d will be zero. Change your counter variables you're using in loops to double.

    double d;
    while (d < 0) // or whatever
    Making error is human, but for messing things thoroughly it takes a computer

  3. #3
    In The Light
    Join Date
    Oct 2001
    Posts
    598
    Hello kitten,
    when i change the variables c and d to double the line:
    remainder =c%d; fails with an error "improper use of double"
    maybe i need to have another conversion of the input variable like double t=c and double s=d or something like that.

    M.R.

  4. #4
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    remainder =c%d; fails with an error "improper use of double"
    You can't calculate modulus from a floating point value, it has to be integer. Sorry, but I don't have time right now to familiarize with your problem... Maybe another day (if I remember)
    Making error is human, but for messing things thoroughly it takes a computer

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    Try using a cast operation to get the correct result, e.g,
    float c, d;
    int e;
    e = (int)c % (int)d;
    You might use the functions floor a/o ceiling to round the numbers off as well.

  6. #6
    Unregistered
    Guest
    here's an algorhythm to calculate the integer and decimal portion of a variable of type double/float. There is a function in math.h to do this for you if you can find it. I forget it's name however.

    double number;
    int whole;
    double decimal;

    cout << "enter a decimal number" << endl;
    cin >> number;
    whole = (int)number;
    decimal = number - (double)whole;

    This will need to be adjusted for the case where number is less than 1 and greater than 0, but I think you can figure out how to do that. If you wish to allow doubles of a negative value you may also need to make a little adjustment, but I'm not sure on that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple thread for loops
    By lehe in forum C++ Programming
    Replies: 12
    Last Post: 03-29-2009, 01:01 PM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. recoursion or loops?
    By Mecnels in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2002, 12:09 PM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  5. for loops - newbie q's
    By Narciss in forum C Programming
    Replies: 8
    Last Post: 09-26-2001, 03:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21