Hi!
I just upgraded to win 7 64 bit.
I have tryed it both on cygwin compiler and MinGW compiler (toolchain)
Working on eclipse.
My code works perfect in Linux/ubuntu environment but running in win 7 environment I have the following problem.
My last cin does not work. The input is not fetched. At every "enter" it promts me for a new value. Nothing is fetched and cin continues to loop requesting new values ( but just for the last cin).
I have 4 cin requesting input values according as described in the code below.

string Configuration::fetchManInput ( string text1, string valueRange, string defaultValue)
{
    string checkDataOk;
    checkDataOk = "no";
    string input;
    input = "none";

    while (checkDataOk != "y")
    {

        cout << " 

        cout << text1 << " \n";
        cout << " value range:> " << valueRange << " \n";

        cout << ">  ";

      cin >> input ;

        if ( input == "d")
        {
            input = defaultValue;
        }

        cout << "You set it to >  " << input << "\n";

        cout << " ok ?....  if yes print y   " << "\n";
        cout << ">  ";

     cin >> checkDataOk;

    }
    return input;
}

And

string Configuration::readDeviceName()
{


	string deviceName;
	string ans("");

	cout << "ana= " << ans << "\n";
	deviceName = " ";
	string input(" ");

	while (ans != "y")
		{
	// Start up function
	cout << " Name the Device ex Sprinkler1 \n";
	cout << ">  ";

	cin >>  deviceName;

	cout << " ok ?  if yes print y \n";

	cin >> ans;

	}

	return deviceName;
}

Both those methods are called after each other.

The execution looks as follows

------
Do you want to run in test mode ? Default (d) is 'on'
value range:> on/off
> d
You set it to > on
ok ?.... if yes print y
> y
TestModeStatus= > set
========================================================
test1
ana=
Name the Device ex Sprinkler1
> fff
Size devicename= 3
You named it > fff
ok ? if yes print y
> y ....here I gave a y and hit enter
... .... ....here I hit enter
... .... ....here I hit enter
... .... ....here I hit enter
y ....here I gave a y and hit enter
y ....here I gave a y and hit enter
-------
as you can see its just looping.

If I cansel the execution it will continue to run the full application.

Please help.. I am going nuts about it!! I have tryed almost annything. Where is the fault. Obviously it has something to do with windows.

Gunnar

Recommended Answers

All 14 Replies

if you put all your program completely, I'll test it in my winxp!

The code you've posted will not even compile (snippet #1/line #11). So, maybe post the exact code that you are actually using.

In addition to the syntax error mentioned by mitrmkar, your posted code and your posted output do not match. Which one is correct?

I have Win 7 Pro (32-bit) and I use cin all the time with no issues at all (outside the normal lingering characters issue inherrent in the language). As far as I can see, this is a code/user error, not a Win 7 error.

Hi Thanks for your quick answer.

You helped me to find the problem. See my story below.

1. My application is rather big 8 about 15-20 classes) so I started to cut it down to just the essential parts. Onla a main with some function calls. After I had that done, I made a test run.... and it worked ... no strange loops.
2. Next step was to use exacly the same classes I had in my original but still keep it to 2 classes ( out of about 15).... It still worked perfect!
3. Now I am getting a felling of where the problem is. I have "down the line" used the windows Sleep function. Thats the only difference between my Linux and my Windows application. In Linux I use sleep function and that work fine.
It seems as if there is something I dont understand here with Sleep. If I use that function it will freeeze at the last cin.
You can test it in my enclosed app. run the app with the Sleep statement commented and run it with Sleep uncommented.
Running it uncommented result in a strange "freeze".

You find the Sleep line at the end of the main.
Gunnar

//----------------------------------------------------------------------

//----------------------------------------------------------------------


#include <string.h>
#include <iostream>
#include <cstring>
#include <string>

#define NOMINMAX
#include <windows.h>




using namespace std;

string testMode("");

string fetchManInput ( string text1, string valueRange, string defaultValue)
{
    string checkDataOk;
    checkDataOk = "no";
    string input;
    input = "none";

    while (checkDataOk != "y")
    {

        cout << " ====================================== \n";
        cout << text1 << " \n";
        cout << " value range:> " << valueRange << " \n";

        cout << ">  ";

      cin >> input;

        if ( input == "d")
        {
            input = defaultValue;
        }

        cout << "You set it to >  " << input << "\n";

        cout << " ok ?....  if yes print y   " << "\n";
        cout << ">  ";

     cin >> checkDataOk;

    }
    return input;
}
//-----------------------------------------------------
string checkManInputResult (string outputFromFetchManInput)
{
    string dataStatus;
    dataStatus = "unSet";

    if (outputFromFetchManInput == "none")
    {}
    else
    {
        dataStatus = "set";
    }

    return dataStatus;

}
//--------------------------------------------------------------------

void Configuration()
{


    cout << "Configuration: Constructor \n";

    //	string testMode;
    testMode = "none";
    string testModeStatus;
    testModeStatus = "unSet";



    // Set testMode.... printout flag
    while ( testMode == "none")
    {
        testMode = fetchManInput ( "Do you want to run in test mode ? Default (d) is 'on' ", " on/off", "on");
        testModeStatus = checkManInputResult (testMode);
        if ( testModeStatus != "set")
        {
            cout << "You have to set testMode! \n";
        }
        else
        {
            cout << "TestModeStatus= >  " << testModeStatus << "  \n";
            cout << "======================================================== \n";
        }
    }



}
//----------------------------------------------------------------

//-----------------------------------------------------------------

string readDeviceName()
{


	string deviceName;
	string ans("");

	cout << "ana= " << ans << "\n";
	deviceName = " ";
	string input(" ");

	while (ans != "y")
		{
	// Start up function
	cout << " Name the Device ex Sprinkler1 \n";
	cout << ">  ";

	cin >>  deviceName;

	 cout << "Size devicename= " << deviceName.size()  << "\n";
	cout << "You named it >  " << deviceName << "\n";

	cout << " ok ?  if yes print y \n";

	cin >> ans;

	cout << "Size ans= " << ans.size()  << "\n";
		cout << "You answered= " << ans << "\n";




	}

	return deviceName;
}


int main()
{

// Global Declarations





string deviceName;
string s;


cout << " STARTING MAIN \n";

Configuration();











cout << "test1 \n" ;

deviceName = readDeviceName();


unsigned long msec;
	msec = 60 * 1000;
	cout<< "tiemerStop Hit Enter to continue >";

	//##########################################################
// HERE UNCOMMENT THE Sleep TO GET MY PROLEM
Sleep (msec);  // in Windows


//###############################################################

// else Start timer

//while !exit

cout<< "END of APP";


return 0;
}

It seems as if there is something I dont understand here with Sleep. If I use that function it will freeeze at the last cin.

So, you are saying that with Sleep() enabled, the program freezes at line #132 (i.e. 'last cin'). So at that point you'll be unable to input anything and likewise you are not seeing any further output either?
How does the program terminate? Do you have to kill it via Task Manager or something alike?

Hi mitrmkar!

Yes thats right! It fezezes at line 132 with Sleep enabled.

How to solve that is my next big problem.

sleep in Linux works fine but as you see it does nor work with windows Sleep.
Gunnar

did you know that your Sleep() ends after 1 minute!
if you think there's any problem, first reduce this time, so that you can easily test your program & find the problem, if any.

Hi all!
Yes I know that there is a delay of 60 secs. That's not my problem.
The problem is that "cout <<" between cin and sleep is not executed until after the Sleep period is finished.
Compare this with the linux sleep where everything inbetween cin and sleep is executed.

Is this a eclipse problem or a c++ problem?? Well I just executed the binaries directly, outside eclipse debugger, and it seems to work as it should.
Conclusion: this is a exclipse debugger problem
That problem is, kind of , outside this forum isn't it?

Anyone who knows where to turn with that kind of problem, is welcome!

Even though I found out the problem myself, I couldn't have done it without your help, so THANKS alot all of you.

See you
Gunnar
PS
Here is a very much reduced c++ app focusing on my problem.
Try it if you want to!

#include <string.h>
#include <iostream>
#include <cstring>
#include <string>
//#define NOMINMAX
#include <windows.h>

using namespace std;

int main()
{

// Global Declarations

string ans("");
string ans1("");

cout << " STARTING MAIN \n";

cout << "Write something (no no spaces) \n";
cout << ">";
	cin >> ans;
	cout << "you wrote   '"<<  ans   << "'.... THIS SHOULD BE executed before the Sleep starts \n";

	cout << "THIS SHOULD BE executed, as well, before the Sleep starts \n";

unsigned long msec;
	msec = 15 * 1000;
	cout << "Starting timer \n";
//##########################################################
// HERE UNCOMMENT THE Sleep TO GET MY PROLEM
Sleep (msec);  // in Windows
//###############################################################

cout << "Sleep finished \n";
cout<< "END of APP \n";
cout<< "Write something to conclude the app >";
cin >> ans1;
return 0;
}

this is simplified version of your program:

#include <string.h>
#include <iostream>
#include <cstring>
#include <string>
//#define NOMINMAX
#include <windows.h>

using namespace std;

int main()
{
	string ans;
	cout << "Write something: ";
	cin >> ans;
	cout << "\nYou wrote: " << ans << "\n\n";

	cout << "Wait 10 seconds... " "\n\n";
	Sleep (10000);  // in Windows	
	cout<< "Now Write something to conclude the app: ";
	cin >> ans;	
}

in VC++ 2008 Ex:

output:
Write something: hello

You wrote: hello

Wait 10 seconds...

Now Write something to conclude the app: bye

note that Sleep suspends everthing in your program (in Windows).
And I don't see any problems!

Alright. did you run it in eclipse environment, in debug mode and win 7?
if I execute my app it looks like this.
As you can see from below some cout statements are written after the sleep but should be displayed before Sleep satement. Thats whats wrong.
Output:
STARTING MAIN
Write something (no no spaces)
>gggg

*** The following is written after the Sleep statement has been executed. As you can see from the text in cout, the marked part (#### should really be written before the Sleep
####
you wrote 'gggg'.... THIS SHOULD BE executed before the Sleep starts
THIS SHOULD BE executed, as well, before the Sleep starts
Starting timer
###
Sleep finished
END of APP
Write something to conclude the app >

End output

/Gunnar

OK, after all, it looks like you probably want to flush the (buffered) output, in order for it to show like you are expecting, even under the debugger. In other words, you may want to try e.g.

// Use 'endl' to have a newline inserted + output buffer flushed
cout << "Can you see me?" << endl;
// Use embedded '\n' and then 'flush'
cout << "Can you see me?\n" << flush;

did you run it in eclipse environment, in debug mode and win 7?

I use Visual C++ 2008 Express & Windows XP. And I think nothing is abnormal!
also in Dev C++, there's nothing abnormal!

Thanks.
The "endl" did it!!!
I new of "endl but I was under the impression that \n and endl had the same function but obviously the endl does something more, at least in windows environment.
Now I have to change all \n to endl

Thanks again!!!
Gunnar

Somehow it seems as if I lost the track of this thread. It seems to continue elsewhere so I will finish this off by stating the solution.

I have used \n after all cout<<. I was under the impression that \n and endl had the same function but the dont. Not in windows anyhow.

Do as follows and it worked for me.

It seems that one endl; is enought. I changed the last cout <<......\n; to cout <<......<< endl; It is enough with the last one before the Sleep statement.
/ Gunnar

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.