so im suppose to open a file, create a char array and use getline to read lines from the file and put it in the array. Then im suppose to create a function that will count the character in a char array and shouldnt include the null character, return in to the main function , create a third function to reverse the lines.
i messed up by testing out only one line of words on my file instead of multiple lines and now it doesnt count the characters correctly. can someone help me. ill gladly specify more if anyone is still confused. thanks
I tried 2 different files. One containing "TarikNeaj" and one containing "I Love McAndCheese"
First output was 9, which is correct. Second was 18, which is also correct. Don't forget white space is a character.
What's not 100% correct however is the reverseChar. It does everything correct minus the fact that it doesn't print out the first character. So TarikNeaj becomes jaeNkira, not printing out the "T".
Well. If you want to read multiple lines you're gonna have to create a variable that sums up all the number of characters, right now they're just overwriting themselves only counting the last line.
1 2 3 4 5 6 7 8 9 10 11
int numberofchar;
int motherOfAllChars = 0;
while (file.getline(array, VAL))
{
numberofchar = charCounter(array, VAL);
motherOfAllChars = motherOfAllChars + numberofchar;
} // end of while loop
cout << " " << endl;
cout << motherOfAllChars << endl;
thanks that worked, but the reverse function only displays a bunch of junk followed by the reversal of different words of each line if the file had 4 lines for example
I'll give you a few tips on how to fix that. Your problem is with the way you're reading in the lines.
To begin with. The problem is similar to the other one, overwriting.
If the file looks like this:
Tarik Neaj
Hello
After the first loop, array will contain "Tarik Neaj\o". After the second, it will contain "Hello\0Neaj\0".
So, when you read in the array, you're gonna need a variable which changes the location on where it should place the next line, if that makes sense.
1 2 3
int location = 0;
while (file.getline(array + location, VAL ))
{
First line will be read normally, and will be placed beginning at array[0]. To avoid over writing, you need to give location a value so that it begins where "Tarik Neaj" ends. Figure that out =)
Then motherOfAllChars