Linked list using ctrl+z out from loop.But doesn't work well...

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2008
Posts: 2
Reputation: gReen_aXe is an unknown quantity at this point 
Solved Threads: 0
gReen_aXe gReen_aXe is offline Offline
Newbie Poster

Linked list using ctrl+z out from loop.But doesn't work well...

 
0
  #1
Jan 31st, 2008
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. typedef struct List {
  5. int data;
  6. List*link;
  7. List(){
  8. data=0;
  9. link=NULL;}
  10. }SList;
  11. SList*L,*P,*temp;
  12. int x;
  13.  
  14. SList*createnew(int x){
  15. temp=new List;
  16. temp->data = x;
  17. temp->link = NULL;
  18.  
  19. return (temp);
  20. }
  21.  
  22. SList*findLast(SList*L){
  23. temp=L;
  24. while(temp->link != NULL)
  25. temp=temp->link;
  26. return (temp);
  27. }
  28.  
  29. void printList(SList*L){
  30. temp=L;
  31. while(temp!= NULL){
  32. cout<<temp->data<<endl;
  33. temp=temp->link;
  34. }
  35. }
  36.  
  37. int count(SList*L){
  38. temp=L;
  39. int i=0;
  40. while (temp != NULL){
  41. i++;
  42. temp=temp->link;
  43. }
  44. return (i);
  45. }
  46.  
  47. void addend (){
  48. L=NULL;
  49. cout<<"Please enter a number "; cin>>x;
  50. do{
  51.  
  52. if(cin.eof())
  53. break;
  54. P=createnew(x);
  55. if(L!=NULL){
  56. temp=findLast(L);
  57. temp->link=P;}
  58. else
  59. L=P;
  60. cout<<"Please enter a number ";
  61. }while ( cin>>x);
  62.  
  63. }
  64.  
  65. int main(){
  66. addend();
  67. printList(L);
  68.  
  69. return 0;
  70. }



Why i must enter ctrl+z 2 times to out from the loop..?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Linked list using ctrl+z out from loop.But doesn't work well...

 
0
  #2
Jan 31st, 2008
worked ok for me using VC++ 2008 Express on Vista Home Premium. I had to press Ctrl+Z followed by <Enter> keys.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Linked list using ctrl+z out from loop.But doesn't work well...

 
0
  #3
Jan 31st, 2008
Same here, using GCC and XP.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,861
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: Linked list using ctrl+z out from loop.But doesn't work well...

 
0
  #4
Feb 1st, 2008
>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:
  1. this is a test^Z
  2. ^Z
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:
  1. #include <iostream>
  2.  
  3. #define CTRLZ 0x1A
  4.  
  5. int main()
  6. {
  7. char ch;
  8.  
  9. while ( std::cin.get ( ch ) && ch != CTRLZ )
  10. std::cout<< ch <<": "<< int ( ch ) <<'\n';
  11. }
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2
Reputation: gReen_aXe is an unknown quantity at this point 
Solved Threads: 0
gReen_aXe gReen_aXe is offline Offline
Newbie Poster

Re: Linked list using ctrl+z out from loop.But doesn't work well...

 
0
  #5
Feb 5th, 2008
Actually i'm using visual C++..After heard all the comment, i'm trying dev-C++..Actually the code is working well..Thanx for all..
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,861
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: Linked list using ctrl+z out from loop.But doesn't work well...

 
0
  #6
Feb 5th, 2008
>After heard all the comment, i'm trying dev-C++..
You're not likely to see a difference with that particular problem as long as it's a Windows compiler.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 858 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC