I am writing this program that reads in lines from a txt file into an array. Some lines start with a number and some don't. Problem is that I need help on how to display the lines that don't start with a number.

Examples of txt file:

Students
3 Girls
4 Boys

Code I have so far:

getline(inFile,row); //read each line in file

while(!inFile.eof())
{
         
          list[count] = row; //store each line in array
 
          count ++;
          
         getline(inFile,row);
         
}

I just don't know how to get each line that doesn't start with a number from the array and display it.

I have tried something like the below, but I think that it doesn't work since each line that has a number, stores whatever follows that number.

for (i = 0; i < num; i++)
{
    if(list[i] != "3")
    {
    cout << i << ":" << list[i]<<endl;
    }
}

I have tried something like the below, but I think that it doesn't work since each line that has a number, stores whatever follows that number.

for (i = 0; i < num; i++)
{
    if(list[i] != "3")
    {
    cout << i << ":" << list[i]<<endl;
    }
}

I don't quite get why that would work even if all the lines have numbers, because getline() retrieves the full line, including the text after the number in your case. Comparing that entire line with a single number isn't going to work, whether the line has a number in front of it or not.

To print out all the lines, I'd suggest a simple loop:

for (int i=0; i<num; i++)
    cout << i << ":" << list[i] << endl;

Now all you need to do is remove line numbers on the lines that do happen to have numbers.

For removing the number, I'd suggest using string::find_first_not_of with a single space as an argument. Now check that the character found is not a number with isalpha, and then you can proceed either by removing the number if it is one, or simply leaving the string as-is.

Or did I totally misunderstand what you're trying to do?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.