>>Can someone tell me what I'm doing wrong
1st of all, the BIGGEST mistake is
if (len = 4 && !ispunct(inputPrice[1]))
as this *assigns* '4' to 'len' instead of a comparison. The easiest validation method would be
#include <stdlib.h>
//...
while ( 1) {
cin.getline(inputPrice, 10);
int len = strlen(inputPrice);
if (len > 5) {
cout << "Price cannot be exceed $99.99" << endl << endl;
continue; // next try
}
char* pc = NULL;
if ( '$' != inputPrice[0]) {
cout << "Price must start with '$'" << endl << endl;
continue; // next try
}
double dPrice = strtod ( &inputPrice[1], &pc); // start conversion to 'double' after the '$' marker
if ( 0 != *pc) { // conversion stopped at sth. else but the terminating zero
cout << "Price has invalid format" << endl << endl;
continue; // next try
}
theDVD.Price = (float) dPrice;
break; // we got a valid price, stop loop
}
Main Topics
Browse All Topics





by: meow00Posted on 2003-12-09 at 13:17:28ID: 9907590
Hello .... would u mind posting the whole code ... eg. How u define theDVD class ....
hard to debug and compile to check if the result works like this .... thanks !