Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6787597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:23:07+00:00 2026-05-26T17:23:07+00:00

I am trying to create an array, with the data coming from a text

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:23:07+00:00Added an answer on May 26, 2026 at 5:23 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UITableView that was created from data coming from a mutable array.
I am trying the create a loop to extract data from an array created
I'm trying to create an array of arrays that will be using repeated data,
Im trying to create an array of tm* structs, and then return them at
I am trying to create an array or list that could handle in theory,
I'm trying to create an array of strings that can be randomized and limited
I'm trying to create an array in MIPS Assembly, and then add all the
I'm trying to create an array of structs and also a pointer to that
I'm using VB6 and trying to create a DAO recordset from an array of
i am trying to create array from window.location.hash variable but i am failling. My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.