#include "stdafx.h"
#include <iostream>



using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	char ch;
	
	do{
		cout<<"press Q or q to quit,press any key to continue  :  ";
		cin.get(ch);
		cin.ignore();
		
		if(ch!='Q'&&ch!='q')
			cout<<" you want to continue?\n";

		else
			cout<<"you press q to quit ";
		
		
	}while(ch!='Q'&&ch!='q');
	;

	system("pause>NULL");
	return 0;
}

my prof said that whenever how many the letter i input the output must be just one..
output
press any key to continue : ma.teresa l. sagun
you want to continue?
press any key to continue :
you want to continue?
press any key to continue :
you want to continue?
press any key to continue :
you want to continue?
press any key to continue :
you want to continue?
press any key to continue :
you want to continue?
press any key to continue :

it appears like this..will someone help me..

Dani AI

Generated

The loop is pulling characters out of the input buffer one at a time, so typing a full name and hitting Enter makes the loop run for each character left in the buffer. That is why you see many "you want to continue?" messages after a single long input. was on the right track about per-character reads, and was right that you need to clear the rest of the line — but avoid nonstandard tricks like the fflush(stdin) suggestion from (fflush on stdin is undefined in standard C++).

Two simple, portable fixes:

Read the whole line and examine only the first character:

#include <iostream>
#include <string>

int main() {
    std::string line;
    char ch = 0;
    do {
        std::cout << "press Q or q to quit, press any key to continue: ";
        std::getline(std::cin, line);
        if (!line.empty()) ch = line[0];
        if (ch != 'Q' && ch != 'q')
            std::cout << " you want to continue?\n";
        else
            std::cout << "you press q to quit ";
    } while (ch != 'Q' && ch != 'q');
    return 0;
}

If you prefer to keep a single cin.get(ch) style, consume the rest of the input line immediately after reading the char so leftovers don’t drive the next loop iteration:

#include <iostream>
#include <limits>

std::cin.get(ch);
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

If the goal is a true single-key press (no Enter), use a platform-specific function such as _getch() on Windows (<conio.h>), but know that this is not portable.

Choose the approach that fits whether you want whole-line input (recommended) or immediate single-key input.

Recommended Answers

All 5 Replies

you must provide CLEAR details if you want people to help you, what exactly is suppose your code to do?

commented: . +7

Looks like the ch.get(ch) takes each of your input character in and execute them one by one. As a result, each character is being checked in the loop.

Flush the input stream after input...

while (cin.get (temp) && temp != '\n');

fflush(stdin) will help you!
from: <snipped link>

post your question on <link removed>, computer experts will answer your question quickly.

hi, guys
have you solved your problem?

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.