Thread: Very simple question

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    20

    Very simple question

    hi:

    If i can use getchar() function to input some numbers from keyboard only, how i can convert or use any method to sum those numbers up? Thanks!!!

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    20
    also i cannot use array to hold the number..

  3. #3
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    Code:
    int x;
    printf("Enter a number:");
    scanf("%d", &x);
    x now takes the value of whatever you enter.

    Code:
    int c;
    c = getchar();
    int x = c - '0';
    Here, c must be between 0 and 9 inclusive. This is obviously no good.

    Please read this
    Last edited by Tommo; 09-01-2007 at 04:34 PM.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by Tommo View Post
    Code:
    int x;
    printf("Enter a number:");
    scanf("%d", &x);
    x now takes the value of whatever you enter.

    Code:
    char c;
    c = getchar();
    int x = c - '0';
    Here, c must be between 0 and 9 inclusive. This is obviously no good.

    Please read this

    gehchar return and int. So your char should be int to make it perfect.

    ssharish2005

  5. #5
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    Sorry, you are correct yes. Thanks

  6. #6
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Well i'm not sure to understand what you are trying to do but you sure can recompose a number by reading one character at a time.

    Somehow like this
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main()
    {
    	int carac;
    	int nombreEntree = 0;
    
    	printf("Enter a non negative number: ");
    
    	carac = getchar();
    	while (isdigit(carac))
    	{
    		nombreEntree = nombreEntree * 10 + (carac - '0');
    		carac = getchar();
    	}
    
    	// Flush the input buffer if it isn't empty
    	if (carac != '\n')
    	{
    		while (getchar() != '\n');
    	}
    
    	printf("\nThe number you entered is: %d", nombreEntree);
    	getchar();
    
    	return 0;
    }
    But you should make a call to scanf since scanf is good for reading number. Except in the case this was some sort of homework... whatever..

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    13
    Code:
    ...
    nombreEntree = nombreEntree * 10 + (carac - '0');

    Is this (and the others " - '0'"s) because getchar stores an ASCII?

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by Dori&#228;n View Post
    Code:
    ...
    nombreEntree = nombreEntree * 10 + (carac - '0');

    Is this (and the others " - '0'"s) because getchar stores an ASCII?
    YES, Hence getchar return's and int

    ssharish2005

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    13
    Quote Originally Posted by ssharish2005 View Post
    YES, Hence getchar return's and int

    ssharish2005

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM