1. ## help with input.

hello everybody, i'm doing a course in c and one of our homework assigments was to write a program for a flower shop, and i ran into a weird problem

when the customer is asked to input a shipping distance and weekday for shipping, he needs to write it like so: 15.3 when the number before the "." is the distance and after is the weekday, when 0.3 means pickup on day 3.

now everything works fine besides the 0, when i put 0.3 or 0.7 or whatever with the 0 before the "." nothing happens, and the program simply waits for more input. why is that? thank you for your help.

this is the code:

#include <stdio.h>

int main()
{ char F,S;
int a,b,c,d;
float flowers, shipping, total;
scanf("%c", &F);
switch(F){
case 'D':
flowers=1.35;
break;
case 'R':
flowers=4.30;
break;
case 'T':
flowers=2.75;
break;
case 'P':
flowers=2.75;
break;
case 'B':
flowers=2.75;
break;
case 'C':
flowers=1.95;
break;
case 'G':
flowers=1.95;
break;
case 'F':
flowers=1.15;
break;
case 'O':
flowers=3.50;
break;
case 'A':
flowers=3.50;
break;
default:
printf("ERROR");
return 1;}
scanf("%d", &a);
if(a>0) flowers=a*flowers;
else {printf("ERROR");
return 1;}
scanf("%d", &b);
if(a>(b*12)) flowers=flowers+(b*10);
else {printf("ERROR");
return 1;}
printf("please enter shipping distance and weekday, seperated by '.':");
scanf("%d.%d", &c,&d);
if (c<25) shipping=(c*(float)3/7);
else if (c>=25) shipping=((25*(float)3/7)+((c-25)*(float)2/7));
if (d>=1 && d<=7);
else {printf("ERROR");
return 1;}

if (c>0) printf("would you like an express shipping?");
scanf("\n%c", &S);
if (S=='Y') shipping=(shipping+25);
else if (S=='N') shipping=shipping;
else {printf("ERROR");
return 1;}
printf("receipt:");
printf("\ncost of flowers = %f", flowers);
printf("\ncost of shipping on day %d = %f", d, shipping);
printf("\ntotal cost of flowers + shipping = %f NIS", total=shipping + flowers);

return 0;
}

i can only use switch, and if else functions.

2. Welcome to the forum, fatsrir!

I suggest keeping weekday an integer, and distance as an integer. Think of the period between them, as just a separator - something to note, and then ignore.
Code:
```scanf("%d%c%d", &c,&dot,&d); //so add char dot to your variable
//declarations, and forget changing
//anything to a float.```
The easy way to work with money is to *immediately* times it by 100, (making everything pennies), and then work with the money as integers, and NOT FLOATS!

Then divide it by 100 when you need to print it out again. Why? Because floats can't represent every number - and you will get small errors.

3. Read this thread if you plan to post code, and want people to read it. I read your post anyway this time, but don't expect people to keep doing that.

The problem has nothing to do with the scanf() call where you are reading variables c and d.

It has to do with the fact that execution continues and reaches these lines
Code:
```if (c>0) printf("would you like an express shipping?");
scanf("\n%c", &S);```
You are probably expecting that the second line is affected by the preceding "if" statement. It is not, so your program is waiting for you to enter a character (and hit the return key).

Welcome to the forum, fatsrir!

I suggest keeping weekday an integer, and distance as an integer. Think of the period between them, as just a separator - something to note, and then ignore.
Code:
```scanf("%d%c%d", &c,&dot,&d); //so add char dot to your variable
//declarations, and forget changing
//anything to a float.```
The easy way to work with money is to *immediately* times it by 100, (making everything pennies), and then work with the money as integers, and NOT FLOATS!

Then divide it by 100 when you need to print it out again. Why? Because floats can't represent every number - and you will get small errors.
the weekday and distance are already declared as integer while . is just a seperate. i tried what u suggested with another variable instead of, but nothing changed. same problem. thank you for your help.

5. Originally Posted by grumpy
Read this thread if you plan to post code, and want people to read it. I read your post anyway this time, but don't expect people to keep doing that.

The problem has nothing to do with the scanf() call where you are reading variables c and d.

It has to do with the fact that execution continues and reaches these lines
Code:
```if (c>0) printf("would you like an express shipping?");
scanf("\n%c", &S);```
You are probably expecting that the second line is affected by the preceding "if" statement. It is not, so your program is waiting for you to enter a character (and hit the return key).
thank you for your help!! yes you are right, when i try entering Y or N, it finish the program properly, so i just put { before the printf and} after the last else with return1.

THANK YOU! i hate these stupid mistakes!

6. You have done very silly mistakes
scanf("%d.%d", &c,&d);

no dot b/w %d and %d
it should be like %d%d

7. Originally Posted by Mr. AB
You have done very silly mistakes
scanf("%d.%d", &c,&d);

no dot b/w %d and %d
it should be like %d%d
Except, of course, he wanted the . there explicitly. Did you read the thread?