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.

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?

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

This question has already been answered. Start a new discussion instead.