DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   while(true) loop + getline() = infinite loop?? (http://www.daniweb.com/forums/thread21076.html)

jimFan Mar 29th, 2005 5:35 am
while(true) loop + getline() = infinite loop??
 
Dear all,

Forgive me for posting two threads for the same problem. I was not showing my problem clearly enough in the previous one. Thank Narue for answering that anyway. Now I do it again here:

#include <iostream>
#include <string>
using namespace std;

int main(void)
{
    string inputStr;
    while (true)
    {
        getline(cin, inputStr);
        if (inputStr == "quit") break;
        cout << inputStr << endl;
    }
    return 0;
}


This programme runs fine when I compile and click the executable. But things go wrong if I do it in command-line and redirect the input stream to a text file:

$a.out < inputFile

*The programme does not let me do any input and it loops again and again instead.* I guest this is a problem associated with getline() function.

So what do you think about this?



Jim

Dave Sinkula Mar 29th, 2005 9:49 am
Re: while(true) loop + getline() = infinite loop??
 
Quote:

Originally Posted by jimFan
This programme runs fine when I compile and click the executable. But things go wrong if I do it in command-line and redirect the input stream to a text file:

$a.out < inputFile

Don't you mean input from? What are the contents of your input file?

Or do you mean to redirect the output to a file?
$a.out > inputFile

jimFan Mar 29th, 2005 10:33 am
Re: while(true) loop + getline() = infinite loop??
 
Quote:

Originally Posted by Dave Sinkula
Don't you mean input from? What are the contents of your input file?

Or do you mean to redirect the output to a file?
$a.out > inputFile


Hi Dave,

Oh sorry for my grammer. I actually mean input from:

$a.out < inputFile


Here is what the content of inputFile maybe:

ls
changecc COMP271 5
quiz
quit

The problem occur when the programme has read all the way to the last line:

quit

The programme loops and does not ask me for input.


Jim


All times are GMT -4. The time now is 2:24 am.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC