Thread: How to take in input of just ints

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    65

    How to take in input of just ints

    here is the code, if you have any ideas help me out please.
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
         printf("test for robot");
         for(x=0,x>=0;x++){
                                        while(( array[x] = getch()) != EOF){
                                                  if(array[x] == 1)
                                                  printf("test 1");
                                                  else if(array[x] ==2)
                                                  printf("test 2");
                                                  else 
                                                  printf("error");  }
                                       }
    }
    as you can notice I have it set to getch/getchar and I need it to be set to look for int's, any easy way to achieve this or will I need to make a function, if you have any suggestions or ideas, help a brother out.
    You rant and rave about it, but at the end of the day, it doesn't matter if people use it as long as you don't see.
    People are free to read the arguments, but if the only way for you to discover gravity is by jumping off a cliff, then that is what you're going to have to experience for yourself.
    Eventually, this "fast and loose" approach of yours will bite you one too many times, then you'll figure out the correct way to do things. - Salem

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Where is array defined?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Something like
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int getnumber( void ) {
       char input[DIGITS_LONG + 1];
       unsigned int idx;
    
       fgets(input, DIGITS_LONG + 1, stdin);
    
       for ( idx = 0U; idx < DIGITS_LONG && isdigit(input[idx]); idx++ )
          ;
    
       if( idx == DIGITS_LONG )
          return atoi(input);
       else
          return -1;
    }
    But you'd have to define what DIGITS_LONG is.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > for(x=0,x>=0;x++)
    My guess is that this won't even compile, so the code you've actually tried is different.

    Post actual code you've tried, not what comes from smashing your head into the keyboard.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    well I was working on the code in a back room and has no network connections, so I quickly ran across the building to a computer with internet to post the code. I was in a rush and made sure I posted the single problem because everything else had been working correctly. That why nothing is int'ed, the for loop is wrong and the indents are goofy. Funny thing Salem that it not only compiles but worked great as a nested loop.
    You rant and rave about it, but at the end of the day, it doesn't matter if people use it as long as you don't see.
    People are free to read the arguments, but if the only way for you to discover gravity is by jumping off a cliff, then that is what you're going to have to experience for yourself.
    Eventually, this "fast and loose" approach of yours will bite you one too many times, then you'll figure out the correct way to do things. - Salem

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Like I said, the code you have may compile and "work", but you're asking us to comment on something you just typed from memory.

    I barely trust people to be able to copy/paste correctly, so typing what you think you have has no chance.

    So figure out a way of copying the code from your development machine to your net machine, or face a series of useless replies pointing out the dumb typos in your post. That is before we all get ........ed off from telling you to do this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input redirection
    By sashaKap in forum C Programming
    Replies: 6
    Last Post: 06-25-2009, 01:59 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. Help with Input Checking
    By Derek in forum C Programming
    Replies: 7
    Last Post: 06-17-2003, 03:07 AM