so heres a snippet of my code:

cout << "enter keyphrase: \n";
	cin >> nextchar;

	while(nextchar != '.')
	{
		isalreadyin = 0;
	for (int i=0; i<counter; i++)
	{
		if (key[i] == nextchar)
			isalreadyin = 1;
	}
		
		if(isalreadyin ==0)
		{
			key[counter]=nextchar;
			counter ++;
		}

		cin >> nextchar;

	}

at the moment i have '.' as my terminiating character. but i want new line to be it. But i cannot get it to work. ive tried '\n' and that dont work. i also tried the ascii value but that dont work so i kinda came to a dead end. So could anybody give me ideas on why it wont work?

p.s the nextchar is a char so could it be that char variable cannot accept '\n'

Dani AI

Generated

Short answer: the problem is not that char can't hold '\n' — it can — but that operator>> for char skips whitespace (spaces, tabs and newlines). Using cin >> nextchar will never read the Enter as '\n', so comparing to '\n' will never be true.

Use an extraction that does not skip whitespace. For example:

char ch;
std::cout << "enter keyphrase: ";
while (std::cin.get(ch) && ch != '\n') {
    // process ch (add to your key buffer if not already present)
}

If you wanted to read the whole line and process its characters (as suggested), std::getline into a std::string is the cleanest approach — but on old VC++6 if that gives trouble, use the stream member that fills a C buffer: std::cin.getline(buf, bufsize), or the C-style getchar() loop. Beware of mixing >> and get()/getline() — leftover newlines can bite you; call std::cin.ignore() to discard them before switching.

For debugging (see ), print the numeric value of the read character (cast to int) to confirm what the stream returns. Also note a small efficiency tip: repeatedly scanning key[] is O(n^2); for larger inputs consider a bool seen[256] lookup or std::set<char> instead.

References to prior replies: 's question about "how it fails" is answered by the whitespace-skipping behavior; 's prompt about the type is relevant — keep nextchar as char (it can hold '\n').

Recommended Answers

All 6 Replies

what is nextchar defined as?

Why not read the line in as a std::string using getline() (which will stop at the newline) and do the processing of the characters afterwards?

Why not read the line in as a std::string using getline() (which will stop at the newline) and do the processing of the characters afterwards?

i tried that before but for college i have to use visual c++ 6.0 and there is a bug when using getline which reads in extra characters. there is a fix on microsofts website but i cant fix it at college as i cant edit the programs files

How does using '\n' not work? It won't compile, crashes if you hit enter or pressing enter does not terminate the loop?

How does using '\n' not work? It won't compile, crashes if you hit enter or pressing enter does not terminate the loop?

it compiles fine but just doesn't terminate the loop

Then you aren't reading a \n.

After reading the character, immediately display it's binary value cout << (int) nextchar; If you don't see the value of '\n', you aren't reading it.

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.