Title: hints for hw 4
1hints for hw 4
- Must be able to read both integers and floats.
What is the best data type for this (you do not
want to lose information)?
2/This program will read a float using scanf then
read a char using getchar(). / /Use it to
experiment mixing and matching scanf and
getchar() / /You may want to add more
variables, or use constructs to skip certain
input. / include ltstdio.hgt int main()
float answer 0 char oper ' '
while(1) printf("Enter your data
") scanf("f", answer) oper
getchar() printf("answer f, oper
'c'\n", answer, oper) return 0
3notes on CTRL-C
- CTRL-C is not EOF. You DO NOT have to test for a
CTRL-C character. CTRL-C will kill the DOS
window where your program runs. Try it on one
you've written so far. - Remember we used CTRL-C in class to break out of
an infinite loop. Essentially, that is what you
will use it for here too - to break out of an
infinite loop like while(1)
4hw algorithm (needs refinement)
- Within your outermost loop while (1) (iterates
once per expression) you need to do the
following - read your first operand (hint could assign it to
answer) - while an illegal character hasn't been entered
and you don't have a new line, you'll be looking
for your valid operators, ,-,/, - if you have one then you'll
- read in a new number then update your answer by
the operator (e.g. Answer Operand ) - Check for divide by zero
- when you get a new line you'll have to print the
answer - or an illegal character flush the rest of the
line with the code given on the assignment's web
page. - Of course you must then go back to reading in
your first operand, etc.