#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    1

    Mixing characters and numbers in an array


    I am trying to build this program which would take a few integers, hyphens and characters of a phone number as input, and then display them as only integers and hyphens:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char a[15];
    	int i;
    	
    	printf("Enter phone number: ");
    	for (i = 0; i < 15; i++) {
    		scanf("%c", &a[i]);
    	}
    	
    	for (i = 0; i < 15; i++) {
    		if (i == 'A' || i == 'B' || i == 'C')
    			a[i] = '2';
    		else if (i == 'D' || i == 'E' || i == 'F')
    			a[i] = '3';
    		else if (i == 'G' || i == 'H' || i == 'I')
    			a[i] = '4';
    		else if (i == 'J' || i == 'K' || i == 'L')
    			a[i] = '5';
    		else if (i == 'M' || i == 'N' || i == 'O')
    			a[i] = '6';
    		else if (i == 'P' || i == 'Q' || i == 'R' || i == 'S')
    			a[i] = '7';
    		else if (i == 'T' || i == 'U' || i == 'V')
    			a[i] = '8';
    		else if (i == 'W' || i == 'X' || i == 'Y' || i == 'Z')
    			a[i] = '9';
    	}
    	
    	printf("In numeric form: ");
    	for (i = 0; i < 15; i++) {
    		printf("%c", &a[i]);
    	}
    		
    	return 0;
    }
    But this just prints out random garbage data. While compiling, it shows that there is something wrong with %c in the last printf. How do I fix this?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Actually it's not printing garbage, it's trying to print what you've told it to print. You're compiler should be able to warn you about the issue:
    main.c|34|warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]|
    Since you have an ampersand in front of your variable the printf() is trying to print the address of the variable, not the actual value the variable holds.

    Jim

    Comments on this post

    • arman.khandaker agrees
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    1
    Originally Posted by jimblumberg
    Actually it's not printing garbage, it's trying to print what you've told it to print. You're compiler should be able to warn you about the issue:


    Since you have an ampersand in front of your variable the printf() is trying to print the address of the variable, not the actual value the variable holds.

    Jim
    Okay that problem is solved. It doesn't print the address of the variable anymore. But it just prints back whatever was entered. It doesn't replace the characters with digits.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    236
    Rep Power
    28
    Surely, since you compare the characters with 'i' instead of 'a[i]'.

    By the way, why you still did not learn the usage of debugger? It is crucial for anyone wishing to master programming. And it will help you answer many similar questions yourself ;)

    Comments on this post

    • arman.khandaker agrees
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    1
    Originally Posted by rodiongork
    Surely, since you compare the characters with 'i' instead of 'a[i]'.

    By the way, why you still did not learn the usage of debugger? It is crucial for anyone wishing to master programming. And it will help you answer many similar questions yourself ;)
    Wow thanks! It was a stupid mistake. And the problem wasn't detected by the built-in debugger of Dev-C++.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    1
    I take a break for a few days, and my learned skills go history! -_-

IMN logo majestic logo threadwatch logo seochat tools logo