c++ - Currency denomination -


trying use proper currency denomination game. currency stored string (i.e. can't change due professor) , in order of platinum, gold, silver , copper. example, if initialize currency "0.1.23.15" means have 0 platinum, 1 gold, 23 silver , 15 copper.

i need able covert higher denomination, however. mean? example if have 105 silver pieces (i.e. 0.0.105.0), should show 1 gold , 5 silver (i.e. 0.1.5.0).

i bolded problem within setcost method. checking if number greater 100 , if -- make column 0, go previous element , add 1 ascii value giving proper carry. unfortunately, debugger showing "/x4" being dumped element instead of "4". know why , how can change it??

edit: edited code , works long don't enter number above 100. having brain lapse on how make work numbers larger 100.

this of sloppiest code have ever written. please gentle. :(

void potion::setcost(std::string cost) {     char buffer[256];     std::string currencybuffer [4];     int integerbuffer[4];     int * integerpointer = nullptr;     int temp = 0;     int = 0;     char * tokenptr;     //convert string cstring     strcpy(buffer, cost.c_str() );      //tokenize cstring     tokenptr = strtok(buffer, ".");      while(tokenptr != nullptr)     {         //convert ascii integer         temp = atoi(tokenptr);          //store temp currency buffer         integerbuffer[i] = temp;          //make pointer point integer buffer         integerpointer = &integerbuffer[i];          if(*integerpointer < 100)             currencybuffer[i] = tokenptr;         else         {             //store 0 in column if number              //greater 100             temp2 = temp % 100;             itoa(temp2, temp3, 10);             currencybuffer[i] = temp3;              //go , add 1 currency buffer             temp = atoi(currencybuffer[i-1].c_str());             temp += 1;             itoa(temp, temp3, 10);             currencybuffer[i - 1] = temp3;         }          i++;          //get next token         tokenptr = strtok(nullptr, ".");     }     newline();      std::string tempbuffer;      //store entire worth of potions     tempbuffer = "platinum: ";     tempbuffer += currencybuffer[0];     tempbuffer += "\ngold: ";     tempbuffer += currencybuffer[1];     tempbuffer += "\nsilver: ";     tempbuffer += currencybuffer[2];     tempbuffer += "\ncopper: ";     tempbuffer += currencybuffer[3];      mcost = tempbuffer; } 

i think problem in line:

currencybuffer[i - 1] = temp; 

you're assigning int (temp) string (currencybuffer[i-1]), causes garbage characters written. allowed, apparently: (why c++ allow integer assigned string?) because ints can converted implicitly chars, , chars can assigned strings,.

you want convert temp char using itoa or similar function (you did opposite correctly, atoi, when getting int string).

since you're in c++, easy way is:

std::stringstream itos; itos << temp; currencybuffer[i-1] = itos.c_str(); 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -