| | |
infile into array and sort
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Thanks to everyone for their assistance thus far. I have a bit of problem. When I try to compile the program...I get: Debug assertion failed (string subscript out of range)
So pretty much, the program was able to read the files from the infile..however since i inputted the loops I had those error messages. Below is my code:
What I'm simply trying to do is sort the names in normal alphabetical order and then display them to the screen and print printer in 5 columns.(refer to my initial post for a copy of the inFile)...Thx much
So pretty much, the program was able to read the files from the infile..however since i inputted the loops I had those error messages. Below is my code:
C++ Syntax (Toggle Plain Text)
#include <cstring> #include <iostream> #include <iomanip> #include <cmath> #include <fstream> #include <string> #include <ctime> using namespace std; int main() { ifstream inFile; ofstream outFile; inFile.open ("listnames.txt"); outFile.open ("outnames"); string names[120]; string name; int i, j, k; int count=120; while(getline(inFile,name)) { for (i=0;i<count-1;i++) for (j=i+1;j<count; j++) if (name[i]>name[j]) swap (name[i],name[j]); } for (k=0; k<120; k++) { if (i%5==0) cout<<endl; cout<<setw(2)<<name[i]; }//end for i inFile.close(); outFile.close(); return 0; }
Suggestion: don't attempt to sort the array on every iteration of the input loop. Read all the strings and sort only one time after the loop terminates.
>>int count=120;
initialize that variable to 0 instead of the max array size. Then insert the new string into the array at count element and increment it in anticipation for another string.
>>int count=120;
initialize that variable to 0 instead of the max array size. Then insert the new string into the array at count element and increment it in anticipation for another string.
C++ Syntax (Toggle Plain Text)
int count = 0; while(getline(inFile,name)) { names[count] = name; ++count; }
Last edited by Ancient Dragon; Sep 26th, 2007 at 6:26 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
don't check for integer length but check to make sure the program doesn't read in more strings than the array can hold, something like this
The above loop when either of the two conditions exist: either the value of count becomes 120 or when end-of-file is reached. After that loop terminates then you can code a bubble sort or some other sorting algorithm to sort the strings.
C++ Syntax (Toggle Plain Text)
int count = 0; while(count < 120 && getline(inFile,name)) { names[count] = name; ++count; }
The above loop when either of the two conditions exist: either the value of count becomes 120 or when end-of-file is reached. After that loop terminates then you can code a bubble sort or some other sorting algorithm to sort the strings.
Last edited by Ancient Dragon; Sep 26th, 2007 at 6:36 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
C++ Syntax (Toggle Plain Text)
#include <cstring> #include <iostream> #include <iomanip> #include <cmath> #include <fstream> #include <string> #include <ctime> using namespace std; int main() { ifstream inFile; ofstream outFile; inFile.open ("listnames.txt"); outFile.open ("outnames"); string names[120]; string name; int i, j, k; int count=0; while(count < 120 && getline(inFile,name)) { names[count] = name; ++count; } { for (i=0;i<count-1;i++) for (j=i+1;j<count; j++) if (names[i]>names[j]) swap (names[i],names[j]); } for (k=0; k<120; k++) { if (i%5==0) cout<<endl; cout<<setw(10)<<names[i]; } inFile.close(); outFile.close(); return 0; }
Thats what I have so far....however, i only the the colums for one of the lines of my inFile...in particular 'ZOVUROPA'....i copied the inFile into Microsoft Word and it located that particular line of letters @ line 120....i don't see what i'm doing wrong.....also, it doesn't print out in 5 columns as i wanted...it prints out in 7 instead....thx for the assistance.
Last edited by Ancient Dragon; Sep 27th, 2007 at 9:06 am. Reason: add line numbers to code tags
line 38 uses k as the loop counter, but lines 40 and 42 use a different variable. Either correct lines 40 and 42, or change line 38 to re-use i integer (preferred). Programs should attempt to reuse variables, especially one-letter loop counters, whenever possible so that the program has fewer variables and makes it easier to understand.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Sort A Multidimension Array (Python)
- Can't get array to sort. (Pascal and Delphi)
- Multidimensional array sort problem (C++)
- Could someone please help me with a bucket sort. (C)
- Input Output File Streams - Sorting Help With Array (C++)
- Working with array of files (C++)
- Cannot utilize an array properly (Java)
Other Threads in the C++ Forum
- Previous Thread: hi sir/ma'am canu help me??
- Next Thread: ":" other than inheritance?
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






