943,394 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3297
  • C++ RSS
Dec 10th, 2004
0

confused how to begin this program

Expand Post »
i am taking a cpsc 140 class and i'm a little confused how to begin my assignment. Write a C++ program that reads in an unknown number of characters from the a text file, count the number of words, and print each word in reverse to the screen.
Words are delimited by blanks, tabs, newlines and the end of file. Note that mulitple whitespace characters should only count one word. Hint: You can use the get method to enter whitespace . i dont recall what the get method is. if anyone can start me out i would be greatly appreciated. thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mak 23 is offline Offline
3 posts
since Dec 2004
Dec 10th, 2004
0

Re: confused how to begin this program

Quote ...
int fgetc(FILE* stream);
Returns next character from (input) stream stream, or EOF on end-of-file or error.
http://www.infosys.utas.edu.au/info/...C/CStdLib.html
Reputation Points: 32
Solved Threads: 2
Light Poster
serfurj is offline Offline
35 posts
since Dec 2004
Dec 10th, 2004
0

Re: confused how to begin this program

could you help me get started on this program. i dont want u write the program for me, i just would like to know what steps i have to take to figure it out on my own
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mak 23 is offline Offline
3 posts
since Dec 2004
Dec 10th, 2004
0

Re: confused how to begin this program

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. int main(void)
  6. {
  7. std::ifstream file(__FILE__);
  8. std::string word;
  9. while ( file >> word )
  10. {
  11. std::cout << word << std::endl;
  12. }
  13. return 0;
  14. }
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 11th, 2004
0

Re: confused how to begin this program

Just a little help ...
[php]// pulls the words out of a text stream
string get_word(istream& in)
{
char ch;
string s;

while(in.get(ch))
{
// these characters separate the words
if (ch == ' ' || ch == ',' || ch == '.' || ch == '\n' || ch == '\t')
break;
s.push_back(ch); // build the word
}
return s;
}
[/php]
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Dec 11th, 2004
0

Re: confused how to begin this program

steps:
1) order a large amount of coffee
2) order some pizzas
3) kickstart your brain
4) start thinking how you would solve it
5) write down your solution in words on a piece of paper
6) translate that into a flowchart and/or other diagrams
7) translate that into a functional model of your application
8) translate that into code
9) test and fix until working
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 13th, 2004
0

Re: confused how to begin this program

thanks for all the help..the only thing i cant figure out now is how to put the actual text file in. the text is :

this is a file containing lots of
words with whitespace such as tabs and
endoflines and blanks but no
punctuation
you should be counting the
words and reversing each word before
echoing this tothescreen at the end
print the number of words in the file


is there a way for me to put this in without actually putting all this text in my code?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mak 23 is offline Offline
3 posts
since Dec 2004
Dec 13th, 2004
0

Re: confused how to begin this program

Sure, and that's exactly how you should do it:

Create a textfile on disk.
Read in the file in your program. C(++) has full facilities for reading and writing any kind of file built right into the core language.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 13th, 2004
0

Re: confused how to begin this program

Quote originally posted by mak 23 ...
thanks for all the help..the only thing i cant figure out now is how to put the actual text file in. the text is :

this is a file containing lots of
words with whitespace such as tabs and
endoflines and blanks but no
punctuation
you should be counting the
words and reversing each word before
echoing this tothescreen at the end
print the number of words in the file


is there a way for me to put this in without actually putting all this text in my code?
Select the text in your editor and save it as test.txt then read it into your program as a text file. You can separate the text into words with strtok(), setting the appropriate delimiters. It's up to your genius to spell each word in reverse.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

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: please help me - extremely clueless :(
Next Thread in C++ Forum Timeline: System() Function: Info on uses & Description





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


Follow us on Twitter


© 2011 DaniWeb® LLC