Thread: Need help in storing characters

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    Need help in storing characters

    Can someone tell me what am I doing wrong? The printed values are not the characters I input. I'm writing the code this way because I'm having trouble returning two values from the info function.

    Code:
    #include <stdio.h>
    
    char info ()
    {
        char x;
    
        printf("Insert a character: ");
        getchar();
        scanf("%c", &x);
    
        return x;
    }
    
    void main ()
    {
        char a, b, x;
    
        info ();
        a = x;
        info ();
        b = x;
        printf("%c and %c", a, b);
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well, you are calling info which returns its value to nowhere as it is discarded, thus you lose everything you read. a,b,x remain uninitialized and may contain any rubbish known to man.

    you want to do a = info(), b = info().

    HOWEVER, even your info function is wrong.Move the getchar() after your scanf to eat the enter key you press.

    Also, main should be int main(void) and should return 0;
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Thanks for the thorough help. The program works without declaring main() as int and adding the return 0 line. Is it necessary? I mean, the main function isn't supposed to return any value...

    It's weird that I've always used getchar() before scanf() and the program always worked, but not in this case. For instance:

    Code:
    #include <stdio.h> //standard header
    
    void main()
    {
    	float celsius, fahrenheit; //variables declared with float to allow for decimal places
    	char ans; //required to repeat the loop
    
    	do  //loop start
    	{
    	printf("Enter temperature in Fahrenheit: ");
    	scanf("%f", &fahrenheit); //input in Fahrenheit
    	celsius = (fahrenheit - 32) * 5 / 9; //conversion formula
    	printf("%.4f Fahrenheit is %.4f Celsius\n", fahrenheit, celsius); //displays output with 4 decimal places
    
    	printf("Do you want to try again? [Y/N]"); //loop prompt
    	getchar(); //required for the following scanf to work
    	scanf("%c", &ans); //asks if user wants to retry
    	} while (ans == 'y' || ans == 'Y'); //condition for loop
    }
    What makes this input characteristic different?
    Last edited by 843; 10-16-2010 at 05:38 AM.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    No, you are doing the same thing there... You are scanf-ing something... later on in the code you are getchar()-ing which eats the enter key pressed after the first scanf (THAT IS INPUT TOO!), and then you are scanf-ing again. If you remove that getchar() your enter key (i.e. new line ) will be fed into the second scanf which won't even wait for you to input anything. Try it out.

    As for main(), main is supposed to return an integer. In older C versions this was done automatically which is why you could write main(). However, since this is the 21st century and we have evolved from C89 quite a bit the correct forms are now:
    Code:
    int main(void){
     /* this program does not take arguments */
     return 0;
    }
    
    int main(int argc, char* argv[]){
    /* this program takes argc arguments stored in argv */
     return 0;
    }
    To read more about it go here:
    Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Ah, that explains why the second scanf wouldn't work without the getchar(). So they are always used together to eat up the Enter key? Is this normal?

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    If you plan to take input in the way you do (with scanf) then yes, it's pretty normal. There are other ways to take input in C and parse it afterwards. I suggest you look at functions like fgets(), fgetc(), sscanf(), sprintf() to learn more.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Thanks for the info.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing characters read from text file in 2 different string
    By rcplguy15 in forum C++ Programming
    Replies: 2
    Last Post: 09-25-2010, 04:25 AM
  2. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  3. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  4. 2D arrays and storing characters
    By John_L in forum C Programming
    Replies: 4
    Last Post: 10-13-2007, 12:17 PM
  5. storing a pattern of characters into a 2D array
    By haroonie in forum C Programming
    Replies: 2
    Last Post: 04-20-2005, 05:19 AM