Click here to Skip to main content
15,878,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to have the user to enter a line of characters into a program of which should keep running until the user enters a lone q to quit.

What I have tried:

C++
//mywhile.cpp -- entering q to quit
#include <iostream>
using namespace std;
int main()
{
    {int q{};
    cout << "Enter a line ('q' to quit): ";
    cin >> q;
    if (cin.fail())
    {
        cin.clear();
        cin.ignore('\n');
    }
    cout << "Try again: ";
    cin >> q;
    cout << "Thanks for entering: " << q << " . Goodbye. \n";
    }
    system("pause");
}

Here is what I have tried but it is not working so I don't know where my error is?
Posted
Updated 3-Nov-21 13:56pm
v2
Comments
Richard MacCutchan 3-Nov-21 12:14pm    
You accept input from the user but you never test to see exactly what letter they typed. Also the input variable should be a char variable for a single letter reply, or a std::string for a complete line. You have declared q as an array of integers which will not work for either case.
merano99 4-Nov-21 14:35pm    
As you can see from our discussion below, there is no known C ++ solution to this frequently asked question. Under Windows there are solutions in C that also work under C ++.

The cheapest way on Windows would be this:
C++
#include <iostream>
#include <conio.h>

int main()
{
	char letter;

	std::cout << "enter 'q' to quit\n"; // ask user 

	do {
	letter = getch();
	} while (letter != 'q');
}
 
Share this answer
 
Comments
Richard MacCutchan 4-Nov-21 5:11am    
The question is about C++, so why are you using getch rather than cin?
merano99 4-Nov-21 14:25pm    
:-)
Out of desperation, because at this point it is neither platform-independent nor with "real" C ++.
If you don't like conio like me, I only see the possibility to use system calls directly. Unfortunately, it then becomes quite cumbersome. I also offered the solution here, see there.
I am not aware of any solution with cin that works without <enter>.
As Richard says, you don't test your input.

Start here: 3 Ways to Compare Strings in C++ - JournalDev[^]
 
Share this answer
 
Comments
merano99 3-Nov-21 18:03pm    
The linked artikel is using <string.h>. It works, but <string> would be the first choice for C ++.
If you do not like the cheap version, here is a solution without conio.h for Windows
C++
#include <windows.h>
#include <strsafe.h>
#include <iostream>

int main()
{
	char letter;
	DWORD cNumRead;
	INPUT_RECORD irInBuf[128];

	std::cout << "enter 'q' to quit\n"; // ask user 

	do {
		HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
		if (hStdin == INVALID_HANDLE_VALUE) exit(1);
	
		// Enable the window input events (without mouse)
		if (!SetConsoleMode(hStdin, ENABLE_WINDOW_INPUT))
			exit(2);

		if (!ReadConsoleInput(hStdin, irInBuf, 128, &cNumRead)) {	
				DWORD dw = GetLastError();
				std::cout << "System Error: " << dw << "\n"				
				exit(3);
			}

		// Dispatch events 
		for (unsigned i = 0; i < cNumRead; i++) {
			switch (irInBuf[i].EventType)
			{
			case KEY_EVENT: // keyboard input
				if (irInBuf[i].Event.KeyEvent.bKeyDown) {
					letter = irInBuf[i].Event.KeyEvent.uChar.AsciiChar;
					std::cout << letter;
				}
				break;
			default:
				letter = ' ';
			}
		}

	} while (letter != 'q');
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900