| | |
Array and File Help
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
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.
Views: 1697 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






