I've converted char *itemPrice to a float, but I can't seem to get the length of the char *itemPrice before I convert it in order to do some error checking to make sure all characters are numbers.
Here's the code
I can't get the length of itemPrice1 in order to do the error checking.Code:int PriceItemLength; cout << endl << " You entered option 3" << endl << endl; system("pause"); system("cls"); cout << endl << " Please enter a price: $"; cin >> itemPrice1; PriceItemLength = itemPrice1.length(); // having problem here while (index < PriceItemLength) { if (itemPrice1[index] == '9' || itemPrice1[index] == '8' || itemPrice1[index] == '7' || itemPrice1[index] == '6' || itemPrice1[index] == '5' || itemPrice1[index] == '4' || itemPrice1[index] == '3' || itemPrice1[index] == '2' || itemPrice1[index] == '1' || itemPrice1[index] == '0' || itemPrice1[index] == '.') index++; else { system("cls"); cout << endl << " Please enter a price only!"; cout << endl << " Please enter a price: $"; cin >> itemPrice1; index = 0; } } // ends while loop double x; x = atof( itemPrice1 ); cout << setprecision(2) << x;
bob2509



LinkBack URL
About LinkBacks


