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.