Of getline(cin,...), Ctrl+D and infinite loops

Here is a small program. It's meant to keep reading lines from the
stdin till someone types the (really cute) string "manchu". In that
case it breaks out of the loop and the program terminates.

using namespace std;

int main() {
string str;
char ch;
while ( cin ) {
cin.ignore( cin.rdbuf()->in_avail() );
getline( cin, str, '\n');
if ( str == "manchu" )
if ( cin.eof() || ) {

return 0;

I am compiling this with gcc 3.3.1 on Solaris 9 SPARC, and with gcc
3.2.3 on RHEL 3.

Now here is the crux. I wanted that if the user just typed "Ctrl+D",
it should clear the eof and fail bits and continue the loop looking
for more input. It does this nicely on Linux but goes into an infinite
loop on Solaris sparc, repeatedly encountering EOF. I am guessing that
this is a wrong piece of code
and somehow it is working in one case, and not in the other case.

And hoping that someone actually points out where I am going wrong.

[ See for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Posted On: Monday 5th of November 2012 01:32:25 AM Total Views:  170
View Complete with Replies

Related Messages:

Assignment involving loops, if statements and arrays; program will not run   (106 Views)
my program basically records the grades of 5 students in 3 subjects and prints the results according to total marks, percentage and what that percentage translates to in the form of a letter grade. Also it states the class, roll number, section and of the student. As I said, the program stops working after I try to enter the first value for class. Any help would be greatly appreciated, than you very much for your time. Code: #include #include int main() { float mth[5],bio[5],chem[5],tm[5],p[5]; char name[5][25],sec[5]; int i,c[5],r[5]; for (i=0;i
Nested loops..   (81 Views)
Hi everybody, im a newbie in C programming and i got project which i need to submit them tomorrow and im really struggling with "while nested loops".. my problem is i don't know how to make it, so if somebody could help i will be really appreciated... my problem is i have values which are a1= 0.1 0.2 0.3 0.4 0.5 a2= 0.5 0.6 0.7 0.8 0.9 1.0 and i need to create two dimensional array by using while loop to be more clear i need the output to be like this total area= 0.1/0.5 0.1/0.6 0.1/0.7 0.1/0.8 0.1/0.9 0.1/1.0 0.2/0.5 0.2/0.6 0.2/0.7 0.2/0.8 0.2/0.9 0.2/1.0 0.3/0.5 0.3/0.6 0.3/0.7 0.3/0.8 0.3/0.9 0.3/1.0 0.4/0.5 0.4/0.6 0.4/0.7 0.4/0.8 0.4/0.9 0.4/1.0 0.5/0.5 0.5/0.6 0.5/0.7 0.5/0.8 0.5/0.9 0.5/1.0 so please if anyone who know how to do it please help me, i've trying to make it since a week but my problem is i just don't get the concept of nested while loops...
Print fiibonacci series using recursion(print in recursive function,do not use loops)   (114 Views)
I am required to print the fibonacci series using a recursive function int fib(int n) where n is the number of elements in the series.I pass this through main.And i am not allowed to use any loops in main,such as Code: int b=0; for(c=0;cr) { p=fib(n-1)+fib(n-2); printf("%d\n",p); r=n; return p; } else { p=fib(n-1)+fib(n-2); return p; } } return 0; }
infinite loop   (104 Views)
I've written a code which is supposed to take the input from the user, save it in a file called "name" and then print it in the reverse order. But its getting stuck in an infinite loop, printing only the last character typed. The logic of the program is that, the getc(fp) in putchar(getc(fp)) will return a character and advance the file pointer by 1. The putchar will print the character and the condition in the while loop will bring the cursor back 2 places so that the next charcter can be printed... Code: #include #include int main() { FILE *fp;int n; char c; fp=fopen("name","w"); if(fp==NULL) printf("\nUnable to open\n"); while((c=getchar())!=EOF) putc(c,fp); printf("\nNo. of characters = %ld\n",ftell(fp)); fclose(fp); fp=fopen("name","r"); n=0l; while(feof(fp)==0) { fseek(fp,n,0); printf("Position of %c is %ld\n",getc(fp),ftell(fp)); n=n+5l; } putchar('\n'); n=ftell(fp); fseek(fp,n-1,0); do { putchar(getc(fp)); }while(!fseek(fp,-2L,1)); fclose(fp); }
Multiplication Table using loops   (107 Views)
, I need some help with formatting on my program. I can't seem to get the table to look like this: Code: 1 2 3 4 ------------------- 1 1 2 3 4 2 2 4 6 8 3 3 6 9 12 4 4 8 12 16 Here is my code: Code: #include int mult(int x, int y); int main(void) { int x, y; for (x = 1; x < 10; x++) { printf("%4d", x); } printf("\n"); printf("-------------------------------------\n"); for (y = 1; y < 10; y++) { printf("%d\n", y); } for (x = 1; x < 10; x++) { for (y = 1; y < 10; y++) { printf("%4d", mult(x, y)); } } return (0); } int mult(int x, int y) { return(x * y); } Now, I realize this would be 100x easier with arrays, but we haven't learned them in class yet, so I need to use loops. I just don't know how to get the numbers I need on the lines I want, like shown above. Help/Tips appreciated.
infinite loop   (82 Views)
hi so i developed this piece of code to help me find a co prime of a number. basically it just a fancy way of finding a number between 1 and itself, that is a prime number and not a factor of that number here is the code any help is appreciated Code: int finde(int phi){ int e =4; int resultprime; resultprime = IsPrime(e); while(e
Need to run 2 for loops simultaneously in C. Microprocessor Class. MSP430 TI   (128 Views)
I don't know about that microprocessor therefore I have to ask: By "simultaneously" you mean concurrently or at exactly the same time If the first, is that microprocessor interrupt driven If yes, learn how to use/set/call those interrupts. If no, sorry but you can't do it. If the second, do you have multiple cores/processors If yes, learn how to arrange/assign them different code to run. If no, sorry but you can't do it. Sorry for being too generic...
Skipping loops in step-by-step debug without skipping rest of function   (109 Views)
I have Visual C++ 2008 Express. For a long time, I've been wondering how I can skip over loops (while and for loops) without skipping to the end of that function. What do I mean Here's a quick example: Code: void TestFunction() { // locals // stuff I want to test beforehand - I need to test this for (LongLoop = 0; LongLoop < 65536; LongLoop++) { // lots of stuff to do - holding F10 for hours is not worth it } // aftermath stuff I must check as well - I must skip the loop to do it } Any ideas how this can be done There is Shift+F11 for "step out", but this causes everything after this function to be skipped and I can't have that. Holding the F10 key down for hours is not worth it for such long loops. F11 seems to have the same properties as F11, except that you can go into a function (doesn't always happen due to the annoying "no source code" error that comes up, even for my own functions). Any ideas on how to skip the loop without skipping the rest of the function
Range-based for loops and existing variables   (113 Views)
I was reading the proposal for the ranged-based for loop recently accepted into c++0x ( If I understand this proposal correctly, it is illegal to use an already existing variable in the first part of this kind of loop. Thus the following code would be legal: vector vec; for (int x : vec) { /* do whatever */ } But this code would not be legal: vector vec; int x = 0; // Declare x outside of loop for ( x : vec) { /* do whatever */} Am I correct in my reading of this paper If so, what is the reason for making the second type of loop illegal Joe Gottman -- [ See for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
Strange behavior with for loops in MSVC++2005   (124 Views)
. We recently encountered the following strange behavior in Microsoft Visual Studio 2005 SP1. //----------------------- template void f(); struct S { friend void f(); }; template void f() {} void test() { for (int i = 0; {} i = 123; // This line does not generate an error! } //----------------------- By including the first three lines, the compiler suddenly starts to leak loop variables out of for loop scopes. As far as we can tell, it seems to work appropriately in all other respects. If the first three lines are removed or permutated, the compiler generates an error as one should expect. Is this a known problem, and what workarounds might there be Best
efficiency and nested loops   (138 Views)
Luna Moon wrote: > My friend has three nested loops, each has 10000, 1000, 100 > iterations, respectively. > > What should be the most efficient way of layout out the three nested > loops Efficient for the programmer or for the CPU > > for (i=0; i for (j=0; j for (k=0; k
does your compiler / lint catch infinite recursion ?   (202 Views)
Does anybody know if there are any compilers that warn of infinite recursion ( a very simple case ) like void foo( int a ) { return foo( a ); } gcc does not seem to and lint does not seem to catch it either.
infinite stl list loop   (304 Views)
Dear all, I am just using a list iterator in a for loop to get the all values in the list, like this: for (list::iterator itt=labelLists[labels3X3cube[*iter]-1].begin(); itt!=labelLists[labels3X3cube[*iter]-1].end(); itt++ ) { std::cout
Help with loops   (85 Views)
I am trying to do this in dos with c++ 6 5 7 4 6 8 3 5 7 9 I am trying to do this with just loops so far i have tried a million things to get this to work and can't seem to get the math down so far i have done this for ( int a = 0; a < 6; a++) { for(int b = 0; b < a; b++) { for(int c = 6; c > 0; c--) { cout
Variable declaration and while loops   (107 Views)
, This seems like an extremely basic question, and I'm a bit embarassed that I can't answer it myself. I just recently started using GCC and tried to type the following code: while ((int i = getint()) != 0) { /* something */ } which my compiler has been supporting for a good few years. Under GCC this doesn't compile. At first it didn't even occur to me that maybe the code was invalid. I just thought GCC was being stupid. But then it occurred to me that this probably really isn't valid C++, and that my compiler has just been breaking the rule. Can anyone shed some light Can you declare a variable in a while loop similar to how you can with a for loop
random function in loops   (183 Views)
I hope someone could correct this simple excerpt since I fail to see how I can make rand() function works in a loop manner ///////////////////////////////// srand(time(NULL)); int r1=rand()%10+1; for(int o=0;o