Thread: scanf reading more values than intended

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    45

    scanf reading more values than intended

    Hello all..I am kind of trying my hand at C as a beginner following a standard book. I typed program as such for gcd of 2 numbers. but I dont know why scanf is taking more values than 2 and not terminating. I checked the syntax and it seems fine. What could be the problem?

    I am attaching the code. Thanks in advance.

  2. #2
    Registered User
    Join Date
    Dec 2010
    Posts
    7
    scanf is working fine in your code. there is a problem with your 'gcd' function.
    if I put printf after scanf in main like this:

    Code:
    #include <stdio.h>
    // more code
    int main(){
    	int i =0 ,j = 0;
    	puts("Enter the numerator and denomonator");
    	scanf("%d %d", &i, &j);
    	printf("You entered: %d %d\n", i, j);
    	int k = gcd(i,j);
    	printf("The GCD of the numbers inputted are %d	\n", k);
    	return 0;
    }
    If I enter 1 and 2, I get:
    Code:
    Enter the numerator and denomonator
    1
    2
    You entered: 1 2
    Look through your gcd function

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    Thanks..I kind of figured it out..missing braces for while loop..
    Last edited by livin; 03-10-2011 at 08:59 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading in char for float with scanf
    By ramparts in forum C Programming
    Replies: 3
    Last Post: 11-05-2006, 01:05 AM
  2. Need help with project
    By chrisa777 in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2006, 05:01 PM
  3. keeping values of scanf
    By n00by in forum C Programming
    Replies: 1
    Last Post: 04-17-2004, 03:38 AM
  4. reading an input line using scanf() with %c
    By linucksrox in forum C Programming
    Replies: 6
    Last Post: 04-04-2004, 03:10 PM
  5. Replies: 14
    Last Post: 03-17-2003, 10:07 AM