1

I'm writing a program that is supposed to take in a list of names from the user, store them in an array, and then search through the list to check and see if the next name the user enters is part of the original list of names.

The issue I'm having is that when I go to enter a list of names, it only saves the last name entered into the list. Here is the part of code where I have problem:

#include<stdio.h>
#include<conio.h>
#include<string.h>

#define MAX_NAMELENGTH 10
#define MAX_NAMES 5
void initialize(char names[MAX_NAMES][MAX_NAMELENGTH]);

int main()
{
    char names[MAX_NAMES][MAX_NAMELENGTH];

    initialize(names);
    getch();
    return 0;
}

void initialize(char names[MAX_NAMES][MAX_NAMELENGTH])
{
    int i,Number_entrys;

    printf("How many names would you like to enter to the list?");
    scanf("%d",&Number_entrys);

    if (Number_entrys>MAX_NAMES) {
       printf("Please choose a smaller entry");
    }   
    else {
        for (i=0; i<Number_entrys; i++){
            scanf("%s",names[i]);
        }   
    }   

    printf("%s",names); 
}

3 Answers 3

4

That should read scanf("%s",names[i]);

Right now, you are storing it as scanf("%s",names);, which is equivalent to scanf("%s",names[0]);

So, you are overwriting that same array entry in every pass.

EDIT: Also, when you pass char names[][] to a function, it only passes the pointer to the first element. You should declare atleast one bound of it, to the same value as the one to which you declared it.

int main(){
    //To accept 2 names of 2 characters each
    char names[2][2];// or char** names;
    initialize(names, 2,2);
}
void initialize(char names[][2],const int MAX_NAMES,const int MAX_NAMELENGTH){ .. }
                             ^ syntax error if index not present

(Reference)

Sign up to request clarification or add additional context in comments.

12 Comments

See I knew this was probably the right answer, but every time I try implementing that i get an error saying "invalid use of array with unspecified bounds". I figured that the way you stated was logical, but I couldn't figure out that error so I started second guessing myself.
You also need to pass one dimension, when passing char names[][]. Try char names[][1] in your function. See edited answer.
thats interesting, had no idea that was something that could effect it. fixed the error, but now it outputs the first letter of the first name entered, followed by the entire name of the next name entered. Oh the joys of programming lol.
The first letter is because you used upper bound of 1. Replace the 1 with MAX_NAMELENGTH's value. char names[][MAX_NAMELENGTH]
See edited answer. I have mentioned that the 2 is //To accept 2 names of 2 characters each. You'll have to change it to the number of char you want to accept.
|
2

You should store the name into a specific entry in the array:

scanf("%s", names[i]);
printf("%s\n", names[i]);

also few generic issues:

  • capital names like MAX_NAMES are used in most cases for definitions and not for variables
  • scanf is not a safe function as you cannot limit the amount of chars it reads and writes

1 Comment

interesting, I didn't know that. Thank you for the information, ill keep this in mind for the future.
0

To check your entries so far, you need to loop from 0 to i-1 (after reading into names[i]) and check each one against the most recent.

To compare strings you can use strcmp:

if( strcmp(names[i], names[j]) == 0 ) {
    /* Duplicate name - reboot universe */
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.