0

I am trying to create an array, with the data coming from a text file. I am then trying to count how many pieces of data there are in that array. In the array, there are two pieces of data, however when the counter executes, it says there is only 1.

Here is my code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
   int n = 0;
   ifstream show_version_library;
   show_version_library.open("version_library.txt");
   char show_version_library_var[1024];
   if (show_version_library.is_open())
   {
       while (!show_version_library.eof())
       {
          show_version_library >> show_version_library_var;
       }
   }

   // string replace * to ,
   string show_version_library_actual( show_version_library_var );
   int position = show_version_library_actual.find( "*" ); // find first space
   while ( position != string::npos ) 
   {
       show_version_library_actual.replace( position, 1, "," );
       position = show_version_library_actual.find( "*", position + 1 );
   }  

   // string replace ^ to "
   string show_version_library_actual2( show_version_library_actual );
   int position2 = show_version_library_actual2.find( "^" ); // find first space
   while ( position2 != string::npos ) 
   {
       show_version_library_actual2.replace( position, 1, "\"" );
       position2 = show_version_library_actual2.find( "^", position2 + 1 );
   } 

   // convert show_version_library_actual2 to char*  
   char* lib2;
   lib2 = &show_version_library_actual2[0];

   // array counter
   char* library_actual[100] = {
                               lib2
                               };

   char* p;
   while (p != '\0')
   {
       n++;
       p = library_actual[n];
   }

   cout << "\nN is " << n << " that was n\n"; // should be outputting 2
   show_version_library.close();

   system("PAUSE");
   return 0;
}

(code has been reformatted so that it will show up as code here)

And in version_library.txt

"1.8_HACK"*"1.8"*

I tried outputting "lib2" and it comes up with

"1.8_HACK", "1.8", 

.. as it should...

However, when I output library_actual[0], the whole line comes up, instead of just "1.8_HACK".

I am fairly new to cpp, so please excuse my terrible code..if any.

Thank you!

1 Answer 1

1

I think things start going wrong from this section:

   // convert show_version_library_actual2 to char*   
   char* lib2; 
   lib2 = &show_version_library_actual2[0]; 

That does not convert anything. What it does is declare a char pointer lib2 and sets it's value to the address of the first character in the show_version_library_actual2 char array. There's nothing wrong with that but no conversion is taking place.

The sequence:

   // array counter 
   char* library_actual[100] = { 
                               lib2 
                               }; 

does not count anything. What it does is setup an array of 100 pointers to char. You initialize it with only 1 value -- the pointer to the first character in the show_version_library_actual2 character array. The other 99 values in the array are uninitalized.

The next loop:

   char* p; 
   while (p != '\0') 
   { 
       n++; 
       p = library_actual[n]; 
   } 

probably isn't what you intended since it is dealing with a mostly uninitialized array. It returns 1 but only because library_actual[0] (lib2 = first character of the show_version_library_actual2 array) is non null so the loop ends.

I'm not sure exactly how you intended to identify the pieces of data. Maybe you thought that by adding commas that you'd get separate strings. If that's what you wanted you have to remember that in C/C++ nothing like that would happen by itself. Assignments are usually just that -- no conversion is going to happen as might happen in many other languages.

If you need to get separate strings you'll need to do that. If you simply want to count the number of times a comma occurs that's fairly easy. For example you could do:

char *p = lib2;
int n = 0;
while(*p != '\0')
{
    if(*p == ',')
        ++n;
}

With this n will represent the number of comma. In your sample that would give you 2. I'll leave it as an exercise to you to handle the case where there's 2 items but separated by 1 comma. Hint: sometimes it's useful to add an initial or terminal separator to make processing consistent.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.