issues with your trick duoas

Duoas im trying to implement your cleanest method of keeping the window open. It seems best option compared to system calls and other tricks. However I have a problem. I wrapped your trick in a class and it works 99% of the cases. however if i use cin operation something is happening and it just exits like it ignores the trick. I have some code below. if you use regular function calls in main like cout and others it works as expected. add cin and the whole thing fails. all methods i have tried to fix this cause it to break the other way if you dont use cin.

this is the code that fails because of the cin.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>

using namespace std;

class pause
{
public:
	~pause()
	{
		cout << endl << endl;
		cout << "Press ENTER to continue...";
		cin.ignore(numeric_limits<streamsize>::max(), '\n' );
    }
};


int main(int argc,char* argv[])
{
        cout << "test" << endl;
	cout << "enter your name : ";
	string name;
	cin >> name; // edit out this and name below and it works
	cout << name; //

	pause ps; // no joy for me
}
closed account (4Gb4jE8b)
Well, I can't answer you're question as to why it does, though i could have sworn you needed limits.h included to pull that off, i don't know for sure.

But i can offer a change that will likely work. though my trick is slightly different from his, it's still very similar in that i edited his code for my purposes, you may find that it works (or not, no idea)

1
2
3
4
5
void pause()
{
	cout << "Press ENTER to continue\n";
	cin.get();
}


include this before int main, any time you need to pause, just call the function.

note, you use new lines before, feel free to just include that in the general code shown. Also this only needs iostream

note2, i have seen errors with this when the user enters multiple characters before pressing enter. If anyone knows how to deal with that, i would love them forever (yeah i could probably figure it out, i've just never put my mind to it :P)
Last edited on
Problem with synchronization.

1
2
3
4
int d;
std::cin >> d;
std::cout << d;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');


This one won't work as you may expect. The cin hasn't been synchronized. Thus, cin has already started to receive data at the line 2 and has already received '\n' by the time my program reaches the line 4.

That is exactly the same, what is taking place in your program.
You have to use cin.sync(); each time you want to notify the stream that input operation's ended and it is time to synchronize the buffer.
you are brilliant lionishy. it works as i wanted it to now. never knew anything about synchronization. i will have to check into that. also im a bit clueless why i didnt need to add limits to this and have it still work. although ive added it here for clarity.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

#include <iostream>
#include <string>
#include <limits>

using namespace std;

class pause
{
public:
	~pause()
	{
	    cin.sync();
	    cout << endl << endl;
	    cout << "Press ENTER to continue...";
	    cin.ignore(numeric_limits<streamsize>::max(), '\n' );
        }
};


int main(int argc,char* argv[])
{

	cout << "enter your name : ";
	
	string name;
	cin >> name;

	cout << name;

	pause ps; // yay works now :)
}
Last edited on
acorn>i will have to check into that

That is a good point.

why i didnt need to add limits to this and have it still work

I can't give you an expert answer :-(
I suppose, that data are not transferred to the cin until enter's been pressed. It is console tool subject. Nevertheless, it is better that some one experienced with a console data input will answer you.
Last edited on
acorn
My knowledge of the iostream is rather limited.
Of cause, with synchronization we get rid of unread characters and ensure that cin.ignore() won't unexpectedly catch the '\n' escape from unread symbols.
However... It might be that we, in fact, need those symbols.

I'm confused... So, better to be careful with sync() call.


why i didnt need to add limits to this and have it still work

I've thought out a situation when we really have to include limits.

Consider the next code:
1
2
3
4
5
6
7
8
    std::string firstNameStr, secondNameStr;
    std::cout << "What is your first name?" << std::endl;
    std::cin >> firstNameStr;
    std::cout << "What is your second name?" << std::endl;
    std::cin >> secondNameStr;
    
    std::cout << firstNameStr << std::endl;
    std::cout << secondNameStr << std::endl;


And now imagine Leeloo is trying to insert her name :-) Leeloo Minai Lekarariba-Laminai-Tchai Ekbat De Sebat

The out will be somewhat like:
Leeloo
Minai


And the program wouldn't even give her a chance to insert the second name.

With the aid of cin.ignore() trick we can avoid such a pity.

1
2
3
4
5
6
7
8
9
    std::string firstNameStr, secondNameStr;
    std::cout << "What is your first name?" << std::endl;
    std::cin >> firstNameStr;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n')
    std::cout << "What is your second name?" << std::endl;
    std::cin >>secondNameStr;
    
    std::cout << firstNameStr << std::endl;
    std::cout << secondNameStr << std::endl;


And now everything is working as we may expect!

And the output will be:
Leeloo
Dallas


The first name is still corrupted, nevertheless she is able to insert her second name :-)
Last edited on
Topic archived. No new replies allowed.