### Numbers to Strings

Working on a project for school that converts dollar amounts to written check amounts. I have most of the code done already but I obviously messed up somewhere in the string conversion I'm just not sure where and how. Just asking for some help and the modf function must be used as well as the two functions and arrays.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124`` ``````#include #include #include #include #include using namespace std; const string digitStr[] = {"zero","one","two","three","four","five","six","seven","eight","nine"}; const string teenStr[] = {"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"}; const string tenStr[] = {"ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}; const string suffix[] = {" ", "thousand", "million" , "billion"}; //--------------------------------------------------------------------- string writtenThreeDigits (int threeDig ) { // (ndx%10); // (ndx/10); //wholeNumber = 123 unsigned int ones, tens, hundreds; ones = threeDig % 10; tens = (threeDig / 10) % 10; hundreds = (threeDig / 10) %10; string hundred; if ( hundreds != 0 ) { hundred = hundred[hundreds] + " hundred "; } if ( tens == 1 ) { hundred = hundred + tenStr[tens]; } else { hundred = hundred + tenStr[tens] + digitStr[ones]; } return hundred; //return "error written three digits not implemented"; } //--------------------------------------------------------------------- string writtenNumber (int wholeNumber) { string totalAnswer = ""; int groupNum = 0; do{ int threeDig = wholeNumber % 1000; wholeNumber = wholeNumber / 1000; string threeDigit = writtenThreeDigits( threeDig ); totalAnswer = threeDigit + suffix[groupNum]+ " " + totalAnswer; groupNum++; }while( wholeNumber != 0 ); return totalAnswer; //return "error written number not implemented"; } int main() { double amount, wholeDollars, wholeCents; //string writtenNumber(); while ( !cin.eof() ) { amount = -1.0 ; while ( amount < 0 ) { cout << "Enter a dollar amount: "; cin >> amount; // break in middle if done if ( cin.eof() ) break; if ( cin.fail() ) { cout << "You entered an invalid dollar amount!\n" ; cin.clear(); cin.ignore(1024, '\n'); amount = -1.0; } wholeCents = modf( amount, &wholeDollars ); cout<< wholeDollars << " dollars " << "and " << wholeCents << " cents" << endl; } string writtenDollars = writtenNumber ( wholeDollars ); string writtenCents = writtenNumber ( wholeCents ); cout << fixed << setprecision(2)<< endl; cout << "You entered: " << amount << endl; cout << "\$" << setw(14) << setfill('*') << amount << endl ; cout << wholeDollars << " " << wholeCents << endl; } }``````

Last edited on
please use code tags, they are the <> symbol under format. also, so you want to convert strings to numbers?
sorry about that, fixed it. I'm trying to get the output of the string to display correctly, somewhere its buggy and returns the wrong output.
Topic archived. No new replies allowed.