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: Swemp is an unknown quantity at this point 
Solved Threads: 0
Swemp Swemp is offline Offline
Newbie Poster

Read number in txt file into an Array

 
0
  #1
Jan 2nd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 18
Reputation: da penguin is an unknown quantity at this point 
Solved Threads: 5
da penguin's Avatar
da penguin da penguin is offline Offline
Newbie Poster

Re: Read number in txt file into an Array

 
0
  #2
Jan 2nd, 2009
fstream header will do. just include <ifstream>
then do
ifstream vname("filename");
vname >> array[i]; as many times as needed
No, ma'am, we are musicians.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 630
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Read number in txt file into an Array

 
0
  #3
Jan 2nd, 2009
Note that c++ arrays are 0 based. That is, array[0] is the first element, not array[1].

Dave
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 88
Reputation: DavidB is an unknown quantity at this point 
Solved Threads: 2
DavidB DavidB is offline Offline
Junior Poster in Training

Re: Read number in txt file into an Array

 
0
  #4
Jan 2nd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 20
Reputation: Swemp is an unknown quantity at this point 
Solved Threads: 0
Swemp Swemp is offline Offline
Newbie Poster

Re: Read number in txt file into an Array

 
0
  #5
Jan 2nd, 2009
Originally Posted by daviddoria View Post
Note that c++ arrays are 0 based. That is, array[0] is the first element, not array[1].

Dave
I know that, but I'm using a for loop..
for (i=1; i<=number; i++)
and array[i]

Or isn't that correct ?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 630
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Read number in txt file into an Array

 
0
  #6
Jan 2nd, 2009
nope, if you make an array of length 5

  1. int test[5];

then you have to use elements 0 to 4
  1. for (i=0; i<5; i++)
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 20
Reputation: Swemp is an unknown quantity at this point 
Solved Threads: 0
Swemp Swemp is offline Offline
Newbie Poster

Re: Read number in txt file into an Array

 
0
  #7
Jan 2nd, 2009
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. void drukintro ();
  7. void geef_rij( int& aantal, int getallen[]);
  8. void kies_bestand (int aantal, int rij[]);
  9. void lees_bestand (char bestandnaam[], int aantal, int bestandrij[]);
  10. void sorteerint(int aantal, int rij[]);
  11. void printintrij(int aantal, int rij[]);
  12.  
  13. void drukintro ()
  14. {
  15. int i;
  16. cout << "Dit is een programma dat een rij van getallen sorteert" << endl
  17. << "en het voorkomen van elk getal telt," << endl
  18. << "en daarna van hoog naar laag op het scherm drukt." << endl;
  19. for ( i=0; i<=54; i++)
  20. {
  21. cout << "-";
  22. }
  23. cout << endl << endl;
  24.  
  25. }
  26.  
  27. void geef_rij( int& aantal, int getallen[])
  28. {
  29. int i, eigen_getal;
  30. cout << "How many numbers do you want to enter ?" << endl;
  31. cin >> aantal;
  32.  
  33. for (i=0; i<aantal; i++)
  34. {
  35. cin >> eigen_getal;
  36. getallen [i]= eigen_getal;
  37. }
  38. }
  39.  
  40. void kies_bestand (int aantal, int rij[])
  41. {
  42. int keuze_lijst;
  43.  
  44. cout << "Which list do you want to use ?" << endl
  45. << "For list 1, type 1" << endl
  46. << "For list 2, type 2" << endl
  47. << "For list 3, type 3" << endl << endl;
  48. cout << "Keuze : ";
  49. cin >> keuze_lijst;
  50.  
  51. if (keuze_lijst < 1 || keuze_lijst > 3 )
  52. {
  53. cout << "This list doesn't exist !" << endl;
  54. exit(1);
  55. }
  56.  
  57. if (keuze_lijst == 1)
  58. {
  59. lees_bestand ("lijst1.txt", aantal, rij);
  60. }
  61.  
  62. if (keuze_lijst == 2)
  63. {
  64. lees_bestand ("lijst2.txt", aantal, rij);
  65. }
  66.  
  67. if (keuze_lijst == 3)
  68. {
  69. lees_bestand ("lijst3.txt", aantal, rij);
  70. }
  71. }
  72.  
  73. void lees_bestand (char bestandnaam[], int aantal, int bestandrij[])
  74. {
  75. ifstream bestand;
  76. int i=0;
  77.  
  78. bestand.open(bestandnaam);
  79. if ( ! bestand.fail() )
  80. {
  81. do
  82. {
  83. bestand >> bestandrij[i];
  84. i++;
  85. }while (i<aantal);
  86. bestand.close();
  87. }
  88.  
  89. else if (bestand.fail())
  90. {
  91. cout << "Opening of txt file failed ! \n";
  92. exit(1);
  93. }
  94. }
  95.  
  96.  
  97. void sorteerint(int aantal, int rij[])
  98. {
  99. int i,j,temp;
  100. for (i=0; i<aantal-1; i++)
  101. for (j=aantal-1; j>i; j--)
  102. if (rij[j-1]>rij[j])
  103. {
  104. temp=rij[j];
  105. rij[j]=rij[j-1];
  106. rij[j-1]=temp;
  107. }
  108. }
  109.  
  110. void printintrij(int aantal, int rij[])
  111. {
  112. int i;
  113. for (i=0; i<aantal; i++)
  114. cout << rij[i] << " ";
  115. cout << endl;
  116. }
  117.  
  118.  
  119. int main ()
  120. {
  121. int eigen_rij[50], aantal_getallen;
  122. char antwoord;
  123.  
  124. drukintro ();
  125. cout << "Do you want to give in an array of whole numbers yourself" << endl
  126. << "or read from an array of whole numbers from a file ? (Y / F )" << endl;
  127. cin >> antwoord;
  128. cout << endl;
  129.  
  130. if ( antwoord == 'Y' || antwoord == 'y' )
  131. {
  132. geef_rij ( aantal_getallen, eigen_rij );
  133. cout << endl;
  134. }
  135.  
  136. if ( antwoord == 'F' || antwoord == 'f' )
  137. {
  138. kies_bestand( aantal_getallen, eigen_rij );
  139. cout << endl;
  140. }
  141.  
  142. else
  143. {
  144. exit (1);
  145. }
  146.  
  147. sorteerint ( aantal_getallen, eigen_rij );
  148. cout << "Sorted array : ";
  149. printintrij( aantal_getallen, eigen_rij );
  150. }

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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,820
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Read number in txt file into an Array

 
0
  #8
Jan 2nd, 2009
Originally Posted by Swemp View Post
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..
We can't test it out if you don't supply the input text files. Which text file doesn't read in?
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 20
Reputation: Swemp is an unknown quantity at this point 
Solved Threads: 0
Swemp Swemp is offline Offline
Newbie Poster

Re: Read number in txt file into an Array

 
0
  #9
Jan 2nd, 2009
Originally Posted by VernonDozier View Post
We can't test it out if you don't supply the input text files. Which text file doesn't read in?
It doesn't read any of them..

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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,820
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Read number in txt file into an Array

 
0
  #10
Jan 2nd, 2009
Originally Posted by Swemp View Post
It doesn't read any of them..

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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC