944,209 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3916
  • C++ RSS
Oct 1st, 2006
0

strstream problem

Expand Post »
Ok, I have a small problem. I'm just trying to get my program to read a single line from a file at a time, and put the results in a vector.

The problem is at the end of each vector. It repeats the last element twice and also adds a 0. I'm not sure whats going on. I don't think the 0 is a newline, because my strstream never receives a '\n'. I also don't know why it repeats the last element. I posted my program plus my input and output at the end. Any help or ideas would be appreciated Thank you.

- Daniel


#include <iostream>
#include <fstream>
#include <strstream>
#include <string>
#include <vector>

void main() {
// To determine if the the list is smaller or equal to 8, calculate how
// many times youd have to cut the list to reach less than/equal to 8 BEFORE
// you go into the mergesort loop.

ifstream in("in.dat");
vector<float>numbers;
float myfloat;
char ch[256];


while (in) {
in.getline(ch, 256, '\n');
numbers.clear();
strstream myss;
myss << ch;

while(myss) {
myss >> myfloat;
numbers.push_back(myfloat);

}
for (int a = 0; a <= numbers.size(); a++)
cout << numbers[a] << ", ";
cout << endl;
}
}


My in.dat file looks like this:

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30

My output looks like this:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 0,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 20, 0,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 30, 0,
30, 22,
30, 22,
Last edited by nanodano; Oct 1st, 2006 at 3:42 pm.
Similar Threads
Reputation Points: 36
Solved Threads: 2
Junior Poster in Training
nanodano is offline Offline
78 posts
since Feb 2005
Oct 1st, 2006
0

Re: strstream problem

the loop is formatted wrong. Crevat: atof() does not detect errors in the string, but it doesn't really matter if you know the strings are all formatted ok.
C++ Syntax (Toggle Plain Text)
  1. while( in.getline(ch, 256, '\n'))
  2. {
  3. numbers.push_back(atof(ch));
  4. }

better yet, use a std::string instead of a char array
C++ Syntax (Toggle Plain Text)
  1. std::string line;
  2.  
  3. while( getline(in, line) )
  4. {
  5. numbers.push_back(atof(line.c_str));
  6. }
Last edited by Ancient Dragon; Oct 1st, 2006 at 5:04 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is online now Online
21,966 posts
since Aug 2005
Oct 1st, 2006
0

Re: strstream problem

Your code shouldn't even compile. You're missing a using declaration (I assume) and main should return 'int'

Also, you should use std::stringstream instead of strstream (I believe the latter is deprecated)
Last edited by GloriousEremite; Oct 1st, 2006 at 5:06 pm.
Reputation Points: 108
Solved Threads: 14
Junior Poster in Training
GloriousEremite is offline Offline
65 posts
since Jul 2006
Oct 1st, 2006
0

Re: strstream problem

Click to Expand / Collapse  Quote originally posted by nanodano ...
    for (int a = 0; a <= numbers.size(); a++)
   cout << numbers[a] << ", ";
I think you are in for a bit of surprise
Super Moderator
Featured Poster
Reputation Points: 3244
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Oct 1st, 2006
0

Re: strstream problem

Thank all of you for your help,
I took all of your suggestions into account and made some changes in my code. Ancient Dragon, I took your advice on the string, which I think is more appropriate since I don't know how long the input may be. The thing is, I'm not trying to pushback the string. I use a string so I can work on one line at a time. Then, I had a thought. In the long run, I'm trying to use a mergesort to sort each line and output on its own line. I think what I'm going to try is something like this:
C++ Syntax (Toggle Plain Text)
  1. psuedocode:
  2. vector<vector<float>>inputlines
  3. vector<float>floatvector
  4.  
  5. while (infile) {
  6.  
  7. extract one line into a string
  8. put the string into a strstream
  9. while (mystrstream) {
  10. mystrstream >> myfloat
  11. floatvector.pushback
  12. }
  13. inputlines.pushback(floatvector)
  14. }

Hopefully my psuedo code is psuedoreadable. I was thinking about creating a vector to hold each line of input, and gathering all the input at the beginning of the program. Then in the next procedure merge sort each input line and then output them.

Ok, so, with that unneccessary information, I did run into another problem even with my modified code. At the end of the run, each vector has one too many numbers still. The last number is always repeated. I also printed out the sizeof each vector, and it comes to 11, instead of the 10 inputs on each line. I'm not sure why the last number is being duplicated every time. Should I try a different kind of loop? Maybe a do-while and check the status of the stream at the end of the loop? My in.dat and my output are at the end of the post.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <strstream>
  4. #include <string>
  5. #include <vector>
  6. using namespace std;
  7.  
  8. void main() {
  9.  
  10.  
  11. ifstream in("in.dat");
  12. vector<float>numbers;
  13. float myfloat;
  14. string input;
  15.  
  16.  
  17. while (in) {
  18. getline(in, input);
  19. numbers.clear();
  20. strstream myss;
  21. myss << input;
  22.  
  23. while(myss) {
  24. myss >> myfloat;
  25. numbers.push_back(myfloat);
  26. }
  27.  
  28. for (int a = 0; a < numbers.size(); a++) // outputs one line
  29. cout << numbers[a] << ", ";
  30. cout << endl;
  31. cout << "sizeof numbers: " << numbers.size() << endl;
  32. }
  33. }

OUTPUT:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10,
sizeof numbers: 11
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 20,
sizeof numbers: 11
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 30,
sizeof numbers: 11
30,
sizeof numbers: 1

in.dat:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
Last edited by nanodano; Oct 1st, 2006 at 6:04 pm.
Reputation Points: 36
Solved Threads: 2
Junior Poster in Training
nanodano is offline Offline
78 posts
since Feb 2005
Oct 1st, 2006
0

Re: strstream problem

>>I'm not trying to pushback the string. I
If you read my example carefully I didn't push_back the string either.

>>At the end of the run, each vector has one too many numbers still
re-read my previous post -- I already told you that your loop is incorrect and I showed you the correct loop
Last edited by Ancient Dragon; Oct 1st, 2006 at 6:10 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is online now Online
21,966 posts
since Aug 2005
Oct 1st, 2006
0

Re: strstream problem

Sorry for misunderstanding it. I got the problems all worked out now. Thank you all. =)
Reputation Points: 36
Solved Threads: 2
Junior Poster in Training
nanodano is offline Offline
78 posts
since Feb 2005
Oct 1st, 2006
0

Re: strstream problem

Ok, for anyone interested, this is the entire mergesort program I was working on. It is finished and works(w/ g++). The stringstream issues I had were in the "GetInput" function. You will see how I finally decided to solve the problem.

C++ Syntax (Toggle Plain Text)
  1. /* mergesort.cpp | John Daniel Leon | johndleon@gmail.com | 10/01/06
  2.  This program sorts numbers using a merge sort algorithm
  3.  Merge sort: O(nlog(n)) - Divides unsorted list in half and sorts each sublist
  4.  Recieves input from file in.dat - NOTE : End of file does not contain a new line. */
  5.  
  6. #include <iostream>
  7. #include <fstream>
  8. #include <sstream>
  9. #include <string>
  10. #include <vector>
  11.  
  12. using namespace std;
  13.  
  14. void GetInput(vector<float> &numbers, vector< vector<float> > &inputlines);
  15. void mergesort(vector<float> &myvec);
  16. void mergesort(vector<float> & a, vector<float> & tmp, int left, int right);
  17. void merge(vector<float> & vec1, vector<float> & vec2, int leftpos, int rightpos, int rightend);
  18.  
  19. void main()
  20. {
  21. vector<float>numbers;
  22. vector< vector<float> >inputlines;
  23.  
  24. GetInput(numbers, inputlines); // Uses "in.dat" - no new line at EOF
  25.  
  26. for (int q = 0; q < inputlines.size(); q++)
  27. mergesort(inputlines[q]);
  28.  
  29. for (int x = 0; x < inputlines.size(); x++){
  30. for (int a = 0; a < inputlines[x].size(); a++)
  31. cout << inputlines[x][a]; if(a != inputlines[x].size() - 1) cout << ", ";
  32. cout << endl;
  33. }
  34. }
  35.  
  36.  
  37. void GetInput(vector<float> & numbers, vector< vector<float> > & inputlines)
  38. // This function reads "in.dat" file and stores each float number on a line into a float
  39. // vector. Then the float vectors are stored in a vector called inputlines.
  40. // This waay I can gather all the data in one procedure and then close the file
  41. // and move on with the rest of the program.
  42. // NOTE: End of "in.dat" should not contain a newline.
  43. {
  44. ifstream in("in.dat");
  45. if(!in)
  46. cout << "Error reading file: in.dat" << endl;
  47. string input;
  48. float myfloat;
  49.  
  50. while (getline(in, input)) {
  51. numbers.clear();
  52. stringstream myss;
  53. myss << input;
  54. while(myss >> myfloat) {
  55. numbers.push_back( myfloat );
  56. }
  57. inputlines.push_back(numbers);
  58. }
  59. in.close();
  60. }
  61.  
  62.  
  63. void mergesort ( vector<float> &unsortedvec )
  64. // Calls mergesort on the unsorted vector and a temporary
  65. // vector the same size as unsortedvec.
  66. {
  67. vector<float> temp(unsortedvec.size());
  68. mergesort(unsortedvec, temp, 0, unsortedvec.size() - 1);
  69. }
  70.  
  71.  
  72. void mergesort(vector<float> & a, vector<float> & tmp, int left, int right)
  73. // If the size of the vectors is not 1, then it recurses on itself and
  74. // then calls merge.
  75. {
  76. if (left < right)
  77. {
  78. int center = (left + right) / 2;
  79. mergesort(a, tmp, left, center);
  80. mergesort(a, tmp, (center + 1), right);
  81. merge(a, tmp, left, (center + 1), right);
  82. }
  83. }
  84.  
  85.  
  86. void merge(vector<float> & vec1, vector<float> & vec2, int leftpos, int rightpos, int rightend)
  87. // This is the merge routine. It compares the elements in each vector and manipulates the elements
  88. // so that vec1 ends up with the final merged(sorted) values.
  89. {
  90. int leftend = rightpos - 1;
  91. int tmppos = leftpos;
  92. int numelements = (rightend - leftpos + 1);
  93.  
  94. while(leftpos <= leftend && rightpos <= rightend)
  95. if (vec1[leftpos] <= vec1[rightpos])
  96. vec2[tmppos++] = vec1[leftpos++];
  97. else
  98. vec2[tmppos++] = vec1[rightpos++];
  99.  
  100. while(leftpos <= leftend) // Copy rest of first half
  101. vec2[tmppos++] = vec1[leftpos++];
  102.  
  103. while(rightpos <= rightend) // Copy rest of right half
  104. vec2[tmppos++] = vec1[rightpos++];
  105.  
  106. for(int i = 0; i < numelements; i++, rightend--) //Copy vec2 back
  107. vec1[rightend] = vec2[rightend];
  108. }
Reputation Points: 36
Solved Threads: 2
Junior Poster in Training
nanodano is offline Offline
78 posts
since Feb 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: String to array
Next Thread in C++ Forum Timeline: Unsorted List ADT Client Code Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC