New program to read three numbered integer using getchar

This is a discussion on New program to read three numbered integer using getchar within the C Programming forums, part of the General Programming Boards category; 1. I know I'll get told off for not using int void main() and return 0; but when I tried ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    81

    New program to read three numbered integer using getchar

    1. I know I'll get told off for not using int void main() and return 0; but when I tried using them the program wouldn't compile at all. Now it runs but puts out a strange number (i.e. when I input 123 it outputs 2293568

    Code:
    #include<stdio.h> void main()
    
    
    {
    
    
    int num[3]; /*Sets array to 10*/
    int count;
    
    
    	printf("Enter a 3 numbered integer:");
    
    
    	for(count=0; count<3; count++){
                getchar();
                
      }
      
     	 printf("%d", num);
    		
    		
    return;
    
    
    }
    Last edited by Interista; 11-03-2011 at 11:24 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,630
    I don't really know what you are trying to do, but here is an example based on your code:
    Code:
    #include<stdio.h>
    
    int main(void)
    {
        int num[3];
        int count;
        printf("Enter a 3 numbered integer:");
        for (count = 0; count < 3; count++)
        {
            num[count] = getchar();
            printf("%d\n", num[count]);
        }
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    16
    Code:
    #include<stdio.h>
    int main()
    {         int num[3]; /*Sets array to 3*/
            int count;
            char c;
            printf("Enter a 3 numbered integer:");
            for(count=0; count<3; count++){ 
                    c=getchar();
                    printf("%c\n",c);
            } 
            return 0;
    
    }
    ~

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by looza View Post
    Code:
    #include<stdio.h>
    int main()
    {         int num[3]; /*Sets array to 3*/
            int count;
            char c;
            printf("Enter a 3 numbered integer:");
            for(count=0; count<3; count++){ 
                    c=getchar();
                    printf("%c\n",c);
            } 
            return 0;
    
    }
    ~
    If you are trying to compile a 3 digit number like 192 using getchar()...
    1) use scanf() it's just a whole lot easier.

    2) you need this...
    Code:
    num = 0;
    
    for (i = 0; i < 3; i++)
      { num = num *10;
         num =  num + getchar(); }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing an integer in an array using getchar
    By Interista in forum C Programming
    Replies: 9
    Last Post: 11-04-2011, 12:37 PM
  2. read in from serial port; getchar()
    By s.dodd in forum C Programming
    Replies: 9
    Last Post: 10-23-2011, 10:50 AM
  3. Keeping track of the player in a 2d numbered grid.
    By Shamino in forum C++ Programming
    Replies: 1
    Last Post: 03-25-2009, 05:39 PM
  4. How to read in an integer and display it again
    By axr0284 in forum C++ Programming
    Replies: 7
    Last Post: 12-07-2004, 12:37 PM
  5. numbered lines
    By michele CD in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2003, 10:44 AM

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