having problem with getline for string in linked list

This is a discussion on having problem with getline for string in linked list within the C++ Programming forums, part of the General Programming Boards category; hi i decided to make my linked list program accept getline characters so i use getline( cin,s) which use string ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    61

    having problem with getline for string in linked list

    hi i decided to make my linked list program accept getline characters
    so i use getline( cin,s) which use string s declared in string
    why is no error but the part of reading getline(cin,s) doesn't get input??

    here's my coding

    Code:
    #include<iostream>
    #include<cstring>
    #include<string.h>
    using namespace std;
    
    struct node
    {
           string course,coursename,name;
           int matric;
           float chour;
           node *next;
           } yours;
    node *start_node=NULL;
    node *current;
    int option=0;
    
    void add_node()
    {
         node *temp,*temp2;
         temp=new node;
         
    
            cout << "enter a course code : " ;
         getline (cin,temp->coursename);
             cout << "enter a subject : " ;
         cin >> temp->course;
              cout << "enter the credit hour : " ;
         cin >> temp->chour;
         cout << endl;
         temp->next=NULL;
         if(start_node==NULL)
         {
         start_node=temp;
         current=start_node;
         }
         else
         {
             temp2=start_node;
             while(temp2->next!=NULL)
             {
             temp2=temp2->next;
                                     }
             temp2->next=temp;
         }     
    }
    void display_node()
    {
         node *temp;
         temp=start_node;
         if(temp==NULL)
         {
         cout << "the list is empty" << endl;
                       }
         else
         {
               
            
                              /*void display()		//display function
    {
    	cout<<endl;
    	schedule *temp;
    	temp=ptr_head;
    	int c=1;
    	if(temp==NULL)	//check whether the stack is empty
    	{
    		cout<<"EMPTY"<<endl;
    	}
    	else			//print if stack not empty
    	{
            cout<<"- This is the Course Registration Period"<<endl;
    		cout<<"- Range of  Matric No - Time  Period -"<<endl;
    		cout<<"------------------------------------------------------------"<<endl;
    		while(temp!=NULL)
    		{
    			cout<<" "<<c<<"  "<<temp->matric<<"  "<<temp->matric2<<"  "<<temp->time<<"  "<<temp->time2<<endl;
    			temp=temp->next;
    			c++;
    		}
    		cout<<"------------------------------------------------------------"<<endl;
    	}
    	cout<<endl;
    }*/
    
           cout<<"- This is your course registration slip for this semester"<<endl;
           cout<<"- Cours  Name - Course code  - credit hours"<<endl;
           cout<<"------------------------------------------------------------"<<endl;
            while(temp!=NULL)
        
             {
             	cout<<"  "<<temp->course<<"  "<<temp->coursename<<"  "<<temp->chour<<"  "<<endl;
             
             temp=temp->next;
             cout << endl;
            
            
             }
                cout<<"------------------------------------------------------------"<<endl;
             cout << "the list is finished" << endl;
         }
    }
    void del_start_node()
    {
         node *temp;
         temp=start_node;
         start_node=start_node->next;
         if(temp==NULL)
         {
         cout << "can't delete coz list is empty" << endl;
                       }
          else{
          cout << "first node is deleted " << endl;
          delete temp;
               }     
    }
    void del_end_node()
    {
         node *temp,*temp2;
         if(start_node==NULL)
         {
         cout << "can't delete node coz list is empty" << endl;     
                             }
         else
         {
             temp=start_node;
             if(temp->next!=NULL)
             {
             delete temp;
             start_node=NULL;
             }
             else
             {
                 temp2=temp;
                 temp=temp->next;
             }
         delete temp;
         temp2->next=NULL;
    }
         
    }
    int main()
    {
      int matric;
      string name;
               cout << "enter a your name : " ;
         cin >>name;
         cout<< endl;
              cout << "enter a matric number : " ;
         cin >>matric;
         cout << endl;
        int option;
    
        do
        {
            cout << "please choose ..." << endl;
            cout << "0 to exit " << endl;
            cout << "1 to add subject " << endl;
            cout << "2 to delete first subject " << endl;
            cout << "3 to delete last subject " << endl;
            cout << "4 to display the list of your registered subjects " << endl;
            cin >> option;
            
            switch(option)
            {
            case 1 : add_node();
                     break;
            case 2 : del_start_node();
                     break;
            case 3 : del_end_node();
                     break;
            case 4 : 
        /*printf("\n------------------\n");
        printf("ID number: %s\n", studno_1.id_num);
        printf("Name     : %s\n", studno_1.name);
        printf("Gender   : %c\n", studno_1.gender);
        printf("Age      : %d\n", studno_1.age);
        printf("------------------\n");*/
                
             cout<<"\n-----------------------------"<<endl;
             cout << "NAME :"         <<name  <<"\t"<<endl;
             cout << "MATRIC NUMBER :"<<matric<<"\t"<<endl;
             cout<<"-----------------------------\n"<<endl;
             display_node();
                     break;
            }
        }while(option!=0);
        system("pause");
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    I would be more inclined to trace your code if you formatted it properly, but since you did not, all that I will say is: you probably left a newline in the input buffer after reading formatted input with operator>>. The getline call consumed this newline instead of getting the next line. You can solve this by ignoring characters until a newline after using each operator>> for formatted input.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    61
    thanks.
    i found at another place that we can also uses dummies
    i used in another example dummies for clearing the newline
    but it seems it reads my input but does not return the output
    any help for this/?

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    struct Pizza {
        string szCompany;
        float diameter;
        float weight;
    };
    Pizza* pz = new Pizza;
    
    int main()
    {
        
        char dummy;
    
    cout << "Enter the weight: ";
    cin >> pz->weight;
    
    
    
    
    
    cout << "Enter the company name: ";
    // use getline because the company name can have spaces in it.
    getline(cin, pz->szCompany);
    cin.get(dummy);
    
    
    cout << "Enter the diameter: ";
    cin >> pz->diameter;
    
    //cin.clear();
    //cin.sync();
    
    cout << "\nCompany name: " << pz->szCompany;
    cout << "\nWeight: " << pz->weight;
    cout << "\nDiameter: " << pz->diameter;
    
    // cannot forget this step.
    delete pz;
    system("PAUSE");
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    26
    cin extraction operator >> will march past the white space characters (including newlines) to grab the input. However, once it gets its expected input, it will not look further and leave the white space characters in the input buffer.

    cin.getline will just grab everything till (and including) the newline character.

    When you mix the two (some inputs with extraction operator and some with getline) often what will happen is cin.getline will just pick up the "leftover" newline characters of cin's extraction operator. So, strings read by cin.getline will be empty.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 08:53 PM
  2. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 11:03 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  4. Linked List problem
    By spec85 in forum C Programming
    Replies: 1
    Last Post: 06-14-2002, 04:58 AM
  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