multiple decimals = problem

This is a discussion on multiple decimals = problem within the C Programming forums, part of the General Programming Boards category; Im using a double and putting the values into an array. Im trying to make it idiot proof but when/if ...

  1. #1
    Unregistered
    Guest

    Unhappy multiple decimals = problem

    Im using a double and putting the values into an array. Im trying to make it idiot proof but when/if someone were to enter a number with 2 decimals (ie.. 78.9.04) it would take the first legit number (78.9) and enter it as the first object in the array and AUTOMATICALLY enter 0.04 as the second object?

    How can I get it to post an error when more than one ( - or . ) is entered?

    Thanks
    FS

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Start by showing us the code that's doing the read in... then we can guide you better.

    In the meantime, you could try using fgets() to grab the input into a string, then validating it (ie making sure its a proper number) before loading it to the array.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Unregistered
    Guest

    here is what I have so far

    Im very new, so please go slow... =) thank you!

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    
    main ()
    {
    	int maxnum;
    	int counter;
    	double a;
    
    	printf("How many numbers would you like to sort?\n");
    	scanf("%d", &maxnum);
    	printf("Please begin entering your %d numbers:\n", maxnum);
    	
    // GATHER DATA	
    	
    	for (counter = 0; counter < maxnum ; counter++){
    		printf("Enter number %d\n", counter + 1);
    		scanf("%lf", &a);
    		printf("The number %g was entered.\n", a);
    	}
    	getch();
    }

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    sorry

    I didnt see that I wasnt logged in to edit that first post.
    My apoligies.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If you want to stick with the scanf() method, you'll need to flush the input stream after a call to scanf().
    >while (getchar() != '\n');
    This will *eat* the characters in the input stream until a newline character is seen.

    You should also check the return code from scanf() to ensure that a valid number was read.

    Have a go and see what you come up with. Post again if you have more troubles.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. problem while opening files from multiple directories
    By V.G in forum Windows Programming
    Replies: 2
    Last Post: 11-08-2004, 03:29 PM
  3. small reference problem
    By DavidP in forum C++ Programming
    Replies: 6
    Last Post: 06-21-2004, 08:29 PM
  4. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 01:30 PM
  5. Replies: 5
    Last Post: 12-03-2003, 05:47 PM

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