Program runs and calculates before all integers are input

This is a discussion on Program runs and calculates before all integers are input within the C Programming forums, part of the General Programming Boards category; Hi People I am trying to run the following program in at the command line in Ubuntu and it prompts ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    4

    Program runs and calculates before all integers are input

    Hi People
    I am trying to run the following program in at the command line in Ubuntu and it prompts for the first number ok but as soon as I press enter to input the next number it runs the program with an erroneous results.

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int num1, num2;
    	int product;
    
    	printf("Please enter the first integer number: ");
    	scanf("%D%*c", &num1);
    
    	printf("Please enter the second integer number: ");
    	scanf("%D%*c", &num2);
    
    	product = num1 * num2;
    
    	printf("The product of the two numbers is: %d\n", product);
    	return(0);
    }
    Please enter the first integer number: 1
    Please enter the second integer number: The product of the two numbers is: 0
    the same program works perfectly well in a terminal in windows but not in Linux where I would prefer to be working.
    Can anybody tell me why this is happening

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    scanf("%D%*c", &num1);
    &#191;Qu&#233;?

    Just avoid scanf.
    User Input: Strings and Numbers [C]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >¿Qué?
    nani?
    My best code is written with the delete key.

  4. #4
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    > nani?
    was?

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    4

    Unhappy Ummm!

    Ok I have had a look at that and tried fgets and fputs instead of scanf but still no joy.
    The code I have be given was supplied by my lecturer which is interesting. I am still bemused that scanf works ok in bcc32 in windows but not in gcc in a terminal in linux! Bizarre.
    Can I get a little more guidance.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Use the correct format specifier for an int -- %D is not it (case sensitivity is key) [spoonfeeding is bad].
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    4
    Ok Thanks for that. My first crack at C so I am just not use to the syntax at all having come from VB

  8. #8
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36
    Nani means "what?" though i dont know what &#191;Qu&#233;? means so i googled it:
    When used with the verb ser, cu&#225;l and qu&#233; can both mean "what," but they are not interchangeable. Cu&#225;l is more common, and is used to indicate a selection, ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PROGRAM MENU, urgent, please help!
    By Anfernee001 in forum C Programming
    Replies: 13
    Last Post: 09-27-2002, 04:29 AM

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