| | |
Array and File Help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
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 c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






