counting characters in char array

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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <fstream>

using namespace std;
char charCounter(char array[], const int);
void reverseChar(char array[], int);


int main()
{
   int x = 0;
   const int VAL = 100;
   char array[VAL];
   ifstream file;
   file.open("datEx.txt");


   if (!file)
   {
      cout << "File did not open." << endl;
      system("pause");
      return -1;
   }

   int numberofchar;
   while (file.getline(array, VAL))
   {
       numberofchar=charCounter(array,VAL);
      
     
   } // end of while loop
   cout << " " << endl;
   cout << numberofchar << endl;

   reverseChar(array, numberofchar);
   cout << " " << endl;
   



   file.close();
   system("pause");
   return 0;
}

char charCounter(char array[], const int)
{
   int x = 0;
   
   while (array[x] != '\0')
   {

      cout << array[x];
      x++;
      

   }

   return x;
}

void reverseChar(char array[], int numberofchar)
{
   int u;
   
      for (u = numberofchar - 1; u >= 0; u--)
      {
         cout << array[u];
      }
   

}
Last edited on
It counts the characters perfectly for me.

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".
i meant for a file that would have multiple lines like this:
how are you
doing today?
i am doing
pretty good

and yea the reverse part has that issue
Last edited on
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;
Last edited on
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
Last edited on
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
no luck :/
UTD?
yup
Same, I shouldn't have waited till the last minute for this homework. It's not as easy as the other ones.
exactly. im just going to turn it in like that, at least ill get partial credit
Topic archived. No new replies allowed.