Is my class bypassing my do/while loop?
Nov 12, 2013 at 12:17am UTC
My program for homework is nearly completed. However, my input validation which is inside the class member getData (rational.getData) is not breaking when its supposed too. For example if user inputs 2/0, it should read "DIVISION BY ZERO ERRROR! " and then break, starting the do while loop over for another input. In previous programs (this is my first one utilizing class/struct) this format has worked fine, but i wonder if the way the class is set up, that the compiler will accept any data, despite the boolean foundFlag signaling true. Also, if you look at my "Check" function which puts out the "reduced form", if Standard Input goes (0/2) it should just print out "0", but its output will do the "O" and also "0/2".
I hope this information can be addressed. I personally just started programming at the end of August, with no prior experience, and I was just looking at my previous posts, and I am dumbfounded at the progression I have made. To all people on here who help noobs like myself. It is greatly appreciated. Thank You.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
#include<iostream>
#include<iomanip>
using namespace std;
class rational
{
private :
int numerator, denominator;
public :
void reduce2();
rational(); //constructor
void getData();
void printToConsole();
rational operator * (const rational &c2);
rational operator / (const rational &c2);
rational operator + (const rational &c2);
rational operator - (const rational &c2);
bool foundFlag;
void Check();
double Evaluate();
};
rational::rational() // rational a
{
}
void rational:: Check()
{
cout << "\nJust Checking...Reduced Form Is:" ;
reduce2();
if (numerator==0&&denominator!=0)
{
cout<< " == 0" <<endl;
}
if ((numerator==denominator)&&numerator!=0)
{
cout << " == 1" <<endl;
}
if ((denominator == 1)&& numerator!=0)
{
cout<< " == " << numerator<<endl;
}
else
cout<< " == " << numerator<<"/" <<denominator<<endl;
}
rational rational::operator +(const rational &c2)
{
rational temp;
fflush(stdin);
temp.denominator=rational::denominator*c2.denominator;
temp.numerator= (rational::numerator*c2.denominator)+(c2.numerator*rational::denominator);
cout<< numerator<<"/" <<denominator<< " + " << c2.numerator<<"/" <<c2.denominator<<" == " ;
cout<< temp.numerator<< "/" << temp.denominator;
return temp;
}
rational rational:: operator -(const rational &c2)
{
fflush(stdin);
rational temp;
temp.denominator=rational::denominator*c2.denominator;
temp.numerator= (rational::numerator*c2.denominator)-(c2.numerator*rational::denominator);
cout<< numerator<<"/" <<denominator<< " - " << c2.numerator<<"/" <<c2.denominator<<" == " ;
cout<< temp.numerator<< "/" << temp.denominator;
return temp;
}
double rational::Evaluate()
{
fflush(stdin);
double n = numerator;
double d = denominator;
cout<<" == : " <<fixed<<setprecision(2)<< n/d<<endl;
return (n/d);
}
rational rational :: operator *(const rational &c2)
{
fflush(stdin);
rational temp;
temp.numerator=numerator*c2.numerator;
temp.denominator=denominator*c2.denominator;
cout<< numerator<<"/" <<denominator<< " * " << c2.numerator<<"/" <<c2.denominator<<" == " ;
cout<< temp.numerator<< "/" << temp.denominator;
return temp;
}
rational rational :: operator /(const rational &c2)
{
fflush(stdin);
rational temp;
temp.numerator= numerator*c2.denominator;
temp.denominator=denominator*c2.numerator;
cout<< numerator<<"/" <<denominator<< " / " << c2.numerator<<"/" <<c2.denominator<<" == " ;
cout<< temp.numerator<< "/" << temp.denominator;
return temp;
}
void rational:: reduce2()
{
int tmpNum, tmpDen, n;
if (denominator < 1)
{
numerator = numerator * -1;
denominator = denominator * -1;
}//end if
//reduce to lowest terms
//need abs to find gcd
tmpNum = abs(numerator); //temporary numerator becomes absoluted.
tmpDen = abs(denominator);
if (tmpDen > tmpNum){
n = tmpDen;
tmpDen = tmpNum;
tmpNum = n;
}//endif
for (int i = tmpDen; i > 0;i--)
{
if ((tmpDen % i == 0) && (tmpNum % i == 0))
{
numerator = numerator/i;
denominator = denominator/i;
break ;
}//endif
}//endfor
fflush(stdin);
}
void rational::getData()
{
do
{
char n[20], num[20], den[20];
bool foundFlag; // signal invalid entry
int countDot=0; // count how many colon entered so far
unsigned len; // length of input value
int x = 0;
fflush(stdin);
cout<<"\n Please enter a fraction in (a/b) format: " ;
countDot = 0; // there was no dot found at first
foundFlag = false ; // there was nothing wrong at first
fflush(stdin);
cin.getline(n, 20); // read in a value entered from keyboard
len = strlen(n);
if (len == 0)
{
cout << "Oops! No value was entered!\n" ;
foundFlag = true ; // cannot have empty input
break ;
}
else
{
int x = 0;
for (unsigned i = 0; i<len; i++) //unsigned is positive integer
{
if ( n[i] == ' ' ) // space was not allowed - like 1 :56
{
cout << "\nspace was not allowed.\n" ;
foundFlag = true ;
fflush(stdin);
break ;
}
if (n[i]=='/' )
{
x=i;
}
}
for (int i=0; i<x; i++)
{
if ((n[i]<'0' || n[i]>'9' ) && (n[0]!='-' ))
{
cout<< "Invalid:\n " ;
foundFlag=true ;
fflush(stdin);
break ;
}
int j=0;
for (int i=0; i<x; i++)
{
num[j]=n[i];
j++;
}
numerator=atoi(num);
}
fflush(stdin);
int j=0;
for (int i=x+1; i<len; i++)
{
if ((n[i]<48||n[i]>57)&& n[x+1]!='-' )
{
cout<< "Invalid: Not numbers.\n" ;
foundFlag=true ;
fflush(stdin);
break ;
}
else
{
den[j]=n[i];
j++;
}
denominator=atoi(den);
}
if (denominator == 0)
{
cout<< "\nERROR DIVISION BY ZERO! TRY AGAIN!" ;
foundFlag=true ;
break ;
fflush(stdin);
}
}
}while (foundFlag==true );
}
int main()
{
char again;
do
{
rational c1, c2;
c1.getData();
c1.reduce2();
c1.Check();
fflush(stdin);
c2.getData();
c2.reduce2();
c2.Check();
fflush(stdin);
(c1 + c2).Evaluate();
(c1 - c2).Evaluate();
(c1 * c2).Evaluate();
(c1 / c2).Evaluate();
cout << "\n\nRun again (y/n)? " ;
fflush(stdin);
cin >> again;
while (toupper(again) != 'Y' && toupper(again) != 'N' )
{
fflush(stdin);
cout << "Please enter \'y\' for Yes, or \'n\' for No: " ;
cin >> again;
}
fflush(stdin);
if (again=='y' ||again=='Y' )
{
system("cls" );
}
}while (again=='y' ||again=='Y' );
cout << "\nProgrammer:Andrew HOlder I FREAKING HATE PROGRAMMING!" ;
cout << "Good bye! Press <Enter> key to end the program... " ;
fflush(stdin);
cin.get();
return 0;
}
Topic archived. No new replies allowed.