User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 374,017 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,675 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 11080 | Replies: 2
Reply
Join Date: Dec 2004
Location: Hong Kong, SAR
Posts: 22
Reputation: jimFan is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
jimFan jimFan is offline Offline
Newbie Poster

Solution while(true) loop + getline() = infinite loop??

  #1  
Mar 29th, 2005
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,418
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 15
Solved Threads: 137
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: while(true) loop + getline() = infinite loop??

  #2  
Mar 29th, 2005
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
Reply With Quote  
Join Date: Dec 2004
Location: Hong Kong, SAR
Posts: 22
Reputation: jimFan is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
jimFan jimFan is offline Offline
Newbie Poster

Re: while(true) loop + getline() = infinite loop??

  #3  
Mar 29th, 2005
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:


The programme loops and does not ask me for input.


Jim
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:05 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC