Hello,
I think your right that this rule is the problem : typedef vector<double>::size_type vec_sz ;
When I deleted that rule in the loop and put it before the while . It seems to work.
Roelof
Hello,
I think your right that this rule is the problem : typedef vector<double>::size_type vec_sz ;
When I deleted that rule in the loop and put it before the while . It seems to work.
Roelof
Start again, do each small bit and then test!
Write the loop to read words and store in the vector. Check that this works by writing a test loop to print each word.
Once that works, do the next part.
I learn quicker by breaking each part down as small as I can and then coding and testing. If you write too much at once, you will not know where the issues lie and will get frustrated.
I think what you want to do is something like this:
Code:#include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> eg; string data; cout << "Enter all words then hit ctrl-D:\n"; while (!cin.eof()) { cin >> data; eg.push_back(data); } int i, len = (int)eg.size(); cout << endl; for (i=0;i<len;i++) { cout << (i+1) << ": " << eg[i] <<endl; } return 0; }
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Hello MK27.
Not complety.
What I try to do is count every distinct word which the user has input.
Im now beginning again in very little steps so I can see what went wrong.
Roelof
Well, here is the output from that:
As you can see, for some reason "etc" is repeated twice. I'm pretty new to C++ too! I can maybe guess why that repetition is there tho; anyway, changing the while loop to this fixes that problem:[root~/C++] ./a.out
Enter all words then hit ctrl-D:
one two and then
some more
etc
1: one
2: two
3: and
4: then
5: some
6: more
7: etc
8: etc
I'm not trying to spoil anything for you roelof, just you seem to be having a lot of trouble with this and I thought a working example might be helpful.Code:cout << "Enter all words then hit ctrl-D:\n"; while (cin >> data) { eg.push_back(data); if (cin.eof()) break; }
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Hello MK27,
Thank you for the tip and the help.
tomorrow I will work on this project.
It''s now late here.
Roelof
Hello,
I found the answer
Any remarks on this solution.Code:#include <string> #include <vector> #include <iostream> using namespace std ; int main() { // ask for and read the words to count cout << "Enter all the words, " "followed by crtl-z enter on Windows: "; vector<string> words; vector<int> words_count ; typedef vector<double>::size_type vec_sz ; vec_sz size ; int found, place, teller ; string x ; while (cin >> x ) { size = words.size (); for (teller =0; teller < size; teller ++) { if (x == words [teller]) { found = 1 ; place = teller ; } } if (found == 1) { found = 0 ; words_count [place] +=1 ; } else { words.push_back (x) ; words_count.push_back (1) ; } } size = words.size () ; for (teller= 0; teller < size; teller ++) { cout << "Het woord : " << words[teller] << " komt " << words_count [teller] << " voor in de input" << endl ; } return 0; }
Roelof
Looks good, the way words_count gets incremented is clever. Here's a way you could optimize it slightly by losing the "found" and "place" variables, so there is slightly less copying and testing:
"Break" ends a loop (since it's done at that point) and "continue" proceeds to the next iteration, skipping the remaining code in the loop.Code:while (cin >> x ) { size = words.size (); for (teller = 0; teller < size; teller ++) { if (x == words [teller]) { words_count [teller] +=1 ; teller = -1; break; } } if (teller < 0) continue; words.push_back (x) ; words_count.push_back (1) ; }
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Oke,
I get what you mean.
But one thing I don't see.
If you use continue there, are the data put into the vector.
For me it don't seem so.
Roelof
Yes, but the idea is that if teller is negative, then the word already exists in the vector, so you do not want to push it into the vector. Actually, instead of assigning -1 to teller, it would be simpler to make use of the fact that if teller == size, then the word was not found in the vector, hence you would replace the part of MK27's example that uses continue with:
By the way, later you will learn that it is easier to use a std::map container to solve this problem, but for now this is good practiceCode:if (teller == size) { words.push_back(x); words_count.push_back(1); }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Oke,
I get what you mean.
If the teller reaches the value of the size then it's not found.
Thank you for the tip.
Im now trying the exercise where multiple students enter thier grades and I have to compute the average of every student.
So this one is solved.
Everyone thanks for the tip and the help.
Roelof
Correct. For some reason I was thinking that if the match were to the last element teller would == size anyway but of course the break prevents that.
That's what I would have done, but I think in fact roelof's solution is more elegant and definitely more efficient -- sometimes less is more, syntax usage wise. A map would be a waste here.
Last edited by MK27; 06-18-2010 at 08:55 AM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Oke,
Maybe I could use a map but the book from which I try to learn c++ has not talked about maps.
So Im not allowed to use maps.
If I could maps , i would map.find to find out if a name is in the vector.
Roelof
Last edited by roelof; 06-18-2010 at 09:19 AM.
Yes, do not use them yet. It is just something to keep in mind for the future.Originally Posted by roelof
No, roelof's solution is less efficient in the long run: it performs a linear search of the existing words for each word, compared to the O(log n) search of the map for each word. As for elegant: that depends on your point of view, but the map solution is certainly more concise, even if one makes use of stuff like std::find instead of manually implementing the linear search.Originally Posted by MK27
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge