linked list/ stored data

This is a discussion on linked list/ stored data within the C++ Programming forums, part of the General Programming Boards category; i trying to read a line of users input and store that information. My problem is that when the data ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    49

    linked list/ stored data

    i trying to read a line of users input and store that information. My problem is that when the data is being read it has a problem with the spaces.


    Code:
    struct link_list
       {
       char info[100];
           struct link_list *next;
           struct link_list *pre;
       };
    
    typedef struct link_list link;
    
    int count=0;
    
    class ll
      {
       link *first,*list,*temp,*pointer;
        public:
        ll()
          {
    	 first=list;
          }
        int isempty(void);
        void creat(void);
        void append(void);
        void del(void);
        void insbeg(void);
        void ins(void);
        void display(void);
        void inv(void);
        void find(char x[100]);
     };
    
    int ll::isempty()
      {
         if(first==list)
    	  return(1);
         return(0);
    	
     }
    void ll::creat()
      {
          if(first==list)
    	  count=0;
          else
    	  count=1;
    	if(count!=0)
    	{
    	 cout << endl <<"THE LIST ALREADY EXISTS!";
    	 return;
    	}
           char ch;
    	   char tmp[100];
           list=new link;
           first=list;
           while(1)
    	 {
    	    cout<<"Enter Info:";		
    	 //   cin >> list->info;
    
    	    cout<<"Do you want to continue:(y/n)";
    	    cin >> ch;
    	    if(ch=='n')
    	      break;
    	    list->next=new link;
    	    list->next->pre=list;
    	    list=list->next;
    	}
    	list->next=first;
    	first->pre=list;
         }
    ive tried to use a buffer (tmp[100]) to store the data like this..
    Code:
    	    cout<<"Enter Info:";		
    	    cin >> tmp;
                        **now how can i get tmp into list->info**
    Code this

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Search the board for getline...
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    49
    as in
    Code:
    cin.getline(tmp,100,'\n');
    ?

    or can i bypass the buffer and go with
    Code:
    cin.getline(list->info,100,'\n');
    Last edited by SpEkTrE; 04-11-2005 at 10:55 AM.
    Code this

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Try and see!
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  5. #5
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    I know cin.getline works when you need to input stuff.

    So it recognises the white spaces.

    cin.getline(array,81);


    if(array[a]==' ');
    {
    cout<<"You have found a white space"<<endl;
    }

    However, I've searched the web and the posts and I can't find stuff that to recognise white spaces or \n when I use a fstream and file pointers. I'm surprised at how elusive this is. Isn't there just a heading you can include or someting so it doesn't ignore white spaces. So you would have:


    Code:
    fstream file_pointer;
    file_pointer.open("test.txt",ios::in);
    char array[81];
    
    do
      {
        file_pointer>>array;
         if(array=='\n')
         {
            cout<<"newline was found";
         }
         if (array== '  ')
         {
           cout<<"a space was found";
         }
    ..........

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    You can use getline() with an ifstream object you declare just like you do with the standard istream object cin.

  7. #7
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Consider this:
    Code:
    #include<iostream>
    #include <fstream>
    #include <cstring>
    	
    using namespace std;
    
    int main()
    {
            
            char buf[81];
            char * p; 
            ifstream in("test.txt");
            while(in.getline(buf,81))
            {
                cout<<buf<<endl;
            }
            p = strchr(buf, ' ');
            if ( p != NULL)
            {
                 cout<<"space found";
            }
           return 0; 
    }
    Maybe not representative example but it's only one way to do it...
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  8. #8
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    int main()
    {
            
            char buf[81];
            char * p; 
            ifstream in("test.txt");
            while(in.getline(buf,81))
            {
                cout<<buf<<endl;
            }
            p = strchr(buf, ' ');
            if ( p != NULL)
            {
                 cout<<"space found";
            }
           return 0; 
    }
    I tried this but it still doesn't recognise white spaces?

    Josh do u have any code??

  9. #9
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Do you mean that p==NULL in that example? Are you still seeing the contents of the file printed to the screen?

    The more specifically you tell us your problems, the easier it us for us to help.

  10. #10
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Ups, my mistake
    After while loop end of course there is no point to seek for characters

    Code:
    int main()
    {
            
            char buf[81];
            char * p; 
            ifstream in("test.txt");
            while(in.getline(buf,81))
            {
                cout<<buf<<endl;
                p = strchr(buf, ' ');
                if ( p != NULL)
                {
                     cout<<"space found";
                }
            }
            
           return 0; 
    }
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Try this:
    Code:
    cout << "enter a phrase with a space, a tab and a newline char." << endl;
    cout << "end the input by entering an asterix, * and the enter key" << endl;
     
    char input[81];
    cin.getline(input, 80, '*');
     
    /*now enter the following line using enter key and tab key instead of the \n and \t and complete the input after the * by pushing the enter key a second time.
     
    Hi.\nThis is \t a test.*
     
    */
     
    cout << "the characters entered were:" << endl;
    int max;
    int i;
    max = strlen(input);
    for( i = 0; i < max; ++i)
    {
      if(input[i] == ' ')
    	 cout << "space";
      else if(input[i] == '\t') 
    	 cout << "tab";
      else if(input[i] == '\n')
    	 cout << "new line char";
      else
    	 cout << input[i];
      cout << ' ';
    }
    You're only born perfect.

  12. #12
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    isspace() returns true if the character passed to it is ' ', a tab, a newline, etc. Just remember to #include <cctype>

  13. #13
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    And to be precise:
    The standard white-space characters are the following: space, tab, carriage-return, newline, vertical tab, and form-feed. In the C locale,
    isspace() returns true only for the standard white-space characters.
    But notify that the behavior of the isspace() function is affected by the current locale.

    - Micko
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  2. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21