Need ideas for a program that sorts words in a text file

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

42 Replies - 777 Views - Last Post: 12 September 2012 - 08:49 AM Rate Topic: -----

#16 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 270
  • View blog
  • Posts: 3,880
  • Joined: 08-January 12

Re: Need ideas for a program that sorts words in a text file

Posted 12 September 2012 - 01:20 AM

Well, I have to agree with SkyDiver. A std::map would be better.
Was This Post Helpful? 1
  • +
  • -

#17 basicprogrammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-September 12

Re: Need ideas for a program that sorts words in a text file

Posted 12 September 2012 - 02:49 AM

Okay what I did was I stored each text file as a string in a vector. Now I'm trying to get the words out of these text files and store each word in a string pointer in my object. I can't figure out how to do this though... I tried something like this, which I thought would work, but it doesn't.


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?
Was This Post Helpful? 0
  • +
  • -

#18 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 270
  • View blog
  • Posts: 3,880
  • Joined: 08-January 12

Re: Need ideas for a program that sorts words in a text file

Posted 12 September 2012 - 03:00 AM

Hmm... Have you properly initialized "is"? Is word an array of std::string? If both of these are yes, then it should work. Please post some more code.
Was This Post Helpful? 0
  • +
  • -

#19 basicprogrammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-September 12

Re: Need ideas for a program that sorts words in a text file

Posted 12 September 2012 - 03:04 AM

View Postaresh, on 12 September 2012 - 03:00 AM, said:

Hmm... Have you properly initialized "is"? Is word an array of std::string? If both of these are yes, then it should work. Please post some more code.


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.
Was This Post Helpful? 0
  • +
  • -

#20 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 270
  • View blog
  • Posts: 3,880
  • Joined: 08-January 12

Re: Need ideas for a program that sorts words in a text file

Posted 12 September 2012 - 03:07 AM

Huh? Well, don't post the entire code, just how you initialized word. Because after reading your post, I think this is how you did it
string *word;

Am I correct?
Was This Post Helpful? 0
  • +
  • -

#21 basicprogrammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-September 12

Re: Need ideas for a program that sorts words in a text file

Posted 12 September 2012 - 03:09 AM

View Postaresh, on 12 September 2012 - 03:07 AM, said:

Huh? Well, don't post the entire code, just how you initialized word. Because after reading your post, I think this is how you did it
string *word;

Am I correct?

Yes that is how I did it
Was This Post Helpful? 0
  • +
  • -

#22 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 270
  • View blog
  • Posts: 3,880
  • Joined: 08-January 12

Re: Need ideas for a program that sorts words in a text file

Posted 12 September 2012 - 03:16 AM

Well, you are declaring "word" to be a pointer. But where is it pointing to? You first need to allocate some memory. So, do that by using new.
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.
Was This Post Helpful? 0
  • +
  • -

#23 basicprogrammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-September 12

Re: Need ideas for a program that sorts words in a text file

Posted 12 September 2012 - 03:27 AM

View Postaresh, on 12 September 2012 - 03:16 AM, said:

Well, you are declaring "word" to be a pointer. But where is it pointing to? You first need to allocate some memory. So, do that by using new.
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?
Was This Post Helpful? 0
  • +
  • -

#24 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 270
  • View blog
  • Posts: 3,880
  • Joined: 08-January 12

Re: Need ideas for a program that sorts words in a text file

Posted 12 September 2012 - 03:32 AM

Well, the vector thing should work. After you declare the vector, how do you add elements to it? To add elements to your vector of strings, you must use push_back. So, try to use push_back or any other method of adding elements to your vector, and then post the updated code.
Was This Post Helpful? 0
  • +
  • -

#25 basicprogrammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-September 12

Re: Need ideas for a program that sorts words in a text file

Posted 12 September 2012 - 03:43 AM

View Postaresh, on 12 September 2012 - 03:32 AM, said:

Well, the vector thing should work. After you declare the vector, how do you add elements to it? To add elements to your vector of strings, you must use push_back. So, try to use push_back or any other method of adding elements to your vector, and then post the updated code.

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.
Was This Post Helpful? 0
  • +
  • -

#26 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 270
  • View blog
  • Posts: 3,880
  • Joined: 08-January 12

Re: Need ideas for a program that sorts words in a text file

Posted 12 September 2012 - 03:47 AM

Man, what are you doing? word is a vector of string!
string temp;
getline(is,temp,' ');
word.push_back(temp);


In this code, I am adding 'temp' to 'word'. Do you understand this?
Was This Post Helpful? 0
  • +
  • -

#27 basicprogrammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-September 12

Re: Need ideas for a program that sorts words in a text file

Posted 12 September 2012 - 03:52 AM

View Postaresh, on 12 September 2012 - 03:47 AM, said:

Man, what are you doing? word is a vector of string!
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
Was This Post Helpful? 0
  • +
  • -

#28 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 270
  • View blog
  • Posts: 3,880
  • Joined: 08-January 12

Re: Need ideas for a program that sorts words in a text file

Posted 12 September 2012 - 03:53 AM

Well, best of luck :)
Was This Post Helpful? 0
  • +
  • -

#29 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 270
  • View blog
  • Posts: 3,880
  • Joined: 08-January 12

Re: Need ideas for a program that sorts words in a text file

Posted 12 September 2012 - 03:53 AM

Well, best of luck :)
Stupid DIC :P

This post has been edited by aresh: 12 September 2012 - 03:54 AM

Was This Post Helpful? 0
  • +
  • -

#30 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 845
  • View blog
  • Posts: 2,334
  • Joined: 20-August 07

Re: Need ideas for a program that sorts words in a text file

Posted 12 September 2012 - 04:53 AM

View Postbasicprogrammer, on 12 September 2012 - 07:54 AM, said:

View PostSkydiver, on 11 September 2012 - 11:46 PM, said:

Didn't you cover classes/structs and instantiating objects on the stack vs the heap?


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.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3