954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

confused how to begin this program

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 :)

mak 23
Newbie Poster
3 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 
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/documentation/C/CStdLib.html

serfurj
Light Poster
35 posts since Dec 2004
Reputation Points: 32
Solved Threads: 2
 

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

mak 23
Newbie Poster
3 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 
#include <iostream>
#include <fstream>
#include <string>

int main(void)
{
   std::ifstream file(__FILE__);
   std::string word;
   while ( file >> word )
   {
      std::cout << word << std::endl;
   }
   return 0;
}
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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]

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

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?

mak 23
Newbie Poster
3 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

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.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

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.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You