Array and File Help

Reply

Join Date: Oct 2005
Posts: 2
Reputation: Laughs Galore is an unknown quantity at this point 
Solved Threads: 0
Laughs Galore Laughs Galore is offline Offline
Newbie Poster

Array and File Help

 
0
  #1
Feb 25th, 2007
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:
  1. Students
  2. 3 Girls
  3. 4 Boys

Code I have so far:
  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.

  1. for (i = 0; i < num; i++)
  2. {
  3. if(list[i] != "3")
  4. {
  5. cout << i << ":" << list[i]<<endl;
  6. }
  7. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 331
Moderator
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Array and File Help

 
0
  #2
Feb 25th, 2007
Originally Posted by Laughs Galore View Post
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.

  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:
  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?
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC