I am truly LOST. I would love some help.

C

c++dummy

I got this project for my class and I'm totally lost as to how to copy
the 1d array with the bone name into a 2d array using this supposed
strncpy function I'm supposed to create. I believe the teacher wants us
to make the function and not use the library function.

Here's the info:

Write a C++ program that


Opens the file for input using the ifstream facility (see example
program from class).
Declares a two dimensional character array to store the bones from the
input file.
Reads each line of the file and stores the bone in the array position
indicated by the number at the start of the line (e.g., toe is the
first element, foot is the second, etc.; remember that C++ array
indices start with 0). As you process the file, read each bone into a
separate character array and then use the strncpy() function you will
write (see below) to copy the bone name into the main array.
Loops through the main bone array and builds up a long character string
saying which bone is connected to which (e.g., "The toe bone is
connected to the foot bone\nThe foot bone is connected to the ....").
The string should contain a newline character separating each statement
of connectedness. To do this, use the strcat() function you will write
(see below).
Prints the long character string of connectedness to the console.
Your program will use following three functions that you will write.
These functions should use pointer arithmetic rather than array
notation.

void strncpy(char *dest, char *src, int numchr);
Copies the string pointed to by src to the character array pointed to
by dest. numchr is the number of characters to copy. After the copy
operation, dest should be null terminated.

int strlen(char *str)
returns the number of characters in the null-terminated string pointed
to by str.

void strcat(char *dest, char *src)
appends the null-terminated string pointed to by src to the
null-terminated string pointed to by dest. Moves dest's terminating
null to the end of the new string.

Hints:

When you call strncpy, you can use strlen to determine the length of
src
Make sure you make your character arrays big enough


AND HERE IS THE TEXT FILE CONTENT BEING READ:

2 foot
4 leg
10 head
3 ankle
6 thigh
7 hip
1 toe
5 knee
8 back
9 neck
 
J

Jim Langston

c++dummy said:
I got this project for my class and I'm totally lost as to how to copy
the 1d array with the bone name into a 2d array using this supposed
strncpy function I'm supposed to create. I believe the teacher wants us
to make the function and not use the library function.

Here's the info:

Write a C++ program that


Opens the file for input using the ifstream facility (see example
program from class).
Declares a two dimensional character array to store the bones from the
input file.
Reads each line of the file and stores the bone in the array position
indicated by the number at the start of the line (e.g., toe is the
first element, foot is the second, etc.; remember that C++ array
indices start with 0). As you process the file, read each bone into a
separate character array and then use the strncpy() function you will
write (see below) to copy the bone name into the main array.
Loops through the main bone array and builds up a long character string
saying which bone is connected to which (e.g., "The toe bone is
connected to the foot bone\nThe foot bone is connected to the ....").
The string should contain a newline character separating each statement
of connectedness. To do this, use the strcat() function you will write
(see below).
Prints the long character string of connectedness to the console.
Your program will use following three functions that you will write.
These functions should use pointer arithmetic rather than array
notation.

void strncpy(char *dest, char *src, int numchr);
Copies the string pointed to by src to the character array pointed to
by dest. numchr is the number of characters to copy. After the copy
operation, dest should be null terminated.

int strlen(char *str)
returns the number of characters in the null-terminated string pointed
to by str.

void strcat(char *dest, char *src)
appends the null-terminated string pointed to by src to the
null-terminated string pointed to by dest. Moves dest's terminating
null to the end of the new string.

Hints:

When you call strncpy, you can use strlen to determine the length of
src
Make sure you make your character arrays big enough


AND HERE IS THE TEXT FILE CONTENT BEING READ:

2 foot
4 leg
10 head
3 ankle
6 thigh
7 hip
1 toe
5 knee
8 back
9 neck

A 2 dimentional character array would be like:
char Bones[9][50];

This is declaring 9 50 element character arrays.
Bones[0] is a char pointer to the first 50 element character array.

strncpy( Bones[0], whatever, whatever );
would copy whatever into the first char array.
 
C

c++dummy

Ahhh... ok. On a second look...now I see how this isn't so bad. I'm
pretty I just the library functions and go from there. I think I got it
now. Thanks for the help though.



Jim said:
c++dummy said:
I got this project for my class and I'm totally lost as to how to copy
the 1d array with the bone name into a 2d array using this supposed
strncpy function I'm supposed to create. I believe the teacher wants us
to make the function and not use the library function.

Here's the info:

Write a C++ program that


Opens the file for input using the ifstream facility (see example
program from class).
Declares a two dimensional character array to store the bones from the
input file.
Reads each line of the file and stores the bone in the array position
indicated by the number at the start of the line (e.g., toe is the
first element, foot is the second, etc.; remember that C++ array
indices start with 0). As you process the file, read each bone into a
separate character array and then use the strncpy() function you will
write (see below) to copy the bone name into the main array.
Loops through the main bone array and builds up a long character string
saying which bone is connected to which (e.g., "The toe bone is
connected to the foot bone\nThe foot bone is connected to the ....").
The string should contain a newline character separating each statement
of connectedness. To do this, use the strcat() function you will write
(see below).
Prints the long character string of connectedness to the console.
Your program will use following three functions that you will write.
These functions should use pointer arithmetic rather than array
notation.

void strncpy(char *dest, char *src, int numchr);
Copies the string pointed to by src to the character array pointed to
by dest. numchr is the number of characters to copy. After the copy
operation, dest should be null terminated.

int strlen(char *str)
returns the number of characters in the null-terminated string pointed
to by str.

void strcat(char *dest, char *src)
appends the null-terminated string pointed to by src to the
null-terminated string pointed to by dest. Moves dest's terminating
null to the end of the new string.

Hints:

When you call strncpy, you can use strlen to determine the length of
src
Make sure you make your character arrays big enough


AND HERE IS THE TEXT FILE CONTENT BEING READ:

2 foot
4 leg
10 head
3 ankle
6 thigh
7 hip
1 toe
5 knee
8 back
9 neck

A 2 dimentional character array would be like:
char Bones[9][50];

This is declaring 9 50 element character arrays.
Bones[0] is a char pointer to the first 50 element character array.

strncpy( Bones[0], whatever, whatever );
would copy whatever into the first char array.
 
D

David Harmon

On 10 Sep 2006 17:06:59 -0700 in comp.lang.c++, "c++dummy"
I got this project for my class and I'm totally lost as to how to copy
the 1d array with the bone name into a 2d array using this supposed
strncpy function I'm supposed to create.

A 2d array is just an 1d array who's elements are more 1d arrays.
(This is even more true in C++ than it is in general.)

So, if you have an array
char foo[10][10]

the one row of it, foo[x], is an array of char that can be passed as
an argument to your function expecting a 1d array of char.

To get the best help, Post The Code that you have written and ask
very specific questions about it. This issue is covered in Marshall
Cline's C++ FAQ. It is always good to check the FAQ before posting.
You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/

See the welcome message posted twice per week in comp.lang.c++ under
the subject "Welcome to comp.lang.c++! Read this first." or
available at http://www.slack.net/~shiva/welcome.txt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top