| | |
Linked list using ctrl+z out from loop.But doesn't work well...
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2008
Posts: 2
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; typedef struct List { int data; List*link; List(){ data=0; link=NULL;} }SList; SList*L,*P,*temp; int x; SList*createnew(int x){ temp=new List; temp->data = x; temp->link = NULL; return (temp); } SList*findLast(SList*L){ temp=L; while(temp->link != NULL) temp=temp->link; return (temp); } void printList(SList*L){ temp=L; while(temp!= NULL){ cout<<temp->data<<endl; temp=temp->link; } } int count(SList*L){ temp=L; int i=0; while (temp != NULL){ i++; temp=temp->link; } return (i); } void addend (){ L=NULL; cout<<"Please enter a number "; cin>>x; do{ if(cin.eof()) break; P=createnew(x); if(L!=NULL){ temp=findLast(L); temp->link=P;} else L=P; cout<<"Please enter a number "; }while ( cin>>x); } int main(){ addend(); printList(L); return 0; }
Why i must enter ctrl+z 2 times to out from the loop..?
>Why i must enter ctrl+z 2 times to out from the loop..?
The Windows shell has trouble handling EOF properly unless it's the first character on the line. Most likely you're doing something like this:
The program only stops at the second ^Z because the first is preceded by valid characters before a newline. If you want portable code you just have to suffer with it, but you can also perform a workaround because the first ^Z is typically treated as a legitimate character and will be returned by the input function. You can take advantage of that by testing for the specific character value on top of the standard EOF mechanism:
The Windows shell has trouble handling EOF properly unless it's the first character on the line. Most likely you're doing something like this:
C++ Syntax (Toggle Plain Text)
this is a test^Z ^Z
C++ Syntax (Toggle Plain Text)
#include <iostream> #define CTRLZ 0x1A int main() { char ch; while ( std::cin.get ( ch ) && ch != CTRLZ ) std::cout<< ch <<": "<< int ( ch ) <<'\n'; }
New members chased away this month: 5
![]() |
Other Threads in the C++ Forum
- Previous Thread: C++ problem
- Next Thread: Bound Checking
Views: 858 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






