Hello,

I am a newbie in C++....I am trying to modify program to read in upto four words or 30 characters into a string and then print it out.

Here is the code.....

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
	bool isWord = false;  
	char ch;
	int wordCount = 0;  
	do {
		ch = getche(); 
		if (ch == ' ' || ch == '\r') {  
			if (isWord) {    
			wordCount++;  
			isWord = false;  
		}
}
		else if (!isWord)  
		isWord = true;  
}		while (ch != '\r');  
	cout << "\nNumber of words typed: " << wordCount << endl;
	system("pause");
	return(0);
}

Please advice!

Why use getche? As well as being compiler-specific, it is not very c++-like to use such functions. Here is some info on how C++ does i/o.

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.