User input in C++

Hi, I am new to C++, and I was wondering if anyone could please help me with user input. I need for the program to end when the user types "quit". I am not sure what is the best way to do it.
1
2
3
  If system.in = "quit";
System.exit()
Thanks, that's a bit helpful. I still need to know how to exit though. Any ideas? Much appreciated.
1
2
If system.in = "quit";
System.exit()

This will not work if you don't remove that semicolon from the first line and put it on the end of second one.
Returning from main terminates program. So if your code is in the main function just type
return 0; (or return; if main is void).
Last edited on
Wow, sorry I was working on it last night and totally should have noticed the semicolon.
This is my code:

int main()
{

std::string input;
std::cin >> input;

while (input != "quit")
{
// do stuff
std::cin >> input; // get another input

}

return EXIT_SUCCESS; // if we get here the input was quit


I have to write a program which takes reads a word at a time and determines whether the word begins with a vowel, a consonant or something else such as a number. Words are repeatedly accepted until the word ‘quit’ is entered.

Is there a way for C++ to differentiate between those three inputs? I think I can probably do a case for vowels, and maybe even consonants, but it seems tedious. I can use the isalpha function to determine if it's a letter or a number.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
std::string input;
std::string vowels ("aeiouAEIOU"); // predefined string of vowels

std::cin >> input;
while (input != "quit")  {
    if (isalpha(input[0]) == false) {
        std::cout << "It was not a letter\n\n";
    } else if (vowels.find(input[0]) != std::string::npos) {
      // you can use .find to find out if character/string is in a particular string
      // so if this character is part of 'vowels' string, it's a vowel
        std::cout << "It was in a 'vowels' string\n\n";
    } else {
      // if it's a letter but not in a 'vowels' string, it's a consonant
        std::cout << "It's a consonant.\n\n";
    }
   
    std::cin >> input; // get another input
};


On line 8 you can also put:
...if (input[0] == 'A' || input[0] == 'a' || input[0] == 'e' || ... || input[0] == 'U')

http://www.cplusplus.com/reference/string/string/find/
Last edited on
Thanks, I ran it and apparently it's only reading the first letter.
determines whether the word begins with a vowel, a consonant or something else such as a number
1startswithanumber
It was not a letter

Aitsavowel
It was in a 'vowels' string

Cfdfdsfsdf
It's a consonant.

quit


Is this expected output?
Last edited on
I'm sorry, let me rephrase that. It is supposed to take count of every number, vowel, and consonant in a word. I also need the flexibility to enter in more than one word, i.e. a sentence.

Example run:

Enter words (quit to end):

The 12 awesome oxen ambled quietly across 15 acres of farmland. quit
6 words begin with a vowel
3 words begin with a consonant
2 other 'words' were found.
Oh, that's easy to implement, with just a few little changes to my original code.
Declare numVow, numCon, numOther as ints somewhere before while loop and initialize them to 0.
Then, in a while loop replace all std::cout lines with ++numOther;, ++numVow;, ++numCon; respectively.
Put cout << numVow << " words..." << numCon << " words..." << ... after the loop (before return of course).
Topic archived. No new replies allowed.