# while (some value) loops

• 11-13-2001
itld
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.
• 11-14-2001
kitten
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
• 11-14-2001
itld
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.
• 11-14-2001
kitten
Quote:

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) :D
• 11-14-2001
Brown Drake
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.
• 11-14-2001
Unregistered
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.