943,735 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2039
  • C++ RSS
Feb 25th, 2007
0

Array and File Help

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  1. Students
  2. 3 Girls
  3. 4 Boys

Code I have so far:
C++ Syntax (Toggle Plain Text)
  1. getline(inFile,row); //read each line in file
  2.  
  3. while(!inFile.eof())
  4. {
  5.  
  6. list[count] = row; //store each line in array
  7.  
  8. count ++;
  9.  
  10. getline(inFile,row);
  11.  
  12. }
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.

C++ Syntax (Toggle Plain Text)
  1. for (i = 0; i < num; i++)
  2. {
  3. if(list[i] != "3")
  4. {
  5. cout << i << ":" << list[i]<<endl;
  6. }
  7. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Laughs Galore is offline Offline
2 posts
since Oct 2005
Feb 25th, 2007
0

Re: Array and File Help

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.

C++ Syntax (Toggle Plain Text)
  1. for (i = 0; i < num; i++)
  2. {
  3. if(list[i] != "3")
  4. {
  5. cout << i << ":" << list[i]<<endl;
  6. }
  7. }
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:
C++ Syntax (Toggle Plain Text)
  1. for (int i=0; i<num; i++)
  2. 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?
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: char+int?????
Next Thread in C++ Forum Timeline: help with my C++ program.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC