Thread: Counting Integer help!

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    1

    Counting Integer help!

    Hey guys & girls,
    I've got a problem, within an assignment of mine - i have to write up a function that counts the number of integers that have been input by the user, and if an integer has been read more than once, then print "Invalid".

    So far, I have:

    Code:
    #include <stdio.h>
    
    void count_int() {
      int x=0;
      int count=0;
    
      printf("Enter several integer values:\n");
      scanf("%d", &x);
    
      /*not sure what to put in here so it can count the number of integers
      it has read.*/
    
    }
    any suggestions?

    Thanks,
    Jason =)

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    How about putting the printf line of code, and the scanf() line of code, inside a while loop. Tell them to enter -1 when they want to quit entering integers.

    Also in the while loop, would be a counter that would be set to zero just before the while loop, and increment once for every time through the loop.

    And welcome to the forum, Jason!

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    Have an array of integers, and store each value inputed by the user in that array.

    Through each input the user types, loop through all of the values stored in the array and compare them with what the user recently inputed.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    29

    Talking -1 is an integer

    Quote Originally Posted by Adak View Post
    How about putting the printf line of code, and the scanf() line of code, inside a while loop. Tell them to enter -1 when they want to quit entering integers.

    Also in the while loop, would be a counter that would be set to zero just before the while loop, and increment once for every time through the loop.

    And welcome to the forum, Jason!
    We can't leave out -1 from possible integers. I assume it could be always assumed that a -1 is always going to be entered and there isn't a problem with this, but do we want to assume this?

    - Edward

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The program asks the user to input integers - fine.

    The user will have to have *some* way of leaving that loop. In some applications, integers means real integers, because you can't, for example, have -1 students in a class, or have a prescription for -1 pills.

    My suggestion relies on the OP to use common sense in making the choice of data type to use, to exit the entry loop.

  6. #6
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Quote Originally Posted by CProg3 View Post
    Hey guys & girls
    Girls?!
    silly boy! there are NO GIRLS on the internet!

    there has to be a range specified, if one wants to check for repetition, otherwise the arraysize must be equal to integer range...and instead of searching everytime if a number is already stored or not , it would be much easier if one stores the number in the index of the array

    for example if user enters 10 then store 10 in array[10] ... and next time when user enter 10 again check if array[10]=10 or not.
    Last edited by creeping death; 04-25-2009 at 06:12 AM.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. Replies: 7
    Last Post: 08-19-2007, 08:10 AM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM