1

So I have this program that tells you the number of characters and vowels in a paragraph.

Here is the code

#include <iostream>
using namespace std;

int main()
{
    cout<<"Enter your paragraph\nTo finish press 'ESC'\n\n";
    
    //27 Is The ESC Button In ASCII
    char s[500];
    cin.getline(s, 500, 27);
    
    //Vowel and Character counters
    long long vowels = 0, characters = 0;
    
    for(int i = 0; i<500; i++) s[i] = tolower(s[i]);

    for(int i = 0; i<500; i++) {
        if(s[i] == 'a'||s[i] == 'e'||s[i] == 'i'||s[i] == 'o'||s[i] == 'u'||s[i] == 'y')vowels++;
        if(s[i] >= 32 && s[i] <= 126) characters++;
    }
    cout<<"The amount of characters: "<<characters<<"\nThe amount of vowels: "<<vowels<<'\n';
}

How do I make it so that when the user presses ESC, it immediately does the output (On Mac)?

When the user presses ESC, it's supposed to print the number of characters and vowels in your paragraph, but when you press ESC, you must press enter actually to output.

Sadaroni
  • 11
  • 2
  • This is nothing to do with the program. Terminal buffers and sends input data to the program on Enter. I don't think you want to ask a user to tune Terminal. You can request to press Ctrl+D instead of Esc. – 273K Apr 03 '23 at 00:15

1 Answers1

0

Unless the Esc key press is part of your assignment, which would be unusual, I recommend you simply structure to a very common standard:

  • paragraphs end with two newlines

Read characters until the user presses a newline followed immediately by another newline. Your prompt then becomes a very simple:

std::cout << "Type your paragraph. Press 'Enter' twice to finish:\n\n";

Otherwise you should follow the linked posts about manipulating “raw” vs “cooked” input on Linux systems.

Dúthomhas
  • 8,200
  • 2
  • 17
  • 39