i'm looking to get a first name followed by four scores and finally a last name, stored into multiple arrays (a char *array, a float array, & a char *array). getting the scores i understand however the names are pointer arrays. as far that goes what am i doing wrong here
You can find the length of the whole name string (there's a string function for this). Using a loop, go backwards from that last position till you find a blank space. The index of that space is the length of the first name (plus middle name, if present). One past that is the index of the first char of the last name. Consider ...
Assuming you have a declaration for char * newptr; your handling of the pointer arrays looks correct, as far as that goes. There are other problems or questions:
First clarify, is the data in the form you show in example ( fn ln s1 s2 s3 s4) or as in your description ( fn s1 s2 s3 s4 ln ) ? That makes a difference.
Second, reading in first and last names separately will cause problems when there's a middle initial or multiple first or last names (Billy Bob Smith, Sara Jones Carter). As you have the code now, all you read in is a first name, then try to read the scores, which is going to cause input failure.
Third, you can cut out a bit of redundancy by allocating memory directly to the array of pointers, as in FNs[i] = new char; strcpy(FNs[i], Name); What does the assignment actually say about handling the names? Are there any qualifications of the types of names, or assurances of only two name parts?
Breaking up the name is now a bit simpler, but still needs some work.
My approach is to read the whole name in at one go to a temporary string using getline( ). Then work backwards from the end to find the space that marks the beginning of the last name. (and the ending of the first name middle initial). Copy the firstname portion - strncpy or strncat will work, since you know how much to copy. Then you can copy the last name in several ways.
1 - simply loop from where last name start to end of last name, storing char by char to the last name array element.
2 - use strcpy/strcat using the address of the first character of the last name as the source string. Example:
char tempname = "John Q. Public";
char fname = "";
char lname = "";
//find index of the space between Q. and Public, store as start
strncat( fname, tempname, start );
strcat( lname, &tempname[start+1] );
And I'm sure someone else has other more elegant ways, but I think this is clear for the beginner.
understood but a q: i'm assuming i could use that to get the first name (and a initial should it contain one) if i have the number of characters the last name takes up, so how do i work backwards to figure out the length of the last name? let alone separate the first and last name from each other
You can find the length of the whole name string (there's a string function for this). Using a loop, go backwards from that last position till you find a blank space. The index of that space is the length of the first name (plus middle name, if present). One past that is the index of the first char of the last name. Consider
char wholename = "John Doe";
J o h n D o e \0 content
0 1 2 3 4 5 6 7 8 9 10 11.....19 indexes
length of string is 8, which is also the index of the NULL terminator
start loop at 7, go backwards till you find the blank at index 4.
So, first name is of length 4. The string containing the last name starts at
&wholename (address of the element at index 5.) String functions will think the string starts there, and still ends at the NULL terminator.
for the strncat to work, you must set the FN to an empty string. Simply allocating the memory does not do that.
FNs[i] = new char;
strcpy( FNs[i], "" );
In looking for the space before the last name, start you loop at namelength-1. The value that strlen returns is the index of the '\0', so your loop would set index right away. And, you really want to look for the blank space, not the NULL. They are not the same thing.
In copying the characters of the last name, you are simply copying array elements. Don't use a string function. What you have should not even be compiling.
The loop looking for the space must stop when it finds the space. A for loop is not the best choice here. A while loop will stop just when you find the space
int j = namelength-1;
while( !isspace(wholename[j] )&& j >= 0 )
//add some test for j < 0, in case there's no space at all!
strncat(FNs[i], wholename, index);
LNs[i] = new char;
for(k=0; k!=namelength-index; k++)
}while( wholename[index-1] != '\0' ); //loops till NULL copied
it goes through the first time around fine but on coming back to the top and reading a new name into it something goes wrong. SENTINEL being defined as "more" but that's not the problem because it is entering the while loop after that statement. it's just apparently reading in something wrong (or nothing at all, because strlen=0). it just stops (messes up) a few lines after it enters..
I'm not sure without seeing more code. The basis of your loop should work, as this little demo shows:
using namespace std;
char s = "";
int i = 0;
char sentinel = "stop";
while( i < 50 && cin.getline( s, 25 ) && (!strstr( s, sentinel ) ))
cout << s << endl;
The one situation I find it behaves badly is if there's an EOF condition in the same line as the input with the sentinel value, in the case of a file that would be the last line not having a terminal endline. That would not explain it reentering the loop and giving bad results. Please post a program with this problem, and a sample of the data.