Remove * from the second line, you don't wanna create an array of pointers to characters, you want to create array of characters.
This is a string, you can't read it with a %c specifier, you're gonna need to use %s:

I'm working with strict implementation instructions. A 'backbone' was given to me for this code. I can't deviate from the code that was given.

That's fine, but then you must be aware that this means that the name member is an array of pointers to char, hence the expectation is that it is an array of strings, which does not quite make sense for a member named "name" rather than "names" or "name_list". Hence, in your shoes I would clarify the requirements.

By the way, the original poster's username is sahtopi, but your username is Joshua Green. Is there a reason for that?

Originally Posted by Bjarne Stroustrup (2000-10-14)

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

I'm working with strict implementation instructions. A 'backbone' was given to me for this code. I can't deviate from the code that was given.

Fine. You still need to know that each name[i] is a different string, and you need to allocate the ones you are going to use. And you are not doing that currently. This is all the reason why I think this particular implementation decision is an error.

Fine. You still need to know that each name[i] is a different string, and you need to allocate the ones you are going to use. And you are not doing that currently. This is all the reason why I think this particular implementation decision is an error.

I understand, thank you very much for the info. That definitely helps me understand what's going on here

That's fine, but then you must be aware that this means that the name member is an array of pointers to char, hence the expectation is that it is an array of strings, which does not quite make sense for a member named "name" rather than "names" or "name_list". Hence, in your shoes I would clarify the requirements.

By the way, the original poster's username is sahtopi, but your username is Joshua Green. Is there a reason for that?

Perhaps I'm not understanding, but the idea is the struct acts as a container that stores a song's (1)Name (2) rating (3) number of times played. The input file provides 3 different songs, each with their own rating and number of plays. Would:

char name[MAXNAMESIZE] be preferred to char* name[MAXNAMESIZE]?

edit: My reasoning would be that each song only has 1 name. For example, I would want to read the name "Hello" into "*name", the number "5" into "rating", and lastly the number 100 into "numTimesPlayed"

Perhaps I'm not understanding, but the idea is the struct acts as a container that stores a song's (1)Name (2) rating (3) number of times played. The input file provides 3 different songs, each with their own rating and number of plays. Would:

char name[MAXNAMESIZE] be preferred to char* name[MAXNAMESIZE]?

Not that it would be preferred: it is a matter of asking what do you want to store in each struct object. So, you have listed three things: a name would be a string, hence you would either have an array member:

Code:

char name[MAXNAMESIZE];

or a pointer member:

Code:

char *name;

but if you have a pointer member then you need to allocate memory for the string contents itself.

But let's say, hypothetically, that you not only want to store the song's name, but also aliases to the song's name, e.g., some songs are popularly known by alternative names because fans like some of the lyrics so much that they use the lyrics as the song's name, even though officially it isn't its name. Then, maybe you really would store a list of names, perhaps with the first name as the official name, and other names as the aliases, in which case a member like this is fine:

Code:

char *names[MAX_NUM_NAMES];

Last edited by laserlight; 02-02-2016 at 11:06 PM.

Originally Posted by Bjarne Stroustrup (2000-10-14)

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

but if you have a pointer member then you need to allocate memory for the string contents itself.

But let's say, hypothetically, that you not only want to store the song's name, but also aliases to the song's name, e.g., some songs are popularly known by alternative names because fans like some of the lyrics so much that they use the lyrics as the song's name, even though officially it isn't its name. Then, maybe you really would store a list of names, perhaps with the first name as the official name, and other names as the aliases, in which case a member like this is fine:

Code:

char *names[MAX_NUM_NAMES];

Exactly, so with my current implementation, isn't it correct? I defined MAXNAMESIZE as 20 in the program header, so when I stated char* name[MAXNAMESIZE] I have a pointer member with memory allocated for the string contents. Is this incorrect?

Here is my code as it stands currently. No compile errors, I'm just stuck.

Exactly, so with my current implementation, isn't it correct? I defined MAXNAMESIZE as 20 in the program header, so when I stated char* name[MAXNAMESIZE] I have a pointer member with memory allocated for the string contents. Is this incorrect?

It is incorrect. You wrote "My reasoning would be that each song only has 1 name."

Originally Posted by Bjarne Stroustrup (2000-10-14)

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

It is incorrect. You wrote "My reasoning would be that each song only has 1 name."

I understand that most people have their own (incorrect) names for songs. But the reality of the situation is that professionally produced music (i.e something you would purchase or listen to on the radio) has one song name, or multiple names, if so designated. If you were put in a contest to listen to a song and name it, you would be incorrect if you made up your own song title, or used some of the words in the song. Assume the same for this program. In the implementation of my program, I'm not accepting input of any song names other than the input file. Does that make sense? I need a program that reads in song names from an input file, not song names from the mind of a person who is inputting whatever they want as a song name.

edit: approach this situation as if the only 3 songs that exist in the world are "Hello", "Roses", and "Again".

Instead of squishing the macro name as MAXNAMESIZE, I renamed it to NAME_MAX_SIZE. Having NAME come first can be useful when you have many such macro names.

For the Song struct, I made name to be an array of NAME_MAX_SIZE char. No pointer, no additional allocation step required. However, you are indeed limited to a name of at most NAME_MAX_SIZE-1 length.

Instead of passing around an array of Song objects as a pointer along with a length, I combined both into an Album struct.

Note that you should free what you malloc when done.

Originally Posted by Bjarne Stroustrup (2000-10-14)

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

In the first line, you allocated memory for *lenPtr number of Song objects. Great. Then, you immediately begin the loop to read songs from the file. Fine. But in the loop body, you read a string into *album[i].name. album[i].name is an array of pointers. Therefore, *album[i].name is a pointer. This is fine, but the pointer does not point to anything!

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

In the first line, you allocated memory for *lenPtr number of Song objects. Great. Then, you immediately begin the loop to read songs from the file. Fine. But in the loop body, you read a string into *album[i].name. album[i].name is an array of pointers. Therefore, *album[i].name is a pointer. This is fine, but the pointer does not point to anything!