Unwanted Line

This is a discussion on Unwanted Line within the C Programming forums, part of the General Programming Boards category; Hi, I've finished writing a code to display the integral and fractional part of a number. It's all working fine, ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    19

    Unwanted Line

    Hi,

    I've finished writing a code to display the integral and fractional part of a number. It's all working fine, however, when asked to enter a number, the program continues to go down to the next line until a letter is entered. Here is the code:
    Code:
    /* Intgral and Fraction */
    /* By Luke Sowersby */
    
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    float fraction, number;
    char decision;
    
    int Round(float number)
    {
     	return (int)(number);
    }
    
    float fraction2(float fraction)
    {
    	fraction=(number-Round(number));
    	return fraction;
    }
    
    void main(void)
    {
    Start:
    	printf("Please enter your number, including decimals:\n");
    	getchar();
    	scanf("%f%",&number);
    	printf("The integral part of this fraction is: %d",Round(number));
    	printf("\nThe fractional part of this fraction is: %f",fraction2(fraction));
    	printf("\nWould you like to re-run the program? (Y or N)\n");
    Ask:
    	decision=getch();
    	if(decision!='Y' && decision!='N')
    		goto Ask;
    	if(decision=='Y')
    		goto Start;
    }
    Here is a screenshot of the problem i'm having:
    http://img301.imageshack.us/my.php?i...antedlines.jpg

    Thanks, Luke.
    Last edited by lukesowersby; 03-25-2009 at 08:30 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The problem is most likely caused by mixing getch() with getchar() and scanf() - and the first time you run the code, it will wait for getchar() to read a character - it will probably work better to do that AFTER you do scanf().

    And DO NOT use GOTO!!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    the middle of nowhere
    Posts
    11
    use int main instead of void main

  4. #4
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    put the getchar() after the scanf

    remove the extra % after %f inside the scanf
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    19
    Thanks, it was the extra % sign that was the problem, well spotted. Thanks for the help.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Code:
    char decision;
    
    ...
    
    decision=getch();
    getch returns an int, not a char.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer and Polymorphism help.
    By Skyy in forum C++ Programming
    Replies: 29
    Last Post: 12-18-2008, 08:17 PM
  2. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 03:13 PM
  3. Finding carriage returns (\c) in a line
    By JizJizJiz in forum C++ Programming
    Replies: 37
    Last Post: 07-19-2006, 05:44 PM
  4. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM

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