| | |
Read number in txt file into an Array
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 20
Reputation:
Solved Threads: 0
Hi,
I'm a newbie in C++ and i'm working on a project, but it doesn't work..
The C++ program should read numbers into an array, the numbers are in a txt file.
Each array number : ex. array[1], array[2], array[3]... should get a number from the txt file.
In the file, for each new number, a new line is started.
Can someone help me with this ?
Sven
I'm a newbie in C++ and i'm working on a project, but it doesn't work..
The C++ program should read numbers into an array, the numbers are in a txt file.
Each array number : ex. array[1], array[2], array[3]... should get a number from the txt file.
In the file, for each new number, a new line is started.
Can someone help me with this ?
Sven
•
•
Join Date: Jul 2006
Posts: 88
Reputation:
Solved Threads: 2
What, exactly, doesn't work? The input portion?
Once you input the data, have you outputted it to check if it is wrong or right?
Perhaps you could post the code you have already written, to give us something to work with.
Once you input the data, have you outputted it to check if it is wrong or right?
Perhaps you could post the code you have already written, to give us something to work with.
•
•
Join Date: Feb 2008
Posts: 630
Reputation:
Solved Threads: 46
nope, if you make an array of length 5
then you have to use elements 0 to 4
C++ Syntax (Toggle Plain Text)
int test[5];
then you have to use elements 0 to 4
C++ Syntax (Toggle Plain Text)
for (i=0; i<5; i++)
•
•
Join Date: Dec 2008
Posts: 20
Reputation:
Solved Threads: 0
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; void drukintro (); void geef_rij( int& aantal, int getallen[]); void kies_bestand (int aantal, int rij[]); void lees_bestand (char bestandnaam[], int aantal, int bestandrij[]); void sorteerint(int aantal, int rij[]); void printintrij(int aantal, int rij[]); void drukintro () { int i; cout << "Dit is een programma dat een rij van getallen sorteert" << endl << "en het voorkomen van elk getal telt," << endl << "en daarna van hoog naar laag op het scherm drukt." << endl; for ( i=0; i<=54; i++) { cout << "-"; } cout << endl << endl; } void geef_rij( int& aantal, int getallen[]) { int i, eigen_getal; cout << "How many numbers do you want to enter ?" << endl; cin >> aantal; for (i=0; i<aantal; i++) { cin >> eigen_getal; getallen [i]= eigen_getal; } } void kies_bestand (int aantal, int rij[]) { int keuze_lijst; cout << "Which list do you want to use ?" << endl << "For list 1, type 1" << endl << "For list 2, type 2" << endl << "For list 3, type 3" << endl << endl; cout << "Keuze : "; cin >> keuze_lijst; if (keuze_lijst < 1 || keuze_lijst > 3 ) { cout << "This list doesn't exist !" << endl; exit(1); } if (keuze_lijst == 1) { lees_bestand ("lijst1.txt", aantal, rij); } if (keuze_lijst == 2) { lees_bestand ("lijst2.txt", aantal, rij); } if (keuze_lijst == 3) { lees_bestand ("lijst3.txt", aantal, rij); } } void lees_bestand (char bestandnaam[], int aantal, int bestandrij[]) { ifstream bestand; int i=0; bestand.open(bestandnaam); if ( ! bestand.fail() ) { do { bestand >> bestandrij[i]; i++; }while (i<aantal); bestand.close(); } else if (bestand.fail()) { cout << "Opening of txt file failed ! \n"; exit(1); } } void sorteerint(int aantal, int rij[]) { int i,j,temp; for (i=0; i<aantal-1; i++) for (j=aantal-1; j>i; j--) if (rij[j-1]>rij[j]) { temp=rij[j]; rij[j]=rij[j-1]; rij[j-1]=temp; } } void printintrij(int aantal, int rij[]) { int i; for (i=0; i<aantal; i++) cout << rij[i] << " "; cout << endl; } int main () { int eigen_rij[50], aantal_getallen; char antwoord; drukintro (); cout << "Do you want to give in an array of whole numbers yourself" << endl << "or read from an array of whole numbers from a file ? (Y / F )" << endl; cin >> antwoord; cout << endl; if ( antwoord == 'Y' || antwoord == 'y' ) { geef_rij ( aantal_getallen, eigen_rij ); cout << endl; } if ( antwoord == 'F' || antwoord == 'f' ) { kies_bestand( aantal_getallen, eigen_rij ); cout << endl; } else { exit (1); } sorteerint ( aantal_getallen, eigen_rij ); cout << "Sorted array : "; printintrij( aantal_getallen, eigen_rij ); }
I've translated some of the lines of my original program, because there the text that you can read on the screen is Dutch, I'm from Belgium you know..
If you test it out... You'll see that the array doesn't read in one of the txt-files..
Last edited by Swemp; Jan 2nd, 2009 at 7:23 pm.
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
We can't test it out if you don't supply the input text files. Which text file doesn't read in?
•
•
Join Date: Dec 2008
Posts: 20
Reputation:
Solved Threads: 0
•
•
•
•
We can't test it out if you don't supply the input text files. Which text file doesn't read in?
list1 :
15
78
32
15
74
15
96
32
159
-1
96
20
25
45
38
95
18
20
98
159
78
15
list 2 :
58
78
12
13
95
147
258
369
159
753
58
95
95
14
78
62
32
14
159
842
24
75
62
314
789
15
78
321
8
6
7
list3:
45
78
97
12
14
18
32
0
-1
-25
87
26
17
26
78
45
12
18
45
65
32
-25
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
Don't assume that a run-time error means that the program isn't reading from the files. Place some cout statements inside the function that reads from the file that display the parameters passed to the function and display what is being read in. Make sure that the parameters are what you want them to be, and that what you want to read in is actually being stored how you want it to be stored, and that you are going through the loop the correct number of times.
Last edited by VernonDozier; Jan 2nd, 2009 at 9:12 pm. Reason: Improved grammar
![]() |
Similar Threads
- reading a txt file into a 2d array (C++)
- Input Unicode data in sequential data file (Visual Basic 4 / 5 / 6)
- Need Help to read .txt file and store its contents (Java)
- Array and File Help (C++)
- Dont know where to go next. (Java)
- Help Reading Info in Text File Into an Array (C++)
Other Threads in the C++ Forum
- Previous Thread: Automatic implicit type conversions
- Next Thread: Array using ** pointers?
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert 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 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 temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






