I need to write the while loop to terminate with a '|' character
program does not terminate correctly when using two cin numbers in the same line or even on separate lines (i.e. cin>>num1>>num2;)
when the program does terminate with ^Z it just scrolls a long list of the last numbers entered.

I've tried the following:
while (num1!='|') //fails
while (num1!=-1) //fails if two inputs from the cin. Works if only one
//cin variable exists. Sentinel value is "-1"
while (num1!=-1 ||num2!=-1) //fails
while (num1!='|' || num2!='|') //fails

The program should terminate if I were to enter "1 2|" or any other terminating character.
Thanks for any help you guys can give

I don't understand your question? Could you post a few lines of code to demonstrate the problem?

This is my current incarnation of what it is I'm trying to do.
I have a feeling I'm making this more difficult than what it needs to be. I need help getting the program to terminate if I type in something like "1 2{terminating character}<enter>"

#include <iostream>

int main()
{
       int num1, num2;
	
	cout<<"Enter two integers:  ";
	cin>>num1>>num2;

	while (num1!=-1){
		cout << num1<<' '<<num2;

		cout<<"\nEnter two integers:  ";
		cin>>num1>>num2;
        }
        return 0;
}

From what I have read I think your code should work fine but I am not too sure what ur trying to do. Here's my code that for debugging purposes outputs a line when 1 is the input for num1 therefore the loop is terminated.

Although I am new to programming so I would get a second opinion on this. Plus my code doesn't allow you to do the loop more than 3 times which is weird, if anyone knows how to solve that I would be interested to know whats wrong :)

Any way heres that code, no different to urs really :)

#include <iostream>

using namespace std;

int main()
{
	int num1, num2;

	cout<<"Enter one integers: ";
	cin>>num1 >> num2;

	while (num1 != 1)
	{
		cout << num1 << " " << num2;
		cout<<"\nEnter two integers: ";
		cin>>num1 >> num2;
	}
	cout << "Project Terminated." << endl << "You did it!! :D";
	return 0;
}

What integer is '|'? That looks like a character so you cannot read it into an int variable.

To do what you need you'll have to read the input as characters. If you get a number, convert it to an integer. If it's not a number, do what you need with the character.

What about something like this ?

#include <iostream>

using namespace std;

int main()
{
	char chr;
	while(cin.peek() != '|')
	{
		cin.get(chr);
		cin.ignore(1000, '\n');
		/* Put your code here */
	}
	return 0;
}
commented: Very good +4
commented: Actually, it's pretty gross... -3

Thanks for the help guys. I will also start using the code tags to make the code easier to read.

What about something like this ?

#include <iostream>

using namespace std;

int main()
{
	char chr;
	while(cin.peek() != '|')
	{
		cin.get(chr);
		cin.ignore(1000, '\n');
		/* Put your code here */
	}
	return 0;
}

I tried the above code. For the most part it works, the only problem I am running into is when I press "|" to terminate the program the program just spews out lines upon lines of junk.

I tried the above code. For the most part it works, the only problem I am running into is when I press "|" to terminate the program the program just spews out lines upon lines of junk.

I don't understand what you mean, can you please post the code where you're having this problem with?

It should work fine :)

#include <iostream>

using namespace std;

int main()
{
	int num1, num2;
	char chr;

	cout<<"Enter an integer to start:  ";
	cin>>num1>>num2;

	while (cin.peek()!='|')
	{
		cin.get(chr);
		cin.ignore(1000,'\n');

		cout<<"Enter two integers:  ";
		cin>>num1>>num2;

		cout<<num1<<' '<<num2;
	}

	cout<<"Bye!\n";
}

This is what I'm currently trying to run. What happens during execution is I need to enter three digits into the program just to bring up the while loop to enter in the two digits. I go ahead and enter the number, press the <enter> button twice to get the "Enter two integers: " prompt. When I try exiting the program with "|" the screen scrolls "Enter two integers" and then the last two numbers entered into the program.

You are not using the peek properly: The code should be like this

int main()
{
    int num1, num2;
    char chr;

    cout<<"Enter an integer to start:  ";


    while (cin.peek()!='|')
    {


        cin>>num1>>num2;

        cout<<num1<<' '<<num2;
       //do things with num1 or num2
    }

    cout<<"Bye!\n";

}

My ghod! Don't use cin.peek()! Read the line as text and process it.

>>My ghod! Don't use cin.peek()!
Why? You have anything to support your argument?

>>Read the line as text and process it.
OP would have to force to do this if the exit code was not a single charachter but a string (perhaps like "exit" or "bye").
When he needs to peek only one character, Tell me how it is bad.

My ghod! Don't use cin.peek()! Read the line as text and process it.

We aren't allowed to use a language feature or what ?
This is actually what the OP asked for: 'terminate a while loop using a character'

>> Read the line as text and process it.
This is also an option, but as siddhant already said this is only really needed when the exit code is a character sequence :) ...

OK, fine. It's generally the last thing a professional programmer would do.

Peeking is generally used for OS level programming when you need to do things outside the ordinary. Reading the character and processing it is ordinary. And this task certainly can be done easily using the ordinary techniques.

OK, fine. It's generally the last thing a professional programmer would do.

Peeking is generally used for OS level programming when you need to do things outside the ordinary. Reading the character and processing it is ordinary. And this task certainly can be done easily using the ordinary techniques.

You didn't satisfied me atleast. It was better if you had given a citation or some reference. If so however it is your personal advice, I respect. I may follow it too.
Anyways, Tux didn't deserved a Anti-reputation for this.

Peeking is generally used for OS level programming when you need to do things outside the ordinary. Reading the character and processing it is ordinary.

Yeah, but cin.peek() is only reading and returning the next character in the inputstream without extracting it from there ...

And this task certainly can be done easily using the ordinary techniques.

Is using cin.peek() difficult then ?

Edit:: The cin.peek() method doesn't destruct something when it's "peeking" ...

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.