Can't turn string into a double SOMEONE HELP ME

For some reason line 38 returns an error. The user is expected to enter the price of an item on a 16-key pad. Press * to accept, # to decline. It's expected to convert the price thing to a double to add to the total, but why it aint working?
No description
No description
No description
17 Replies
DarwinWasWrong
DarwinWasWrong•5mo ago
Be a lot easier to read text than pictures
ur regular minecraftian
ur regular minecraftianOP•5mo ago
OK... @DarwinWasW
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

LiquidCrystal_I2C screen(0x27, 16, 2);

// Keypad info
const byte cols = 4, rows = 4;
char keys[rows][cols] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowpins[4] = {9, 8, 7, 6};
byte colpins[4] = {5, 4, 3, 2};
Keypad inputkeypad = Keypad(makeKeymap(keys), rowpins, colpins, rows, cols);
// Price info
double total = 0;
// Input
String priceinput = "";
// Other info
byte mode = 1;

void setup() {
screen.init();
screen.backlight();
Serial.begin(9600);
}

void loop() {
screen.clear();
char inputkey = inputkeypad.getKey();
if (inputkey) {
switch(mode) {
case 1: // Price input mode
if (inputkey == 'D') {priceinput += ".";}
else if (inputkey == '*') {total += double(priceinput); priceinput = "";}
else if (inputkey == '#') {priceinput = "";}
else if (! (inputkey == 'A' || inputkey == 'B' || inputkey == 'C')) {priceinput += inputkey;}
}
}
if (priceinput == "") {
screen.print("Type item price");
screen.setCursor(0, 1);
screen.print(String("Total: P") + String(total));
}
else {
screen.print(priceinput);
screen.setCursor(0, 1);
screen.print("*:Entr #:Cancl");
}
delay(100);
}
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

LiquidCrystal_I2C screen(0x27, 16, 2);

// Keypad info
const byte cols = 4, rows = 4;
char keys[rows][cols] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowpins[4] = {9, 8, 7, 6};
byte colpins[4] = {5, 4, 3, 2};
Keypad inputkeypad = Keypad(makeKeymap(keys), rowpins, colpins, rows, cols);
// Price info
double total = 0;
// Input
String priceinput = "";
// Other info
byte mode = 1;

void setup() {
screen.init();
screen.backlight();
Serial.begin(9600);
}

void loop() {
screen.clear();
char inputkey = inputkeypad.getKey();
if (inputkey) {
switch(mode) {
case 1: // Price input mode
if (inputkey == 'D') {priceinput += ".";}
else if (inputkey == '*') {total += double(priceinput); priceinput = "";}
else if (inputkey == '#') {priceinput = "";}
else if (! (inputkey == 'A' || inputkey == 'B' || inputkey == 'C')) {priceinput += inputkey;}
}
}
if (priceinput == "") {
screen.print("Type item price");
screen.setCursor(0, 1);
screen.print(String("Total: P") + String(total));
}
else {
screen.print(priceinput);
screen.setCursor(0, 1);
screen.print("*:Entr #:Cancl");
}
delay(100);
}
PenPengu
PenPengu•5mo ago
https://docs.arduino.cc/language-reference/en/variables/data-types/stringObject/ You can't just cast a string to double. You need to use the .toFloat() method @MinecraftFan69420
ur regular minecraftian
ur regular minecraftianOP•5mo ago
And then turn that float to a double?
ur regular minecraftian
ur regular minecraftianOP•5mo ago
Oh thank you!
PenPengu
PenPengu•5mo ago
double is just a larger float btw this might be a bit advanced for you but just saying... computers (and arduinos) are actually kinda bad at math. When you do maths with doubles, you can sometimes get wrong results. For example 0.1 + 0.2 should be 0.3 but the arduino will actually give you 0.30000000000000004. Might not matter at all for your use case. But if you do more math with the prices, then you might run into issues Idea: Don't use floating point maths, don't use floating point datatypes. So no float and no double. Computers are actually great at maths. Just not at floating point math. So you can just use int and you won't have any of those problems. But int can't do decimal numbers, right? You don't actually need decimal numbers for prices! Just use cent! int price could be the price in cents. Then you just add the decimal when you display it to the user
ur regular minecraftian
ur regular minecraftianOP•5mo ago
So we put the total as an int for c (cents)? And then convert it to a string And then add a decimal at the third-to-last thing Something like that
PenPengu
PenPengu•5mo ago
yes
ur regular minecraftian
ur regular minecraftianOP•5mo ago
@PenPengu Cool
PenPengu
PenPengu•5mo ago
Store the prices and everything as int and use cent. When you want to display it to the user: Convert the int to string. Add a decimal . before the last two digits. basically Again, this might not matter at all in your use case but could still be interesting to think about 😆: Using int instead of double can be way faster, and make your program smaller.
ur regular minecraftian
ur regular minecraftianOP•5mo ago
OK, thx!
WeeGee
WeeGee•5mo ago
It's also possible to store things as integerdollars and cents, which might make printing easier, but you need to be careful to make sure you don't have 51 dollars and 105 cents or something
WeeGee
WeeGee•5mo ago
If you want to learn more about floating point math and why it's the way it is (it all is to do with converting decimal numbers to floating point and back), then here's some resources: https://0.30000000000000004.com/ https://www.youtube.com/watch?v=9hdFG2GcNuA
UncommentatedPannen
YouTube
Floats
I explain how floats work, also known as floating point numbers. Floats are a type of variable, which are used to approximate real numbers. Other types of variables, such as bytes, shorts, and longs, can only represent integers. So in SM64, those are used to represent Mario's lives, Mario's coin count, Mario's HP, angles, timers, vertices of lev...
WeeGee
WeeGee•5mo ago
The tl;dr of it is to imagine you were asked to do calculations with 1/3 but you were forbidden to use fractions anywhere. No matter how many decimal places you use, you can't perfectly represent 1/3 in decimal. Floating point is the same but with 1/10 in binary being a repeating decimal
PenPengu
PenPengu•5mo ago
I didn't watch it yet. But thanks so much! I didn't know how to explain it well and couldn't find a good link right now
WeeGee
WeeGee•5mo ago
it's a video I like linking because - it hits coarseness, repeating digits, type casting all in 1 video - is reasonably beginner friendly and of course, sm64

Did you find this page helpful?