help with input.

This is a discussion on help with input. within the C Programming forums, part of the General Programming Boards category; hello everybody, i'm doing a course in c and one of our homework assigments was to write a program for ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    25

    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;
    printf("please choose a flower:");
    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;}
    printf("please enter quantity of flowers:");
    scanf("%d", &a);
    if(a>0) flowers=a*flowers;
    else {printf("ERROR");
    return 1;}
    printf("please enter quantity of bouquets:");
    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. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.
    Last edited by Adak; 04-16-2010 at 05:15 AM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,210
    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).
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    25
    Quote Originally Posted by Adak View Post
    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. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    25
    Quote Originally Posted by grumpy View Post
    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!
    Last edited by fatsrir; 04-16-2010 at 05:44 AM. Reason: solved

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    10
    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. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Mr. AB View Post
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocating input to an array
    By ij.cohen in forum C Programming
    Replies: 2
    Last Post: 10-12-2009, 10:48 AM
  2. Problem grabbing k/b input
    By falcon9 in forum C Programming
    Replies: 2
    Last Post: 10-28-2007, 11:47 AM
  3. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  4. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  5. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21