Inputting a whole number with a fraction

Because I'm not sure how to make it read three different numbers from the same input

It's not the same input. It's the same stream of data.

How do you read three words in a sentence? One at a time.

okay,

it's starting to make sense now. So basically just use serial.parseint() for the first number, then serial.parseint() for second, serial.parseint() for third.

Then just divide the second and third number to find the fraction?

boolean asked=false;

void setup() { Serial.begin(9600); Serial.setTimeout(10000); }

void loop() { if(!asked) { promptForInput("Overall Length of the Wall: "); int length=getLength(); Serial.println(length); promptForInput("Overall Height of the Wall: "); int height=getHeight(); Serial.println(height); promptForInput("Area of Wall: "); int area=length*height; Serial.println(area); asked=true; } }

void promptForInput(char prompt[]) { while(Serial.available()) { Serial.read(); } Serial.print(prompt); }

float getAnswer() { float answer; int wholeNumber; int numerator; int denominator; answer=0; while(!Serial.available()) { //nothing happens unless there is something typed into serial moniter } wholeNumber=Serial.parseInt(); numerator=Serial.parseInt(); denominator=Serial.parseInt(); answer=numerator/denominator; return answer; }

int getLength() { int finalLength=getAnswer(); return finalLength; }

int getHeight() { int finalHeight=getAnswer(); return finalHeight; }

This is my code thus far, and I'm having troubles. Please bear with me

What I did in a basic language at work was to make an easy way to enter the geometry for R=pL/A
or L/A in this construct where A was the cross sectional area. Sometimes it was square and sometimes it was round and could be in units of mm cm um and angstroms.

I was able to enter things like 2 cm 100 um 1 cm * /

This was a sample with L= 100 um and a cross sectional area of 100 um * 1 cm. PI was allowed too.

It wasn’t hard, because it’s RPN or Reverse Polish Notation like the old HP calculators

This turned into:
2
push
cm
convert # on stack - in this case means ignore
100
push
um
Convert 1st number on stack from um to cm
1
push
convert to cm which means ignore
multiply - top 2 items on the stack
Divide - top two items on the stack

There are algorithms to convert algebraic to RPN, but that was too complicated, the simple notation was even better.

You could reallyaccept something in the form of n#<n#)"/"n# (in!cm!ft!)

So, the idea is to input as a string and assume a unit like feet.

So, you could handle 10.1 ft. 10 cm, 10 3/4 in etc.

It’s really a state diagram where you would start by looking for a space or you could parse it one character at a time.

so, yoou would have no spaces, one space or 3 spaces and then handle accordingly.

If you ignore the units conversions it will be easier.

The space is your friend, and your input when there is non leading space, assumes a fraction because 10 10 is invalid.

It’s messy, but should not be too hard.

Oreprocessing might be necessary where you reduce multiple spaces to 1 and just do a validity test to make sure you don’t get things like 10…2 or A etc.

Reverse Polish Notation like the old HP calculators

Still the best way.....

  answer=numerator/denominator;
  return answer;

So, if I enter 23 7/8, wholeNumber will be 23, numerator will be 7, and denominator will be 8, and answer will be 0.875. Is that what you really want.

and I'm having troubles.

So am I. I'm having trouble divining what your problem is.

HI, can you answer reply #3, please..... Where are you getting the data from that it is in 20 4/23 type format?

Tom...... :)

So, if I enter 23 7/8, wholeNumber will be 23, numerator will be 7, and denominator will be 8, and answer will be 0.875. Is that what you really want.

Yes, that is what I want to happen. Because I want the user to be able to read a tape measure then just be able input the measurement.

t's really a state diagram where you would start by looking for a space or you could parse it one character at a time.

How would I make it look for a space?

Yes, that is what I want to happen.

You want to throw the whole number part away? Well, OK. It's your program...

How would I make it look for a space?

parseInt() does that.

Something to think about, what if the person measures exactly 23 inches with no fraction part? How will they enter that and how will that jive with calling parseInt three times?

Also, you check that serial is available and then make three calls to parseInt, but what if all three numbers haven't arrived yet? Serial is very slow compared to the stuff going on in the processor. What if you get to that line and the whole number part and numerator have arrived but the denominator part hasn't yet? What will happen if you call parseInt three Tim's there?

If it were me I'd be reading the whole thing into a buffer so I could look for my delimiters and make sure I received what I expected. Then I'd use atoi() to get the numbers.

You want to throw the whole number part away? Well, OK. It's your program...

I read your post wrong, I want to keep the whole number

Hi, fine, thought it might have been something like that.
I love the fact I was bought up in a metric world, 34 and 7/10 cm = 34.7cm easy… in ya head.

Tom… :slight_smile:
PS Actually bought up in a world changing from foot, slugs and BTU and Calories measurement to metric.
Ahhh ten, TEN, 10, and 9.81, thats it basic physics taken care of.

I'm still having troubles with it reading the space? How would I do that. I've tried the parse.int() and it didn't work...

I'm still having troubles with it reading the space?

No, you're not.

Oh, wait. that wasn't a question, was it?

How would I do that. I've tried the parse.int() and it didn't work...

The code you didn't post DOES work. It just doesn't produce the result that you want. So, where is the code? What was the input? what was the output?