1

I'm writing a program to display names and the number of apartments however, my array storing the names is unable to display the names saying that they're unidentified. Is there anyway to have the string contained in the array to display? Also, I seem to get the value of n displaying beneath the number of apartments in the display, is there anyway to get rid of this? Here's my code:

#include <stdio.h>

int main(void)
{
    int i;
    char name[] = {North, West, South, East};
    int apt[] = {24, 30, 14, 18};
    const int n = 5;

    printf("Name    No. of Apartments\n");
    for (i = 0; i < n; i++)
            printf("%c        %d\n", name[i],  apt[i]);

    return 0;

}
Jinto
  • 179
  • 1
  • 3
  • 17
  • Your code doesn't even compile at all. The issues you are describing don't seem to correlate with the code you've posted. `char name[] = {North, West, South, East};` is totally invalid, for starters - you didn't even put the strings in quotes, let alone define the array correctly. You want `char * name[] = {"North", "West", "South", "East"}` – Random Davis Feb 16 '17 at 19:46
  • See if http://stackoverflow.com/questions/9907160/how-to-convert-enum-names-to-string-in-c is good for you – chux - Reinstate Monica Feb 16 '17 at 19:46
  • Try putting the names in double-quotes, `"North", ...` – AntonH Feb 16 '17 at 19:47
  • I added quotes and now I get 'excess elements in char array initializer'. Also, the code does not compile because of the string array. Without it, it compiles fine. – Jinto Feb 16 '17 at 19:50
  • You're using `%c` in your `printf`, which is for single characters, not strings. You're also going through the loop 5 times when there are 4 elements, so set `n` to 4, not 5. – AntonH Feb 16 '17 at 19:52
  • When I run it in a text editor I see where the issue is. It seems only one letter is being output per line. So it looks like: S O U T H – Jinto Feb 16 '17 at 19:53
  • I tried %c since %s wasn't working, but it seems to yield the same issue. – Jinto Feb 16 '17 at 19:55

3 Answers3

2

Here is your code, corrected:

#include <stdio.h>

int main(void)
{
    int i;
    char *name[] = {"North", "West", "South", "East"}; /* You're declaring an array of characters, you need an array of strings/pointers */
    int apt[] = {24, 30, 14, 18};
    const int n = 4; /* you have 4 elements in your array, not 5 */

    printf("Name    No. of Apartments\n");
    for (i = 0; i < n; i++)
        printf("%s %d\n", name[i],  apt[i]); /* %c is for characters, you need %s for strings */

    return 0;
}
AntonH
  • 6,359
  • 2
  • 30
  • 40
1

You are declaring a name as a one-dimensional array, when it needs to be a two-dimensional array.

char name[number of names][length of longest name + 1]

Additionally, the strings used for directional names need to be encapsulated in double quotes. So your declaration should look something like this:

char name[4][6] = {"North", "West", "South", "East"};

When printing a character array, use the designator %s. %c is only used for single characters:

printf("%s %d\n", name[i], apt[i]);

Additionally, since the for loop starts at index 0, 'n' should be changed from 5 to 4:

const int n = 4;

  • I didn't think to use a two-dimensional array, since I've only learned of one-dimensional arrays. Clear and concise, thank you for your help. – Jinto Feb 16 '17 at 20:04
1

What appears from your question is that you are new to the concept of strings in C. So,you need to know about the array of pointers.

Solution

#include <stdio.h>

int main(void) {
    // your code goes here
     int i;

    char *name[4];//Array of pointers to store 2d array of characters.
    name[0]="North";name[1]="West";name[2]="South";name[3]="East";
    int apt[] = {24, 30, 14, 18};
    const int n = 5;
    printf("Name    No. of Apartments\n");
    for (i = 0; i < n; i++)
            printf("%s        %d\n", name[i],  apt[i]);

    return 0;
}

And since number of strings in your array is 4 only,so you shouldn't run the loop for n=5 times,it will produce some garbage value for i=4.

asad_hussain
  • 1,959
  • 1
  • 17
  • 27