42 Replies - 777 Views - Last Post: 12 September 2012 - 08:49 AM
#16
Re: Need ideas for a program that sorts words in a text file
Posted 12 September 2012 - 01:20 AM
#17
Re: Need ideas for a program that sorts words in a text file
Posted 12 September 2012 - 02:49 AM
for (int i=0; i< 3; i++)
{
getline(is, word[i], ' ');
}
3 is just an arbitrary number that i chose because there are less more than 3 words in the line. So why isn't this working?
#18
Re: Need ideas for a program that sorts words in a text file
Posted 12 September 2012 - 03:00 AM
#19
Re: Need ideas for a program that sorts words in a text file
Posted 12 September 2012 - 03:04 AM
aresh, on 12 September 2012 - 03:00 AM, said:
is is properly initialized. And yes word is an array of strings. i initialized it by string* word. When I take away the pointer and just try to get one word then it works...
I could also pm you my code. I don't feel comfortable posting it publicly.
#20
Re: Need ideas for a program that sorts words in a text file
Posted 12 September 2012 - 03:07 AM
string *word;
Am I correct?
#21
Re: Need ideas for a program that sorts words in a text file
Posted 12 September 2012 - 03:09 AM
#22
Re: Need ideas for a program that sorts words in a text file
Posted 12 September 2012 - 03:16 AM
string *word = new string[3]; //Change 3 to the amount of strings you want
Don't forget to delete the memory at the end by writing
delete[] word;
However, this method will force you to know number of words per line in file at runtime, which you may not always know. So, use a vector/list/map so that you don't have to worry about number of elements and freeing memory after use.
#23
Re: Need ideas for a program that sorts words in a text file
Posted 12 September 2012 - 03:27 AM
aresh, on 12 September 2012 - 03:16 AM, said:
string *word = new string[3]; //Change 3 to the amount of strings you want
Don't forget to delete the memory at the end by writing
delete[] word;
However, this method will force you to know number of words per line in file at runtime, which you may not always know. So, use a vector/list/map so that you don't have to worry about number of elements and freeing memory after use.
When I replace the pointer with a vector it still doesn't work though.
vector<string> word;
And I'm not sure what to use as the second expression in the for loop. I could copy the entire line into a string by doing
getline(is, line)
then doing
for(int i=0; i < line.length(); i++)
But then I wouldn't be able to getline(line, word) would i?
#24
Re: Need ideas for a program that sorts words in a text file
Posted 12 September 2012 - 03:32 AM
#25
Re: Need ideas for a program that sorts words in a text file
Posted 12 September 2012 - 03:43 AM
aresh, on 12 September 2012 - 03:32 AM, said:
It won't let me do
word.push_back(1)
because it says "no instance of overloaded function matches the argument list"
So instead I tried
word[0].push_back(1); getline(is,word[0], ' ')
I'm just trying to see if I can get the first word to work. I think if I can get the first word then the rest should be easy right? But this code won't even let me get the first.. It lets me run it, but then it aborts and says my vector subscript is out of range.
#26
Re: Need ideas for a program that sorts words in a text file
Posted 12 September 2012 - 03:47 AM
string temp; getline(is,temp,' '); word.push_back(temp);
In this code, I am adding 'temp' to 'word'. Do you understand this?
#27
Re: Need ideas for a program that sorts words in a text file
Posted 12 September 2012 - 03:52 AM
aresh, on 12 September 2012 - 03:47 AM, said:
string temp; getline(is,temp,' '); word.push_back(temp);
In this code, I am adding 'temp' to 'word'. Do you understand this?
Ooohhhh!!! I just learned vectors tonight so I thought you needed to put an int in the parenthesis. The example I saw online had a vector of int's. So I guess that's where the mix up occurred.
Thank you! I should be able to do this now. I'll post again if I have another question. Which I'm sure I will. Haha
#28
Re: Need ideas for a program that sorts words in a text file
Posted 12 September 2012 - 03:53 AM
#29
Re: Need ideas for a program that sorts words in a text file
Posted 12 September 2012 - 03:53 AM
Stupid DIC
This post has been edited by aresh: 12 September 2012 - 03:54 AM
#30
Re: Need ideas for a program that sorts words in a text file
Posted 12 September 2012 - 04:53 AM
basicprogrammer, on 12 September 2012 - 07:54 AM, said:
Skydiver, on 11 September 2012 - 11:46 PM, said:
No. The class I'm in sort of assumed that we had already learned that in a previous class. While my previous class mentioned it, but assumed we'd work on it in the next class.
I'd suggest it's worth discussing with your tutors/lecturers to point this out to them. It sounds to me as if there might have been a gap in your earlier course material, and that this gap is likely to cause you to struggle as you progress through your studies.
Your tutors may be able to provide some assistance in catching up, and mentioning it to them might also help other students on your course if any others happen to be in the same situation.
|
|

New Topic/Question
Reply




MultiQuote



|