Hi~
I've been stuck with this exercise for quite a few days. It's in a book I'm currently self-studying, C++ primer plus. The exercise requires using C syntax, while the exercise which follows this one requires using string objects instead, so i guess this is the appropriate forum^^. It is as follow:

/*
Write a program that uses an array of char and a loop to read one word at a time until
the word done is entered. The program should then report the number of words entered
(not counting done). A sample run could look like this:
Enter words (to stop, type the word done):
anteater birthday category dumpster
envy finagle geometry done for sure
You entered a total of 7 words.
You should include the cstring header file and use the strcmp() function to make the
comparison test.
*/

Since the exercise is in Chapter 5 - Loops and Relational Expressions, and Branching Statements and Logical Operators is Chapter 6, I can't use if statements here.
The following code is how I did it, but it has problems. The idea is to identify a word from the original array, place it in another array to compare with the "done" string, and increment the NumberOfWords variable accordingly.
There is no compile-time errors, but runtime errors there are >"< :
- I typed in "test string done", then pressed enter. The program halted, and VS2010 says: Unhandled exception at 0x011315f6 in C++PrimerPlusFrame.exe: 0xC0000005: Access violation reading location 0x00430000. and in the source code editor there is a yellow arrow poiting at line 46;
- I typed in "word test done", then pressed Enter, the program reported "The text has 2 words" correctly. But when I pressed "Enter" again, VS2010 says "Run-Time Check Failure #2 - Stack around the variable 'Analyzer' was corrupted.", and a yellow arrow points in the last line, the closing brace '}' of main(), of the source code.

Can anyone tell me what was going wrong here? And what do those two messages mean, access violation and stack corruption (what is a stack, anyway?)? What do the two arrows point at? I am only a beginner, and I have only little experience with VS2010. Please, shed some light on me ^^"

Here is the source of the whole thing:

/*
Write a program that uses an array of char and a loop to read one word at a time until
the word done is entered. The program should then report the number of words entered
(not counting done). A sample run could look like this:
Enter words (to stop, type the word done):
anteater birthday category dumpster
envy finagle geometry done for sure
You entered a total of 7 words.
You should include the cstring header file and use the strcmp() function to make the
comparison test.
*/

#include <iostream>
#include <cstring>

int main()
{
	using std::cin; using std::cout; using std::endl;

	const unsigned int LIMIT = 1001; //The limit of analyzable words
	char WordsToAnalyze[LIMIT] = {0}; //The array to store input text
	char Analyzer[LIMIT] = {0}; //The temporary array used for analyzing
	unsigned int NumberOfWords = 0; //The variable to keep track of number of words
	
	//Intro
	cout << "Welcome to WordCounter, char Edition!\n"
		<< "Please enter the words, and use 'done' as the end-marker.\n"
		<< "(maximum " << LIMIT - 1 << " characters.)" << endl;
	cin.getline(WordsToAnalyze,LIMIT);

	// <--- START WORD COUNTING --->
	/*
	- "done" as sentinel word.
	- StringPointer: set at 0, serves as the marker of the processed character in WordsToAnalyze;
	- LOOP:	- Set CharPointer at 0, serves as the marker for Analyzer. Reset Analyzer.
			- LOOP: Add character by character to Analyzer until a space in encountered;
			- Increment NumberOfWords and StringPointer (to push it over the space);
			- Check if Analyzer is "done";
			- Repeat loop
	- Decrement NumberOfWords (because "done" must not be counted).
	*/
	int StringPointer = 0; int CharPointer = 0;
	do
	{
		CharPointer = 0; //Reset CharPointer and Analyzer
		for ( WordsToAnalyze; WordsToAnalyze[StringPointer] != ' '; ++StringPointer)
		{
			Analyzer[CharPointer] = WordsToAnalyze[StringPointer];
			++CharPointer;
		}
		++NumberOfWords;
		++StringPointer;
		Analyzer[CharPointer] = '\0';
	} while ( strcmp(Analyzer,"done") != 0 );
	NumberOfWords--;
	//Analyzer[0] = '\0'; WordsToAnalyze[0] = '\0';
	// <!--- END WORD COUNTING --->

	//REPORT
	cout << "That text has " << NumberOfWords << " words.";
	cin.clear(); cin.get();
	return 0;
}

it's C Forume buddy.. Either post your program in C code or move it to c++ Forume.. :)

Oh ^^" I see. Can you help me with that? I seriously am not very experienced with forums...

This article has been dead for over six months. Start a new discussion instead.