I've read a few threads on this site; all seem to provide almost everything I need to complete this project, but I just cannot get there.

What I am trying to do is, instead of reading the raw text from a local text file and counting the word, have the user paste the raw text into the output window and C++ processes and returns the word count. The raw text can be a bunch of paragraphs, not just a line.

Below is my failed attempt, with the input portion commented out. A little bit more than a URL would be greatly appreciated. I've spent a lot of hours on this and failed each time :)

Background: taking a beginners course in C++

#include <string>
#include <fstream>
#include <sstream>
#include <iostream>

const char *FILENAME = "input.txt";

int main()
{

	/*
	std::cout << "Enter your text: ";
		char raw[1000];
	std::cin >> raw;
*/


	std::ifstream mytext(FILENAME);


	if(!mytext)
	{
		return 1;
	}
	std::string word;
    int totalwords = 0;
    int totalcharacters =0;

	while(mytext >> word)
	{
	std::cout << "Found: '" << word << "' with " << word.length() << " character(s)." << std::endl;
	totalcharacters = totalcharacters +word.length();
	totalwords++;
	}
	std::cout << "-----------------------------------" << std::endl;
	std::cout << "The file has " << totalwords << " words." << std::endl;
	std::cout << "The " << totalwords << " words used " << totalcharacters << " characters." << std::endl;

	return 0;
}

thanks in advance for your help.

Recommended Answers

All 6 Replies

Can you explain what the problem is? What is an example input, current output, and the expected output?

David

Can you explain what the problem is? What is an example input, current output, and the expected output?

David

Thanks David.

The prompt should be something like

Please enter some text:

The user can paste an unspecified amount of text (could be multi-line, multi-paragraph).

The output could say something like:

The text contains 140 words.

I have used the getline method to successfully get a string as input and store it to a string variable, but I am not sure what variable type I'd need to get a multi-line text. And then how do I work with that variable to break it down to count the words?

This is some fancy footwork with some STL stuff, but it does the trick:
http://programmingexamples.net/index.php?title=CPP/Strings/Split

Basically you are trying to separate the paragraph by the ' ' character (a space). Then you can store each word in a std::vector<std::string> and then use yourVector.size() to get a count of the words.

commented: thank you very much! +0

If the text you are reading is from a file, then the code posted should work pretty good as the >> operator should successfully separate the text into individual words based on finding whitespace characters. However, if you want to read from interactive user input you have a bit of a problem, because how does the program figure out when user input has stopped? There are several ways to deal with that issue, if it seems to be the problem you are experiencing.

How can the user "paste" something into a console program? They can enter data from the keyboard, they can get it from a file, or they can get it from some other stream, but I don't see how they can "paste" it in; at least not in the sense of "pasting" something into a window.

If you can get the whole input, then put it into a std::stringstream and you can use the >> operator just like you would with an fstream.

Lerner - I think he just means to paste something into the console window and then press enter, just as if he had typed it in the window directly.

David

thanks for the help, David. the link was very useful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.