\n after scanf

This is a discussion on \n after scanf within the C Programming forums, part of the General Programming Boards category; Hi everyone, I'm new here, could you guys help me on this problem I have a code like this Code: ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    \n after scanf

    Hi everyone,
    I'm new here, could you guys help me on this problem
    I have a code like this
    Code:
    scanf("%g\t",&hours);
    charge=Charges(hours);
    printf("%g\n",charge);
    I want hours and charge on the same line.

    But when I run this, it always runs \n after I put the value of hours in.
    Could anyone help me to make it on the same line?

    Thank you

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    You cannot get it with scanf - you need some platform dependent function for getting input from theuser without pressing the Enter - look in the FAQ
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    If you want hours and charge on the same line, why don't you just print them both on the same line? An input prompt also makes it easier to tell between input and output.
    Code:
    printf("> ");
    scanf("%g",&hours);
    charge=Charges(hours);
    printf("%g\t%g\n",hours, charge);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 07:35 AM
  2. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 03:39 PM
  3. Replies: 2
    Last Post: 02-20-2005, 12:48 PM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM

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