I need some help with a code I'm writing. The user inputs some letters, a loop is ran to check if it's a vowel or consonant, then when the user is done it returns the total amount of vowels and consonants.

The problem I'm having is the loop is suppose to stop when the user clicks Ctrl + X, but I can't get it to work. For now as a test I have it stop when the user enters 'z'. Any help would be VERY appreciated!

#include <iostream>
using namespace std;

void is_vowel(int & vowel, int & cons, char ch);

int main(){
	int vowel = 0, cons = 0;
	char ch = ' ';
	cout << "Please enter some letters (Stop with Ctrl + X): ";
	while (ch != 'z'){
		cin >> ch;
		is_vowel(vowel, cons, ch);
	}
	cout << vowel << endl;
	cout << cons << endl;
	return 0;
}

void is_vowel(int & vowel, int & cons, char ch){
	if (ch == 'a'||ch == 'e'||ch == 'i'||ch == 'o'||ch == 'u'||ch == 'A'||ch == 'E'||ch == 'I'||ch == 'O'||ch == 'U'){
		vowel++;
	}else if(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' ){
		cons++;
	}
}

Recommended Answers

All 11 Replies

The Ctrl+X key combination produces an End Of File (EOF) and associated error condition on the input stream. Figure out how to detect it and reset the stream then go from there.

This thread may help too.

Correct me if I'm wrong, but crtl + x isn't z.

Ctrl + X is sometimes a system command that may terminate or suspend the program.
On windows it's probably Ctrl + C.

Ctrl + X is sometimes a system command that may terminate or suspend the program.
On windows it's probably Ctrl + C.

Oh that's right. Oops. :confused:

EDIT:
Ctrl+C is "cancel".
Ctrl+Z is EOF

Because of the way the standard buffered stream input works, there is no way to capture the control characters using the <iostream> functions. You will need to use OS-specific functions to get raw keyboard input. In the case of Windows, it will also depend on whether you are using Win32 API directly, or a framework such as .NET; similarly, in Linux, it would depend on whether you are using a library such as NCurses.

Correct me if I'm wrong, but crtl + x isn't z.

I find it interesting that when people are right, they say "Correct me if I'm wrong, "
but when they in fact are wrong, they write their information as if it was a fact. Just something I notice... :icon_wink:

For now as a test I have it stop when the user enters 'z'.

Correct me if I'm wrong, but crtl + x isn't z.

Correct me if I'm wrong, but it helps to read the whole OP before responding ;)

So I have gotten the loop to stop when the user enters Ctrl + X, but now the loop only counts the first character the user inputs and ignores everything else. Would anyone know how to fix this?

#include <iostream>
using namespace std;

void is_vowel(int & vowel, int & cons, char ch);

int main(){
	int vowel = 0, cons = 0;
	char ch = ' ';
	cout << "Please enter some letters (Stop with Ctrl + X): ";
	is_vowel(vowel, cons, ch);
	cout << vowel << endl;
	cout << cons << endl;
	return 0;
}

void is_vowel(int & vowel, int & cons, char ch){
	cin >> ch;
	if (ch == 'a'||ch == 'e'||ch == 'i'||ch == 'o'||ch == 'u'||ch == 'A'||ch == 'E'||ch == 'I'||ch == 'O'||ch == 'U'){
		vowel++;
	}else if(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' ){
		cons++;
	}
}

This version isn't doesn't have any loop at all. Is that correct?

Also, how did you manage to trap Ctrl-X using <iostream>? It was my understanding that this isn't possible.

I did a little testing on my system (32-bit Win7 Pro, VC++ 2008) before I wrote my second post. With this code:

#include <iostream>

using std::cin;
using std::cout;

int main() {
  char readChar = '\0';
  cout << "Please enter a char... ";

  while (cin >> readChar) {
    cout << "Entered char is: " << readChar << std::endl;
    cout << "Please enter a char... ";
  }
  if (cin.eof()) {
    cout << "EOF detected on cin.  Resetting...";
    cin.clear();
  }

  return (EXIT_SUCCESS);
}

Ctrl+X produces an "up-arrow" character on my system.

Please enter a char... ^X
Entered char is: ↑
Please enter a char... ^Z
EOF detected on cin. Resetting...

Since this is a character value that doesn't meet either of the conditionals, it shouldn't trigger either action. The OP won't be able to tell for sure until they create the proper loop and test against a set of known data though.

Correct me if I'm wrong, but if there's no loop as Schoil-R-LEA points out, isn't it impossible to read more that 1 character when your input accepts only 1 character?

Correct me if I'm wrong, but if there's no loop as Schoil-R-LEA points out, isn't it impossible to read more that 1 character when your input accepts only 1 character?

Yes, that is true. But adding a loop seems to make Ctrl + X stop working. At this point I don't even know where I'm going with this...

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.