Thread: reading characters

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    40

    reading characters

    Hi,

    I am having some problems with my code. I am trying to read characters from scanf. What happens is that it doesnot read the whole character it read only 1 character. I tried making a loop but it keeps giving me errors.



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	char c;
    	float i;
    	int number;
    	float f =50;
    
    
    	scanf("%c", &c);
    
    	number = atoi(c);
    
    	i=f+(float)number;
    
    	printf("%f",i  );
    
    	return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    atoi takes a string, not a single character, and scanf with %c only reads a single character

    You need something like
    Code:
    char c[10];
    fgets( c, sizeof c, stdin );
    number = atoi(c);

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    40
    the thing is that we are only allowed to use scanf and we have to use characters.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    char c[10];
    
    scanf("%s", c);
    If your teacher isn't letting you use strings, then this is just a bad assignment.
    Last edited by SlyMaelstrom; 02-12-2006 at 09:26 AM.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    It's easier to scanf an int to begin with:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int number;
    
        printf("Please give an integer and press enter: ");
    
        /* read a number from the keyboard and assign it to 'number' */
        scanf("%d", &number);
    
        printf("You gave %d\n", number);
    }

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I think the idea of the program was to convert from a character to an integer to a float.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    40
    I am able to do it with strings. but the problem is that the teacher wants us to read character and then we have to convert all those character to floats so I can do some computations.

  8. #8
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Code:
    char string[9] = { 0 };
    float f;
    
    scanf("%c%c%c%c%c%c%c%c", string[0], string[1], string[2], string[3], string[4], string[5], string[6], string[7]);
    sscanf(string, "%f", &f);
    hehehehe
    Last edited by Brian; 02-12-2006 at 12:14 PM.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    fgets().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    40
    thanks guys I used fgets since the way my prof is saying it doesnot make sense.Thanks alot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Convert string of characters to integers
    By Digital_20 in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2008, 09:40 PM
  3. problem in reading alphabets from file
    By gemini_shooter in forum C Programming
    Replies: 6
    Last Post: 03-09-2005, 01:49 PM
  4. Reading Characters from file into multi-dimensional array
    By damonbrinkley in forum C Programming
    Replies: 9
    Last Post: 02-24-2005, 01:31 PM
  5. Reading files with an unkown amount of characters
    By Zahl in forum C++ Programming
    Replies: 13
    Last Post: 10-10-2002, 02:04 PM