0

I am doing assignment 4a. When I try to print my balance, the display is printing letters instead of numbers. I tried printing them towards my serial monitor, however, here numbers are printed. Does anyone has an idea what is going on??

Share a link to this question (includes your user id)
| edit | | close | delete |
0

My code looks like this:

void Userinterface::printBalance(){ 
  display->setCursor(0, 4); // goto line 4
  display->print(coin); 
  display->print("c");
  balance = controller->getBalance();
  int euros = balance / 100;
  int cents = balance % 100;
  display->drawString(0,5,"Balance:");
  snprintf(buf, sizeof(buf), "%1d", euros, cents); // put value of euros into string buffer
  display->draw2x2String(0, 6, euros); // and print it
  display->draw2x2String(2, 6, ".");
  display->draw2x2String(4, 6, cents);
Share a link to this answer (includes your user id)
| edit | | delete |
0

These 2 lines will not work properly:

snprintf(buf, sizeof(buf), "%1d", euros, cents); // put value of euros into string buffer
display->draw2x2String(0, 6, euros); // and print it

In the last line, you must use buf instead of euros.

Maybe try this:

  // print value balance at line 5,6:
  display->drawString(0,5,"Balance:");
  snprintf(buf, sizeof(buf), "%1d", euros); // put value of euros into string buffer
  display->draw2x2String(0, 6, buf); // and print it
  display->draw2x2String(2, 6, ".");
  snprintf(buf, sizeof(buf), "%1d", cents); // put value of count into string buffer; %2d means we have 2 decimal places
  display->draw2x2String(4, 6, buf);
Share a link to this answer (includes your user id)
| edit | delete |
0

Maybe share your code for the printBalance() method here?

Do you use the example code from the assignment?:

display->drawString(0,5,"Balance:");
snprintf(buf, sizeof(buf), "%1d", euros); // put value of euros into string buffer
display->draw2x2String(0, 6, buf); // and print it
display->draw2x2String(2, 6, ".");
Share a link to this answer (includes your user id)
| edit | delete |
0

Great, this works. Thank you!

Share a link to this answer (includes your user id)
| edit | | delete |

Your Answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.