| | |
Array and File Help
![]() |
•
•
Join Date: Oct 2005
Posts: 2
Reputation:
Solved Threads: 0
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:
Code I have so far:
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.
Examples of txt file:
C++ Syntax (Toggle Plain Text)
Students 3 Girls 4 Boys
Code I have so far:
C++ Syntax (Toggle Plain Text)
getline(inFile,row); //read each line in file while(!inFile.eof()) { list[count] = row; //store each line in array count ++; getline(inFile,row); }
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)
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.
C++ Syntax (Toggle Plain Text)
for (i = 0; i < num; i++) { if(list[i] != "3") { cout << i << ":" << list[i]<<endl; } }
To print out all the lines, I'd suggest a simple loop:
C++ Syntax (Toggle Plain Text)
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?
"Technological progress is like an axe in the hands of a pathological criminal."
![]() |
Similar Threads
- Trying to creating an array from a text file (C++)
- Help with a 2D array from a text file (C++)
- How to load a JPEG image file, store it in array and then save it (Visual Basic 4 / 5 / 6)
- save ARRay elements to a file (C++)
- Homework: filling array from text file (VB.NET)
- Getting an array from a txt file (C)
- Reading an array from a file (Java)
Other Threads in the C++ Forum
- Previous Thread: char+int?????
- Next Thread: help with my C++ program.
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ char class classes code coding compaitibility compile console conversion count delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error factorial file floatingpoint forms fstream function functions game givemetehcodez graph guessing gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix maze memory multiple net news node oop output parameter payment pointer problem program programming project projectassignmenthelp protection python random rank read recursion reference rpg skills string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






